Anton Lindstrom (about, @twitter, @github)

Visual block inserting in Vim


I've recently started using the visual features of the Vim editor. One of the features is visual block inserting. There's a lot of use cases for visual block inserting. Since being introduced to it, I've been mostly using it to prepend multiple rows with comment signs.

An example may provide a better overview, this is the example we have:

1  #include <stdio.h>
2  #include <stdlib.h>
4  int main(int argc, char **argv[])
5  {
6          printf("Hello World\n");
8          if (argc > 1)
9                  exit(EXIT_FAILURE);
11          exit(EXIT_SUCCESS);
12 }

In this case, we have a simple program that returns the exit code 1 when we supply some arguments to the program.

Let's decide we don't what to use that, we'll fire up Vim and comment out the if block (row 8-9).

Go to the 8:th row, press Ctrl-V to go into Visual Block mode. Use j to mark row 9 as well. Then Press Shift-i and insert // before row 8. Press Esc and you will see that row 8 and 9 will look like this:

8  //         if (argc > 1)
9  //                exit(EXIT_FAILURE);

The documentation gives some examples and there's also :help v inside Vim to get more information.