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 operations using the default register

#51

Run time:

Vim’s default register makes it easy to perform some of the most basic types of cut, copy, and paste operations. That’s partly thanks to the p command, which behaves differently depending on whether the default register contains a characterwise or linewise region of text.

Shownotes

From Vim’s documentation (:h quotequote):

Vim fills this register with text deleted with the “d”, “c”, “s”, “x” commands or copied with the yank “y” command, regardless of whether or not a specific register was used.

Vim calls this the unnamed register, which is a sweet oxymoron. I prefer to call it the default register.

Here are descriptions of a few simple commands that interact with the default register:

command description
x cut one character and write it to default register
dd cut one line and write it to default register
yy copy one line into default register
p put text from default register after cursor

There’s more to the p command than meets the eye: its behavior differs subtly depending on whether the register contains a characterwise or linewise region of text (see :h linewise-register). When the default register contains a characterwise region of text, the p command puts the text after the current character. Whereas when the default register contains a linewise region, the p command puts the text after the current line.

Even though xp, ddp, and yyp each consist of two separate commands, we can think of each pair as forming an aggregate command:

commands description
xp toggle characters
ddp toggle lines
yyp duplicate line

You can explicitly tell Vim that you want to paste the contents of the default register by prefixing the p command with "" (that is, doublequote twice). Of course, ""p and p are equivalent, since the default register is used by default.

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