Linux, OpenSource, Programming And Hacks

May 27, 2016

How to Comment and Uncomment Multiple Lines in Vim

May 27, 2016

There are several ways you can comment and uncomment a range of lines in vim editor.


Method I (easiest)

Step 1: Make sure your are in command mode by pressing Esc key

Step2: Move your cusor to the start of the starting line of the block that you want to comment out
press Ctrl + v for visual mode and now press the down arrow key to select till the last line you wanna comment.

Step 3: Press upper case I (shift + i)
and type # or // for commenting depending on the programming language.
and finally press Esc key

wait a sec and it's done!

Method II

In this method we use substitution to comment out lines.
The first two steps are the same as 'Method I'.

Step 1: Make sure you are in command mode

Step 2:. Goto the first line you want to comment
press Ctrl+v
and now go down to the last line you wanna comment

Step 3: type :s/^/#  for commenting with #


type :s/^/\/\/ for commenting with // (we are simply placing a backslash to each \ since its a special character.)

Note: '^' is a special search character for the start of the line.


We can uncomment simply by selecting the commenting characters like # or // and delete them with the x or d key.

Make sure you are in command mode by Pressing the Esc key.

Step 1: Goto to the first line you want to un-comment and press Ctrl+v then press down arrow key to the last line you wanna un-comment. if the comment symbol is more than one character like a '//' then move the cursor to the left/right selecting all the symbols.

Step 2: hit x or d


Here is a video that will explain everything better!

No comments:

Post a Comment