Vim: comment blocks

I have become tired of using the arrow keys to navigate around my source code just to insert comments on each line of a block of code.  Wouldn’t it be nice if I could just select a few lines, hit a button or two, and have all of the lines commented out or un-commented?  (Like in Max-PlusII or QuartusII support in their VHDL editors…)

Well Vim has the ability to do so!  First of all, let me introduce the visual-block command.  In Vim, pressing v allows you to select text. Visual-block is enabled by pressing Cntl-v.  Notice the difference in the following two screen shots.

visual
Regular Visual Mode in Vim
block
Visual-block mode in Vim

In visual mode, if you move downward, the entire line is selected, but in visual-block mode, only the text in the same column is selected.  In visual-block mode, if you move to the right, more columns are selected.

To insert comments, select the text rows that you want to block comment-out with visual-block, Cntl-v. Now press I, and then the commenting characters that you want to insert.  For C++ you would want //, for Ada or VHDL , or for scripting languages a # would do the trick. Finally, press the Esc key to end the visual-block command.  The block of code should now be commented out!

To remove comments from a block of test, usual the visual-block command (Cntl-v) and select the columns of comment text, and then press d, and that should take care of it.

Removing comments is easy enough, but wouldn’t it be nice to have an easier way to add comment blocks without all of those keys to press?  Well, why don’t we map the keystrokes to a single key?  Edit your .vimrc and add the following as needed:

map <F2> I–<Esc><Esc>
map <F3> I//<Esc><Esc>
map <F4> I#<Esc><Esc>

Now I can simply select the text block with visual-block, and press a function key to insert comments for Ada, C++ or shell scripts.  I’m just beginning to see the real power of Vim.

Advertisements

Vi: word completion

For my new job I’m on the Linux platform, and due to the amount of software development that takes place on servers in remote locations, all of the developers on my team utilize the BASH shell, the konsole terminal program, and Vi for editing.  Though I learned the basics of Vi in college due to the lack of other editors on the computer science department servers, compared to the other developers, working with Vi and the terminal feels quite clumsy to me.  I’m working on weaning myself from graphical editors and eventually enjoying the productivity boost of using a solid editor and the more advanced features of the shell.  I hope to make a regular post with tips on improving one’s productivity in Vi, BASH, and even with konsole.

Today I’d like to introduce word completion.  It does not require any third party extensions or libraries and it is enabled by default.  If you’re typing a word in a file or in your code that already exists somewhere in the file, type at least one letter of the word and then do the following:

Cntl-n

You should see a terminal-like pop-up containing all of the possible choices.  Press Cntl-n to traverse the list going downwards, and press Cntl-p to traverse the list in the opposite direction.  That is all there is to it.  It certainly helps with long_variable_names_like_this!

C++ gotcha

I came across an amusing example in Meyers’ Effective STL Item 6***.

class Widget { ... };
Widget w();

As Meyers explains,  it seems like the default constructor is called. However, in C++ this second line is actually declaring a function ‘w’ that takes no parameters and returns a ‘Widget’. If the code author intended for the constructor to be called, he would have to write:

class Widget { ... };
Widget w;

I chuckled to myself after I saw this because the former will cause the Java compiler to get angry. This is another example of how you have to pay attention when you write C++.

*** I love Meyer’s works and his writing style.  He writes to you as a professional and an equal, even though he is a master.