31

It happens more often than not that I have to comment several lines at once in Vim. Methods that I know are not as fast as say TextMate way to comment lines out.

What is your favorite way to do it?

I currently use:

Method 1:

  • go to first char of a line and use blockwise visual mode (ctrl-v)
  • go down/up until the first characters of every lines you want to comment out are selected
  • use shift-i and then type your comment character (# for Ruby)
  • use esc to insert the comment character to every line

Method 2:

  • select lines you need to comment out using linewise visual mode (shift-v)
  • type : which gives you a :'<,'> prompt which you can extend to :'<,'>s/^/#/

Method 3:

  • go to the first line to be commented out
  • make a bookmark for example typing mm
  • go to the last line to be commented out
  • type :'m,.s/^/#/

I like method 1 the most but I still hope there is a better way.

Community
  • 1
  • 1
dimus
  • 8,712
  • 10
  • 45
  • 56
  • I use `#if 0`/`#endif`, but probably that's not what you want ;-) – Michael Krelin - hacker Dec 10 '09 at 20:53
  • gms8994, type character and hit ESC. – Michael Krelin - hacker Dec 10 '09 at 21:10
  • 1
    This question pops up every once in a while: http://stackoverflow.com/questions/1676632/whats-a-quick-way-to-comment-uncomment-lines-in-vim http://stackoverflow.com/questions/58584/in-vim-what-is-the-best-way-to-select-delete-or-comment-out-large-portions-of – innaM Dec 11 '09 at 08:23
  • How does someone without enough rep to vote for close bring a duplicate question to a moderator's attention? I've never had any luck with flagging. – Yewge Dec 11 '09 at 15:52

6 Answers6

9

I think you described the most popular ways to comment code, but if you are open to use Vim Scripts, give a look to those:

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    These days tpope's [commentary.vim](https://github.com/tpope/vim-commentary) is another rather popular choice. – glts Nov 30 '13 at 15:42
4

I use a keymap for the regex part, but I do the same visual selection first. Usually using:

vip

to get the visual block (paragraph visual selection)

then using

\cc
\co

for comment add/remove (cc,co chosen for muscle memory reasons)

with the mappings defined in .vimrc as:

vmap <leader>cc :s/^/#/<cr>
vmap <leader>co :s/^#//<cr>
zen
  • 12,613
  • 4
  • 24
  • 16
2

Though this is rather old I just wanted to add my solution which is pretty similar to everyone elses but adds the unhighlighting function. In my .vimrc file I have the following maps:

:vmap `c :s/^/\/*/<cr>gv:s/$/*\//<cr>:noh<cr>i
:vmap `r :s/^\/\*//<cr>gv:s/\*\/$/<cr>:noh<cr>i

Note: I use /*line of code*/ style of commenting to be compatible with old c code. In vim I simply highlight the lines and push `c to comment and `r to remove comments.

Josh Albert
  • 1,064
  • 13
  • 16
1

I normally just save the step in a macro and then invoke the macro in whichever fashion I feel like.

Sridhar Iyer
  • 2,772
  • 1
  • 21
  • 28
1

Plugins are the way to go. They are extensible, they already support more filetypes that you would ever use, they are automagically able to toggle the commented state of a line (in other words: no need to consume two shortcuts where one is enough).

See the list given by CMS.

Community
  • 1
  • 1
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
1

I was looking for a set of commands for ANSI C one-line commenting and I've tried out most of the answers at SO.

None of them suit my needs and as I have to use an old 7.2 version of Vim at the moment and I cannot easily download suitable plugins I came up with these handy mappings:

:nnoremap <leader>c :exe "normal mqI/* "<esc> :exe "normal A */"<esc> :exe "normal 'q"<cr>
:nnoremap <leader>r ^xxx$xxx^

<leader>c comments out a line and goes back to the beginning of that line.

<leader>r removes the the comments from the beginning and the end of the current line.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79