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.

Pasting from Insert mode

#55

Run time:

We don’t have to be in Normal mode to paste the contents of a register. The <C-r>{reg} command lets us paste a register from Insert mode (and it works in commandline mode too!) Using this command allows us to make changes that can be repeated with the dot command.

Shownotes

In Insert mode, we can use the <C-r>{reg} command to paste the contents from any of Vim’s registers. For example, pressing <C-r>a would insert the contents of named register ‘a’, while pressing <C-r>0 would insert the contents of the yank register. In Vim’s documentation, it lists a few more registers that can be useful in this context:

register description
" the default register
% the current file name
# the alternate file name
* the clipboard contents (X11: primary selection)
+ the clipboard contents
/ the last search pattern
: the last command-line
. the last inserted text
- the last small (less than a line) delete

Note that the <C-r>{reg} commands can also be used at Vim’s command line.

Variations on <C-r>{reg}

The <C-r>{reg} command comes with a few variations, including <C-r><C-r>{reg}, <C-r><C-o>{reg}, <C-r><C-p>{reg}. In the video tutorial, I demonstrate the the difference between <C-r>{reg} and <C-r><C-o>{reg}, by attempting to complete the Vimgolf challenge, Words in parens.

We can use <C-r>" to insert the text that was just deleted with the cw command. That works fine for a one off change, but if we attempt to repeat the change with the dot command, it reproduces the result of the last insertion. In the example, that means using the dot command on two produces (one), when we actually want (two).

If we use <C-r><C-o>" instead, Vim inserts the contents of the default register literally, using the current value of the default register. In this case, we can use the dot command to repeat the remaining changes.

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