There is any piece of text, I would like to completely comment it out in a couple of actions, regardless of its size.
6 answers
I wouldn’t like to use the plugin for the simple reason - that we need versatility) Often you have to work from different machines, including foreign ones - that is why only pure vim )
For myself, I’ve got this option now: ctr+v further } - until the end of the paragraph, either j,k selection. Next I , # and <ESC>
I use the tComment plugin for this. With it, you can select the text, press the key combination (hot keys are configured), and the necessary lines will be commented out. Uncommenting is the same. Plus there are some more useful hotkeys.
You can use The NERD Commenter or analogues . Commenting on pressing two or three buttons. Previously they require to select the text, and this is a separate action, and in some cases it can be performed in one or two clicks.
This answer is only suitable for single-line comments, but nonetheless. To comment on C ++ code, you can do this:
^ Ctrl + v 5jI //
where ^ - Shift + 6 , 5 - the number of lines down, I - Shift + i . Any movement instead of 5j is possible , for example, } .
For another language, you can replace // with the corresponding comment character ( # , -- , etc.)
Upd: Thanks to @Dofri and @KoVadim for the comments. Dofri was more right about Ctrl + v .
- yes, you beat me literally for a minute) for block comments I wrote below) - Dofri
- onejust keep in mind that not all languages code is commented using two slashes. Another very popular comment on the grid :) - KoVadim
To comment on a block, I always used the visual selection mode, which is called by
ctrl + v
(in gvim for windows, the default is ctrl + q).
You need to place the cursor, for example, at the beginning of a line at the top of the block to be selected, press
ctrl + v
, move the cursor to the end of a paragraph by pressing
}
then click
I (shift + i)
, enter a comment character (eg #) and press ESC.
more details:
- it seems like the results of checking answers and they are gone ... - user197988
- and what is there to check? you just reprinted the last answer :) - Dofri
I had a desire not just to quickly comment / uncomment a piece of text, but also to automatically select the correct comment symbol.
And out of curiosity, I wrote a plugin, which I have been using and used to for a couple of years, I must say)
It works like this: select a piece of text and press hh . If you select a commented piece and press hh , then the piece is uncommented.
.vimrc:
nmap hh :LinesCommentNextState <CR> vmap hh :LinesCommentNextState <CR> plugin code:
function! s:LinesCommentNextState() range let l:extension = expand('%:e') let l:comment_symbol = "#" if l:extension == "c" let l:comment_symbol = "\/\/" elseif l:extension == "cpp" let l:comment_symbol = "\/\/" elseif l:extension == "h" let l:comment_symbol = "\/\/" elseif l:extension == "hpp" let l:comment_symbol = "\/\/" elseif l:extension == "xs" let l:comment_symbol = "\/\/" elseif l:extension == "vim" let l:comment_symbol = "\"" elseif l:extension == "lua" let l:comment_symbol = "--" else "default '#' endif let l:first_line = getline(a:firstline) let l:need_comment = 1 " if string already commented, no need comment twice if l:first_line =~ '\v^(\s)*' . l:comment_symbol let l:need_comment = 0 endif for n in range (a:firstline, a:lastline) let l:line = getline (n) if len(l:line) == 0 continue endif if l:need_comment == 1 " comment it! let l:new_line = l:comment_symbol . l:line " but if beginning from space, need save all spaces if l:line =~ '\v^\s' let l:matches = matchlist(l:line, '\v^(\s+)(.*)') let l:new_line = l:matches[1] . l:comment_symbol . l:matches[2] endif else let l:new_line = l:line if l:line =~ '\v^(\s*)' . l:comment_symbol let l:matches = matchlist(l:line, '\v^(\s*)' . l:comment_symbol . '(.*)') let l:new_line = l:matches[1] . l:matches[2] endif endif call setline (n, l:new_line) endfor endfunction function! SaveCursor() let s:cursor = getpos('.') endfunction function! RestoreCursor() call setpos('.', s:cursor) unlet s:cursor endfunction command! -range LinesCommentNextState call SaveCursor() | <line1>,<line2>call s:LinesCommentNextState() | call RestoreCursor()