Visual Block Mode

Visual block mode allows you to select any arbitrary rectangular selection in your document. Most of the time it’s not very useful, but when you’re editing something like a CSV file, it can be pretty awesome.

Visual Block mode always maintains a rectangular selection. Imagine that your text editor isn’t a collection of lines, but a grid filled with characters. You can draw a rectangle on that grid and select all the characters in that rectangle. You can enter it by typing ^v or ^V This is one of the rare cases where the upper and lower case letters do the same thing.

It’s not very useful when dealing with prose, or code, but in something like a CSV file it can be very useful. For example, to select just the fruit in this CSV:

You could delete a column, extract the data from a column, etc. Yes, your content needs to be aligned, but that’s easy to do with things like the Vim Easy Align plugin.

Expanding the selection

When you first hit ^v you’ve got a 1x1 grid consisting of the single character under the cursor. This is the starting corner of your selection. It’s not useful by itself, but like the initial point of a mouse’s click-drag, you’ve got to start somewhere. You can use search, arrow keys, h,j,k,l, etc to expand the selection. The mouse works too. After you’ve entered visual mode shift-click the corner of your desired rectangle that is diagonally across from the where you started the selection.

To expand your selection down, by 20 lines (for example) you simply type 20↓. Expanding your selection down a specific line by typing <line number>G usually looks like it screwed up, because now you’re selected rectangle is to the left of the cursor instead of the right, but really what’s happened is you’ve told vim to move the cursor to the first character of the specified line. Just move the cursor right on the line and the selection will swap to the other side when it passes to the right of the starting point.

Another useful way to select blocks is through the use of marks.

1. Move your cursor to the top left corner of what you want to select and type ma (m sets the mark and a is the name of the mark.
2. Move your cursor to the bottom right corner of your selection by any means: line number, search, hitting ↓ a lot, whatever.
3. Type ^v to start your visual selection
4. Type \a (backtick is the character under the ~) to jump your cursor back to the opposite corner. Voilá one perfect visual block selection.

Deleting the contents

Easy. Select it, then hit x

Multi-line insertion

Ok, you’ve got your visual block selected, and that’s great for copying data, but what if, for example, you wanted to insert something at the start of every line of this selection? Imagine you want to insert another column into your CSV, with the same value on every row.

Once you’ve got your selection type I (shift+i). Now you’re in insert mode, and when you start typing it’ll appear that you’re only typing in that first row, but wait, when you’re done typing, hit <ESC>, and Vim will repeat what you typed at the start of the selection on every line.

Like insert, you have to use Capital A. Hit that, type, hit <ESC> and your typing will be repeated at the right edge of each line of the visual selection.
Yup. Just like insert only your normal c works. No capital required. It’ll delete everything you’ve selected, and drop you into insert mode. When you’re done typing hit <ESC> and it’ll repeat the typing on every line.
Hard word wrap (actually inserting newlines, not just visually wrapping) is your enemy. When I was writing this up I was convinced that c and A didn’t work. In reality what was happening is i was typing a word that crossed my personal word wrap boundary (80 chars) so now, it became a multi-line insertion, and that just doesn’t make sense with the idea of doing at each line boundary of a visual selection. I changed my word wrap boundary and all of a sudden c` was working just fine with visual selections. Yay!