Home Vim column mode

Vim column mode

Today I found myself in the need of Vim's column mode (using regex for what I wanted to do would have been hard).

I googled real quick but the first few hits weren't of much use/very clear and and so after figuring out how it works I think it's worthy of posting here as a) a reminder for myself :) and b) so others who google this may get to see this - faster, hopefully.

Let's say you have sql statements like in the 1st screenshot. Look closely - they are missing a cast of the date fields with to_date. If you are stuck at 10PM at the office on a friday night, like me, and need to perform this, I really think substitution with regular expressions is the least preferable option at this point.

Enter column mode.

Move the cursor to the position where you would like to insert text:

Hit Ctrl+q. This will make Vim go into 'visual block' mode. Select the intersection of columns and rows where you would like to insert your text. In my case, I wanted to insert some text right before the single quote selected:

Now hit Shift+I. This will bring the cursor to the first coordinate selected before in mode. Type in the text you want to enter. Don't fret when you see that you're only updating that line. Once you finish, press ESC - this is where the magic happens:

Isn't vim the greatest text editor in the world? :)

This post is licensed under CC BY 4.0 by the author.


Some Windows magic