Wednesday, November 9, 2016

Comment a code block in gvim


When we are coding we have to comment out some blocks of code and uncomment those time to time. It is easy with IDE like eclipse, netbeens, Visual Studio. I recently moved to vim and here is how to comment and uncomment in gvim. (lets assume character "#" is used for line commenting)

Method 1 

Command mode of vim used for this.
Use this command to comment lines from 10 to 15 with "#"
:10,15s/^/#
Use this command to uncomment lines commented before.
:10,15s/^#/

Method 2

VISUAL BLOCK mode is used for this.
  1. First, move the cursor to the first char of the first line in block code you want to comment. Then press Ctrl + v to enter VISUAL BLOCK mode
  2. Then move cursor with arrow keys to the end of code block that want to comment.
  3.  Then press Shift + i and cursor will come to the begining of code block
  4. The type the commenting character. In this case "#".
  5. Final press Esc and it will comment out the block you marked
To uncomment,

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

No comments:

Post a Comment

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...