Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is your favorite way to comment several lines in Vim? [closed]

Tags:

comments

vim

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.

like image 293
dimus Avatar asked Dec 10 '09 20:12

dimus


People also ask

How do I comment lines in Vim editor?

To comment out a single line in Vim, enter Visual Mode by pressing Ctrl + V . Next, navigate to the line you wish to comment out and press the C key. Depending on your Vim configuration, this should comment out the selected line. That's all there is to it – easy, isn't it?

How do you comment out a block of code in Linux?

You start your editor, navigate down to the beginning row of the block you want to comment out. You hit i to go into insert mode, enter // to comment, hit ESC to go back to command mode, hit j to navigate down to the next row, and then repeat until all the rows are commented out.

What is the comment character in Vim?

The vimrc or Vim configuration file uses the double quote for a single-line comment. A single quote at the start of the line will make this line a comment and not be interpreted as a configuration.


1 Answers

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:

  • The NERD Commenter
  • EnhCommentify.vim
  • tComment
like image 124
Christian C. Salvadó Avatar answered Oct 15 '22 21:10

Christian C. Salvadó