Какой быстрый способ комментировать / раскомментировать строки в Vim?

, если вы включили Rack :: Test *, просто включите методы тестирования

describe "my test set" do
  include Rack::Test::Methods

, затем вы можете использовать метод UploadedFile:

post "/upload/", "file" => Rack::Test::UploadedFile.new("path/to/file.ext", "mime/type")

* ПРИМЕЧАНИЕ. Мой пример основан на Sinatra, который расширяет Rack, но должен работать с Rails, который также использует Rack, TTBOMK

1019
задан Eric Leschinski 25 December 2013 в 02:00
поделиться

10 ответов

Я использую сценарий NERD Commenter . Это позволяет вам легко комментировать, раскомментировать или переключать комментарии в коде.

Как упоминалось в комментариях :

для всех, кого смущает использование, лидером по умолчанию является «\», поэтому 10 \ cc прокомментирует десять строк, а 10 \ cu раскомментирует эти десять строк

170
ответ дан 19 December 2019 в 20:19
поделиться

I have the following in my .vimrc:

" Commenting blocks of code.
autocmd FileType c,cpp,java,scala let b:comment_leader = '// '
autocmd FileType sh,ruby,python   let b:comment_leader = '# '
autocmd FileType conf,fstab       let b:comment_leader = '# '
autocmd FileType tex              let b:comment_leader = '% '
autocmd FileType mail             let b:comment_leader = '> '
autocmd FileType vim              let b:comment_leader = '" '
noremap <silent> ,cc :<C-B>silent <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:nohlsearch<CR>
noremap <silent> ,cu :<C-B>silent <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:nohlsearch<CR>

Now you can type ,cc to comment a line and ,cu to uncomment a line (works both in normal and visual mode).

(I stole it from some website many years ago so I can't completely explain how it works anymore :). There is a comment where it is explained.)

115
ответ дан 19 December 2019 в 20:19
поделиться

Вот как я это делаю:

  1. Перейти к первому символу в первой строке, которую вы хотите закомментировать.

  2. Нажмите Ctrl + q в GVIM или Ctrl + v в VIM, затем спуститесь вниз, чтобы выбрать первый символ в строках, которые нужно закомментировать.

  3. Затем нажмите c и добавьте символ комментария.

Раскомментирование работает точно так же, просто введите пробел вместо символа комментария.

56
ответ дан 19 December 2019 в 20:19
поделиться

Here is a section of my .vimrc:

"insert and remove comments in visual and normal mode
vmap ,ic :s/^/#/g<CR>:let @/ = ""<CR>
map  ,ic :s/^/#/g<CR>:let @/ = ""<CR>
vmap ,rc :s/^#//g<CR>:let @/ = ""<CR>
map  ,rc :s/^#//g<CR>:let @/ = ""<CR>

In normal and in visual mode, this lets me press ,ic to insert comments and,rc to remove comments.

16
ответ дан 19 December 2019 в 20:19
поделиться

I like to use the tcomment plugin: http://www.vim.org/scripts/script.php?script_id=1173

I have mapped gc and gcc to comment a line or a highlighted block of code. It detects the file type and works really well.

9
ответ дан 19 December 2019 в 20:19
поделиться

If you already know the line numbers, then n,ms/# // would work.

7
ответ дан 19 December 2019 в 20:19
поделиться

I use EnhancedCommentify. It comments everything I needed (programming languages, scripts, config files). I use it with visual-mode bindings. Simply select text you want to comment and press co/cc/cd.

vmap co :call EnhancedCommentify('','guess')<CR>
vmap cc :call EnhancedCommentify('','comment')<CR>
vmap cd :call EnhancedCommentify('','decomment')<CR> 
6
ответ дан 19 December 2019 в 20:19
поделиться

I mark the first and last lines (ma and mb), and then do :'a,'bs/^# //

6
ответ дан 19 December 2019 в 20:19
поделиться

Use Control-V to select rectangles of text: go to the first # character, type Ctrl+V, move right once, and then down, up to the end of the comments. Now type x: you're deleting all the # characters followed by one space.

23
ответ дан 19 December 2019 в 20:19
поделиться

For those tasks I use most of the time block selection.

Put your cursor on the first # character, press CtrlV (or CtrlQ for gVim), and go down until the last commented line and press x, that will delete all the # characters vertically.

For commenting a block of text is almost the same:

  1. First, go to the first line you want to comment, press CtrlV. This will put the editor in the VISUAL BLOCK mode.
  2. Then using the arrow key and select until the last line
  3. Now press ShiftI, which will put the editor in INSERT mode and then press #. This will add a hash to the first line.
  4. Then press Esc (give it a second), and it will insert a # character on all other selected lines.

For the stripped-down version of vim shipped with debian/ubuntu by default, type : s/^/# in the third step instead (any remaining highlighting of the first character of each line can be removed with :nohl).

Here are two small screen recordings for visual reference.

Comment: Comment

Раскомментировать: Uncomment

2341
ответ дан 19 December 2019 в 20:19
поделиться
Другие вопросы по тегам:

Похожие вопросы: