Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Creating mappings that accept a count

#62

Run time:

Lots of Vim’s built-in Normal mode commands can be executed multiple times by prefixing them with a count. User-defined Normal mode mappings don’t usually handle counts the way we might like them to. We’ll explore a couple of techniques for making our custom mappings respond predictably to a count.

Shownotes

Problem: make a mapping that handles counts

Suppose that we wanted to create a mapping so that pressing Q executed n.. We could start off with a simple mapping:

nnoremap Q n.

Pressing Q now executes n., giving us two keystrokes for the price of one. Nice! But if we try prefixing our new mapping with a count, something odd happens. For example, pressing 2Q behaves as though we had pressed 2n.. It would be more useful if 2Q was equivalent to n.n..

Solution 1: execute a macro using the expression register

Let’s revise our mapping to this:

nnoremap Q @='n.'<CR>

Now, pressing 2Q is equivalent to n.n., 3Q is equivalent to n.n.n., and so on.

So how does it work? The @ key tells Vim to execute a macro. Rather than using a named register we use the expression register, which lets us specify the contents in place. The characters inside of the quotes are interpreted as keystrokes, and the carriage return enters the string into the expression register.

You can find a short section about this technique in Vim’s documentation by looking up :help map-examples. There’s also an article on the VimWiki explaining it. For more on the expression register, check out episodes 56 and 57.

Solution 2: use the :normal Ex command

This mapping also properly handles a count:

nnoremap Q :normal n.<CR>

This uses the :normal Ex command. I would prefer to see this mapping in a vimrc than the previous example. To my eye it looks less cryptic.

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..