VIM Registers

It's possible to use Vim without learning about registers, but to be really productive it's worth spending some time to understand how actions and commands use them.

There are several groups of registers. Typing help :registers will display these. To see all of the current registers that are in use, type :reg:

Vim 101: Register list

The * register is my system clipboard -- here it contains the text "Vim 101: Registers". The . register stores what I just typed -- this is a read-only register. Pressing escape to enter normal mode then typing yy will "yank" this line. Typing :reg again will now show "Hello" in register 0.

Unnamed Register

Whenever text is deleted or yanked, the unnamed register will be filled. If I type "Zaphod" then move the cursor over the letter "d" and press x to delete it, then the unnamed register will now contain the "d". Typing :reg will confirm this -- the "" register will show the letter "d".

The unnamed register is used as the default argument to many commands, which makes them quick to type. If I want to move a line I'll often find myself typing dd to delete the line, then p to put it somewhere else. These commands can be combined with a named register, which is where registers start to really shine.

Numbered Registers

The 0 register is useful, because it'll always contain the value of the last yank command. Other commands, such as d, cause the unnamed register to be overwritten. The distinction between 0 and the unnamed register is useful if you often yank text without a named register as a quick way of copy and pasting.

Consider the following sequence of key presses:

  • yy -- Yank a line
  • Move to a different line
  • p -- Put the line

This is a convenient way to copy and paste a line somewhere else. Most people learn this quite early on and use it regularly. However, confusion quickly arises when a line is deleted before putting the line:

  • yy -- Yank a line
  • Move to a different line
  • dd -- Delete the line
  • p -- Put the deleted line

In the last step, most people would expect the original yanked line to appear, but the deleted line does instead. Armed with our knowledge of the 0 register we could have done this:

  • yy -- Yank a line
  • Move to a different line
  • dd -- Delete the line
  • "0p -- Put the deleted line from register 0

Named Registers

When I'm working I sometimes find myself rearranging code, but I don't want to lose it and may want to paste it multiple times. This is the perfect time to use a named register to store chunks of code.

Typing "ayy will yank the current line into register a. Typing "ap will put it somewhere else.

Compare that to the previous example where yy was used to yank the current line into the unnamed register. Simply prepending "a specifies that register a should store the result of yy.

These commands all work with visual mode as well. In normal mode, press v to enter visual mode, and then make a selection. Press "ay to yank the selection.

Viminfo

What's even more amazing about registers is they're stored in ~/.viminfo. That means when Vim has been quit and then loaded again, all of the populated registers will still be available.

Conclusion

This brief tutorial should give you enough ammunition to get started with Vim's registers.

There are actually more registers than the ones I've mentioned here, and various useful quirks to their behaviour. Read :help registers to learn more.


http://usevim.com/2012/04/13/registers/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值