Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

How can I select a bunch of text and comment it all out?

Currently I go to the first line, go to insert mode then type # left-arrowdown-arrow and then I repeat that sequence, perhaps saving a few keystrokes by using the . repeat feature to do each line.

Is there anyway I could (for instance) select either multiple lines in visual mode or by using a range of lines and an ex ('colon') command and for that range comment out all the lines with a # to make them a "block comment".

The ability to quickly 'de-comment' (remove the #'s) for a block comment would also be nice.

share|improve this question

marked as duplicate by devnull, jasonwryan, slm, Braiam, terdon Mar 20 at 23:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
    
sed '/START/i \<<"COMMENT" \ /END/a\COMMENT\n' - you only change two lines. And to see what you commented add cat to the beginning. –  mikeserv Mar 21 at 3:38

6 Answers 6

up vote 7 down vote accepted

Ranges:

You can do it with the following command:

:66,70s/^/#

for comment, and for uncomment:

:66,70s/^#/

obviously, here we're commenting lines from 66 to 70 (inclusive).

Hope this helps. Regards.

share|improve this answer
    
Ranges. A good option to have. –  Michael Durrant Mar 20 at 17:53

Visual Block Mode

First, move the cursor to the first char of the first line in block code you want to comment, then type:

CTRL + V

then vim will go in to VISUAL BLOCK mode. Use j to move the cursor down until you reach the last line of your code block. Then type:

Shift + I

now vim go to INSERT mode and the cursor is at the first char of the firts line. Finally, type # then ESC and the code block is now commented.

To decomment, do the same things but instead of type Shift + I, you just type x to remove all # after highlight them in VISUAL BLOCK mode.

share|improve this answer
1  
+1 Visual block mode. A good option. –  Michael Durrant Mar 20 at 17:52
    
Or even use / to jump to the endpoint, or % if the endpoint is a matching } (or { if jumping backwards). I like the visual modes so much more than specifying ranges... –  Izkata Mar 20 at 18:55

Substitute

For the sake of completeness here's another way:

  1. Enter visual mode by pressing v
  2. select the lines you like to comment (up/down arrow or j/k)
  3. enter :s/^/# / which translates to: in selection (:) replace beginning of line (^) with `# '
share|improve this answer
1  
+1 substitute is a good option to know. –  Michael Durrant Mar 20 at 17:49

Markers:

Step1 -> go to first line and set the marker with mt
step2 -> go to the line till which you want range and issue the command :'t,.s/^/#/
In Step1, in mt t is just a name for marker, you can put any character in place of it ,but put the same character in place of t in Step2 also
All the steps are done in command mode

share|improve this answer
    
+1 markers are a good option that I've just starting using. –  Michael Durrant Mar 20 at 17:49

Plugin

There is a plugin, which offers an efficient way of commenting out based on the file system being used.

Also, this answer discusses how to use the NERD commenter for vim. Few more basic instructions on how to use the plugin are discussed here.

share|improve this answer

Use either NERDCommenter as mentioned, but if that's not available to me I use the visual modes. I go into Visual Line mode, make my selection, often its blocks of whitespace, so I go V{ then change to Visual Block mode with Ctrl+Q and then insert the comment symbol at the start of the lines with I, so to comment out a block of text, I'd do

V{<C-Q>I#<Esc>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.