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.

Simple calculations with Vim's expression register

#56

Run time:

The expression register lets us evaluate a snippet of Vimscript code. This is handy when you need to perform simple calculations and insert the result into the document.

Shownotes

From Insert mode, pressing <C-r>= drops us into the expression register. We can enter any Vimscript expression. On pressing Enter, Vim evaluates the expression, and inserts the result into the document. For example, if we entered 2*21 into the expression register, Vim would insert 42 into the document.

In general, we can think of Vim’s registers as being variables that hold a string of text, but the expression register is different. This register behaves like a scratchpad that lets us evaluate Vimscript code. If the return value is a string, or if it can be coerced to a string, then Vim will use the result just as though it had been retrieved from any other register.

When we dial up the expression register, Vim drops us into Commandline mode. Usually, Vim’s command-line uses a colon as the prompt, which lets us run an Ex command. But in this context the prompt is an equals sign, which tells us we’re in the expression register. All of the functionality that is usually present in Commandline mode is available to us in the expression register, including the <C-r>{reg} command.

Creating a mapping to perform calculations

In the video, I created a mapping on-the-fly to speed up the task of inserting the results of calculations into the document. Here’s the mapping:

nnoremap Q 0yt=A<C-r>=<C-r>"<CR><Esc>

This sets up the Q key to perform all of the following work:

command effect
0 move to the start of the line
yt= yank from cursor position up to (but not including) the `=` sign
A move to end of line and switch to Insert mode
<C-r>= dial up the expression register
<C-r>" paste the contents of the default register into the command line
<CR> evaluate the expression register and insert the result into the document
<Esc> return to Normal mode

This mapping is specialized for one particular task, so I wouldn’t save it in my vimrc. Instead, I would just create this macro on-the-fly as a throwaway helper to get me through the task at hand.

This is the final command that I demonstrated:

:'<,'>normal Q

You can read this as follows:

  • :'<,'> for each selected line
  • normal Q execute the Normal mode Q command, using any user-defined mappings

For a discussion of the :normal and :normal! commands, check out chapter 29 of Steve Losh’s Learn Vimscript the Hard Way. Steve suggests always using :normal! and avoiding :normal, which is good advice when writing a script, but it doesn’t apply in this situation. Think about it!

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..