brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Ctrl-r = in Vim insert mode types the result of a calculation

Vim
~/tips/tip-vim-insert-expression

Ctrl-r =19*23<Enter>

Calculates something and inserts the answer while you type.

Need the result of a quick sum in the middle of a sentence? Vim has a calculator one key combination away, without leaving insert mode.

Ctrl-r =19*23<Enter>

In insert mode, Ctrl-r inserts the contents of a register. The = register is special: Vim asks for an expression at the bottom of the screen, works it out and inserts the result. So this types 437 right at the cursor.

The expression is Vim script, so more than arithmetic works:

Ctrl-r =strftime('%Y-%m-%d')<Enter>
Ctrl-r =printf('%.2f', 10.0/3)<Enter>

The first inserts today’s date, the second 3.33. I find the date one handy in notes.

Other registers work with Ctrl-r too, which saves a trip to normal mode. Ctrl-r " pastes what you last yanked or deleted, Ctrl-r / the last search pattern, and Ctrl-r 0 the last yank.

Watch out with whole numbers: 7/2 gives 3, because both sides are integers and Vim does integer division. Write 7.0/2 for 3.5.

Further reading: Vim help: i_CTRL-R_=, Vim help: expression register

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link