Calculator in Vim – The Expression register

~ 1 minute

As a software developer you probably have to perform actions like copying different pieces of your code from multiple files into different locations of your current Vim session. Using only system clipboard, this can be a cumbersome and time consuming task. Once you master Vim registers, your text editing efficiency will greatly improve.

A register is a kind of clipboard. It is a place in memory you can use for storing text. Vim supports several kinds of registers and it fills some of them automatically when you perform actions like yanking or deleting text. Others can be filled explicitly by the user.

It’s possible to be productive in Vim without knowing much about registers, but if you really want to edit texts efficiently, you have to understand how they work.

Think of registers as of different buffers for text. Most operating systems and applications have only a single clipboard that you can use for copy, cut and paste operations. Vim is different. In Vim you have not one or two but nine different clipboards!

Yes, there are nine types of registers in Vim. But in this post, I’ll mention only one – the expression register.

This register lets us evaluate the snippet of Vimscript code. This also means, that we can use it as a simple calculator.

Once you’re in INSERT mode, you only need to hit <Ctrl-r>= and then type the expression. The result will be automatically inserted to your current cursor position.

Have a look at the GIF below, it’s that simple!

 

vim calculator

Btw, if you want to learn more about other Vim registers, get started with Vim, or improve your Vim skills – you should definitely check out my book Mastering Vim Quickly.


If you liked the post, we should get connected - follow me on Twitter

1 Comment Calculator in Vim – The Expression register

  1. Pingback: Calculator in Vim – The Expression Register – Full-Stack Feed

Leave a Reply

Your email address will not be published. Required fields are marked *