vi for the first time can be really intimidating. The learning curve is a bit high and there are many ways to do many things in this simple yet powerful editor. There will come a point where you will be comfortable navigating around using the
l keys and going between command mode and insert mode will be second nature. This is where things get real fun and adding more commands to your
vi toolbox will make you even more productive.
Have you ever found yourself in
vi wanting to comment and uncomment blocks of code? Here are 3 ways to accomplish this.
It may be a little unfair to call this the "bad" way, but it's certainly the most inefficient. 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.
To uncomment the rows, you do almost the same thing. Navigate your cursor the beginning of the last row you want to comment. Hit
xx to remove the first two characters of the line (in our case the
// chars), hit
h to navigate up to the next row, and then repeat until all rows are uncommented.
Being able to navigate around and insert and remove characters like this is definitely not something to be scoffed at. It takes time to even get to this point of sufficiency, but there are better ways.
While in command mode:
: to enter escape mode
11,17s/^/\/\// and then hit
Woh, woh, woh! What just happened here?
Let's break it down in some detail.
11,17 is the range you are interested in. In this case, lines 11-17.
s stands for substitute. The next two arguments are separated by
/, the first being the location or old value and the second being the new value.
s/cat/dog/ takes the first occurrence of the word
cat and replaces it with the word
dog. This will happen for every line.
Now you may have noticed a little problem. We are trying to substitute nothing with
//. Well "nothing" is really the beginning of each line which is represented with
^ (the caret character).
s/^/dog/ would add the word
dog to the beginning of each line.
vi get confused since substitution uses
/ to separate the old value from the new value? This is where we need to use
\ (forward slash) to escape the double back slashes that represent commenting.
/\/\ is the escaped version of
//. Escaping these characters tells the command that we literally want to use
/ twice and not to interpret it as a separator for the command.
We put it all together to get
Uncommenting uses a similar pattern.
: to enter escape mode and enter
Like before, our range is lines 11-17. This time our first argument is the escaped version of
// and the second value is left out. This literally means we are substituting escaped
// with nothing, and therefore removing it.
Phew! That was a lot to cover! This is definitely a very powerful technique, but like many commands involving regular expressions, it can look very ugly.
There is another mode in
vi that can be utilized for our task at hand. Visual block mode allows us to visually block off a section of the editor to perform actions on it. We first start off by navigating down the beginning of the top row we're interested in.
v to enter visual block mode.
j to navigate and block off the beginning of lines 11-17.
i (capital i) to enter insert mode.
// and hit
Uncommenting is just as simple.
v to enter visual block mode again.
Navigate and block off the
//s in lines 11-17.
x to delete.
The combination of using visual mode and commands is the most practical and best way to comment and uncomment code. Apologies to the regex fans out there!
NOTE: Visual mode is actually a feature of
vim and not original
vi. Most modern systems actually use
vim and the binary is either renamed or aliased to
It's hard to believe this is only the tip of iceberg of things that can be accomplished in
vi. Getting pass the initial hump is really the hardest part. More proficiency in
vi translates to more efficiency and productivity. I hope this encourages you to explore and have some fun with this awesome little editor.
DISCLAIMER: The criteria used to place the Bad, Better, and Best judgments are based upon my personal tastes.