Vim Tutorial – Day 3

December 05, 2017

Vim Tutorial – Day 3

In the last Vim tutorial post (see Day 2), I covered how to use Vim’s built-in help menu to get more mileage out of Vim.

Vim logo

In this post, I’m going to introduce some more advanced commands.

Vim as martial arts

Like I mentioned in my Day 1 post about Vim, watching skilled Vim users is akin to watching skilled martial artists.

It’s amazing and yet also a little bit intimidating, especially if you’re new. When I started, there are things I wish I knew. So I’m continuing to write this tiny Vim tutorial to help you get started.

To get their advanced skills, martial artists often train through the use of exercises called katas.

So I’m calling these Vim exercises kata after those simple karate exercises designed to train the mind and body for specific applications.

Move 1 – Joining Lines Without a Space

You can join multiple lines without a space in Vim. For example, let’s say I have the following 5 lines.

1

2

3

If the cursor is position on the number 1 and I then type 5gJ, I get the following output:

123

Kata Steps:
  • Open up Vim. You should be in normal mode by default.
  • Type 5gJ, 4gJ, etc. depending on the number of lines you want to join without a space.

Move 2 – Substitutions Using Vim Regex

You may find yourself wanting to occasionally format a bunch of lines the same way. For example, perhaps you’ll want to change the lines in Block 1 so they look like the lines in Block 2.

Block 1
1,15.1%,good
2,29.8%,bad
3,31.5%,bad

To this:

Block 2
1,.151,good
2,.298,bad
3,.315,bad

To do that you can use a combination of substitution with Vim regular expressions. In command mode, enter:

:%s/\(\d\+\)\.\(\d\+\)%/\.\1\2/g

You’ll notice you have to use the "" to escape a lot of the regex characters. The “\1” matches the first group of digits before the period, and the “\2” matches the second group of digits after the period.

Kata Steps:
  • In Vim normal mode by default, change the lines you want using substitution and regular expressions.
  • Be prepared to figure out which special characters you need to escape.

Move 3 – Very Magic mode

I recently learned that Vim has four different modes of regular expressions that affect which special characters need to be escaped. Previously, I would have to remember to escape certain characters in regular expressions that I used in Vim that I wouldn’t have to in Ruby or Perl. Move 2 shows you the complications this can have.

That’s why I was grateful to have found very magic mode in vim. You can type :help magic in Vim for more details, but basically it means I can use regular expression syntax that I’m used to.

Let’s say I wanted to change Block 3 to Block 4.

Block 3
1,15.1%,good
2,29.8%,bad
3,31.5%,bad
Block 4
1,.151,good
2,.298,bad
3,.315,bad

I would normally use the regex substitution in Move 2. But with very magic mode I can do this:

:%s/\v(\d+)\.(\d+)\%/\.\1\2/g

Kata Steps:
  • In Vim normal mode by default, change the lines you want using substitution and regular expressions.
  • Use the magic mode modifier \v in your regex.

Move 4 – Use a special register for the next yank/delete/etc

So let’s say I want to swap the 5 and 2 in Block so it appears as in Block 6.

Block 5

2,3,5

Block 6

5,3,2

One way to do this is to do a basic delete and paste and assiging the result of each delete into a different register. To do this you use the " followed by a register which can be any of {a-zA-Z0-9.%#:-"}. You can type :help " to see the full help description.

For example, move your cursor over a character you want to delete. If you want to store the it in register c, you would type: "cx. Then to paste from register c, you would type "cp.

Kata Steps:
  • Move your cursor over a character you want to delete. If you want to store the it in register t, you would type: "tx. Then to paste from register t, you would type "tp.

Summary

Hopefully, the above "katas" will help you in Vim. Being able to use regular expressions and special registers will greatly aid your productivity.


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.