vim 的寄存器

If you‘ve been following my series on Vim, it should be clear now that Vim has a pretty clear philosophy of how text editing should work. It‘s based on the Unix philosophy of small composable tools, and doesn‘t necessarily match up with the conventions that other editors use for common commands. So it‘s probably not surprising that Vim has its own way of handling copy and paste operations, and in fact doesn‘t even use those terms. Vim‘s copy and paste handling is minimalist, composable, and powerful, but most people take some time to adjust to it. I‘m going to walk through the basics here, along with a few advanced features that are worth knowing about.

The primary Vim commands for copying something are y (yank) which acts like most people‘s view of copy, and the delete/substitute commands d (delete),c (change), x(single character delete), and s (substitute), which unintuitively all act like the more common concept of "cut". These can be composed with motion commands to select different regions.

Any Vim command that deletes text will also save a copy of that text unless you specifically tell it not too. The idiomatic Vim method of deleting without copying is "_d, which probably sounds bizarre. We‘ll come back to that later, and you‘ll see that it makes sense in the bigger scheme of Vim‘s copy and paste system.

The basic Vim commands for paste are p (put) which pastes after of the current character/line and P which pastes before the current character/line. Vim auto detects whether you‘ve yanked text on a line by line basis or a character by character basis, and pastes accordingly. Basically, if you select only full lines it will paste line by line and not break on the cursor position, if you select any partial lines it will paste at the character level, starting or ending at the current cursor location. Note that p also works in visual mode, where it will replace the current selection.

All of this leads to a very common annoyance for new Vim users. They‘re editing a document and yank some text. Then they go to a new location, delete a few lines, and hit p to replace them with the copied text. Do you see the problem? The text that is "put" is not what they originally yanked, but instead it‘s the contents of their last delete action. Obviously this can be worked around by changing the order of the actions, but it‘s frustrating to users who are used to being able to easily delete without blowing away their paste action. Even more frustrating is when an experienced Vim user comes by and tells them the answer to their problem is to just type"0p instead. Which will in fact put the correct text. So what‘s going on with the weird syntax for these basic action?

Registers

What we‘re missing is a clear understanding of how Vim handles copy and paste operations. So let‘s clarify. Unlike most modern systems, which have a clipboard that holds a single value, Vim can store the values of yank and delete commands in one of many registers. It has 26 normal registers, which correspond to the letters of the alphabet, and several "special" registers. One of those special registers is the default register, which is where yank and delete commands store their content by default. Other registers can be accessed with the "<char> prefix that we‘ve already seen. So in the above example, the text from the initial example is moved into the default register, and then replaced there by the deleted text. But it remains in the 0 register, which always points to the last "yanked" text, ignoring text gained by deleting.

The 26 alphabetical registers serve as great "medium term" storage. You can use them to yank something that you want to have around for a while, and then put it in a few different places, even if you‘re yanking and deleting other things in the meantime. These will even persist across sessions as long as you have the nocompatible option set in your vimrc file. However if you overwrite one, there‘s no easy way to get it back.

One cool feature of registers is the built in ability to append to the end of them. If you want to add text to an existing register, for instance if you missed part of the text you wanted to yank, you can do so by capitalizing the register. So if you had deleted a line and put it in thea register with "add, but meant to include a second line, you could then delete the next line and append it to the register with "Add"ap would then put the 2 lines back into the document.

Special Registers

In addition to the alphabetical registers, there are several special registers that are worth knowing about. I already mentioned the default register, which most Vim users know about, even if they don‘t understand exactly how registers work. Other important registers are the clipboard register, the black hole register, and the numbered registers.

If you have to learn one special register, learn the clipboard register. One of the first things people notice about copy and paste in Vim is that it doesn‘t interact nicely with other applications‘ copy and paste by default. If you yank a line of text in Vim, it‘s not added to the system clipboard. If you copy some code from Stack Overflow and try to paste it in Vim with p, it won‘t work1. But, since we know about registers, it makes sense that the default register might not be mapped to the clipboard, which we don‘t want getting blasted away everytime we delete a character.

Vim isn‘t completely disconnected from the system clipboard though. The clipboard register + is Vim‘s proxy to your system clipboard. So "+y<motion> and "+p act like traditional copy and paste. Your version of Vim does have to be compiled with clipboard support in order to use the + register. You can check to see if you have clipboard support with :echo has(‘clipboard‘).2 On OSX you can use MacVim to get clipboard support, since the default version of Vim shipped with OSX is not compiled with it. On other operating systems you‘ll have to investigate the easiest way to install with clipboard support if your version doesn‘t have it. It should be enabled for most modern mainstream distributions.

Another register to quickly note is the black hole register_. The black hole register, as you would expect, doesn‘t retain what‘s passed to it. So "_y and "_p are no-ops, and "_d is "true delete", as opposed to the delete commands default "cut" like behavior. Of course since most people don‘t use all 26 alphabetical registers, you can also achieve effective true delete by deleting to any unused register.

Finally, the number keys also have special register functionality. You can‘t yank text directly to a numbered register. Instead the 0 register contains the last yanked text, and the 1 through 9 registers contain the last 9 deleted chunks of text. This feature is a cool idea, but unfortunately the implementation is inconsistent and a bit weird. For one thing the distinction between yanked and deleted text seems arbitrary, and since they act the same in other ways, puts an added cognitive load on the user to remember which one they used. Secondly, for whatever reason, deletions that span less than a line get special behavior. If you delete these to an alphabetical register, they‘re saved to "1 just like any other delete. But if you delete them to the default register, they‘re saved to "- and not put in "1. The logic is complicated enough that the numbered registers become tough to use in day to day tasks, and I‘ll have to agree with Drew Neil in labeling them one of Vim‘s"bad parts."

Macros

One last important thing to know about registers is that along with being used for copy and paste, they serve as a place to save macros, Vim‘s reusable command language. You can save a macro by typing q<register key><commands>q, and the macro will be saved to the register. Like yank and delete, capitalizing the register name will let you append to the register instead of replacing it. So there‘s a few things you should know about that. One, if you use macros, don‘t save macros to the same registers that you use for copy and paste. If you use the y register a lot for the convenience of "yy<motion>, don‘t use qy to save your macro unless you‘re ok with it being blown away by your next yank. Two, the sharing of registers allows you to copy text to use as a macro. So if you for instance wanted to have a file with a list of common operations, it would be easy to go to that file, copy a line, and then execute it as a macro. This isn‘t the first thing most people will want to do, but it illustrates the power and flexibility that come when you start combining Vim‘s tools.


More Resources

  • For a more in depth look at Vim‘s special registers, including a bunch I didn‘t cover here, you can check out this great roundup
  • Vimcasts has a whole series of posts on copy and paste in Vim.

Subscribe

This was the seventh entry in a series of posts on learning Vim in a modern way. If you enjoyed the post, please consider subscribing by using the feedTwitter or my mailing list. Also check out the next post in the series.

  1. Of course some people would probably see that as a feature, not a bug ?
  2. Thanks to schweinschmeisser on Reddit for reminding me to add a way to check for support. ?

orign url:http://benmccormick.org/2014/07/28/learning-vim-in-2014-copy-and-paste-the-vim-way/

vim 的寄存器,布布扣,bubuko.com

时间: 2024-10-13 06:07:24

vim 的寄存器的相关文章

vim的寄存器

有26个命名寄存器和1个无命名寄存器,常存放不同的剪贴版内容,可以不同会话间共享 寄存器名称a,b,-,z,格式:"寄存器 放在数字和命令之间 如:3"tyy 表示复制3行到t寄存器中 "tp 表示将t寄存器内容粘贴 未指定,将使用无命名寄存器 .有10个数字寄存器,用0,1,-,9表示,0存放最近复制内容,1存放最近删除内容.当新的文本变更和删除时,1转存到2,2转存到3,以此类推.数字寄存器不能在不同会话间共享 原文地址:https://www.cnblogs.com/l

VIM使用系列:寄存器与复制粘贴缓冲区

转自: http://www.2cto.com/os/201307/227903.html VIM使用系列:寄存器与复制粘贴缓冲区   现在已经可以熟练使用VIM的大多数基本命令.功能来进行项目代码的开发了,但是在项目的开发过程中,依然会感觉到一些操作效率比较低,比如通过h/j/k/l来进行光标的大范围移动这类操作,显然VIM提供了更高效的命令操作方式.最近经常需要完成的工作就是需要在代码之间来回的复制.粘贴.搜索和替换,常用的d/y/x/p命令已经显得不够,于是学习了一下VIM的寄存器功能,使

处理文本的工具sed,vim 编辑器的使用 &nbsp; Linux版

处理文本的工具sed Stream EDitor 行编辑器 sed一次处理一行的内容,处理时,将当前处理的行存储在临时缓冲区,称为"模式空间",接着用sed命令处理缓冲区中的内容,处理完成后,将缓冲区的内容送往屏幕,接着处理下一行不断重复,直到末尾. 一次处理一行 文件--->内存空间--->屏幕 模式空间 文件--->内存空间--->屏幕 | 保持空间 sed   -n:不输出模式空间内容的自动打印 -e:多点编辑 -f:/PATH/TO/SCRIPT_FIL

vim常用命令总结

vim常用命令总结 (转) 在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format.使用gg=G可对整篇代码进行排版.   vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v    从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V    从光标当前行开始,光标经过的行都会被选中

关于VIM编辑器

vim编辑器             vi 是一种文本编辑器,所谓文本数据是基于字符编码的文件,常见的编码有ASCII编码, UNICODE编码等 文本编辑种类分为:                行编辑器:所谓航编辑器就是至一行一行来编辑处理的工具,比如sed                全屏编辑器:编辑空间占据整个屏幕,比如 nano ,vi                 vim 就相当于vi的增强版,vim是模式化的编辑              vim 在工作过程中有三种模式:   

vim操作浅解

vim是一种比vi更加强大的模式化的全屏文本编辑器.vim在工作过程中有三种模式:编辑模式.插入模式.末行模式. 编辑模式(默认):又称命令模式,其工作内容包括移动光标.剪切.粘贴.删除等 输入模式:亦可称为插入模式,主要是在文件中修改文本内容 末行模式:亦称扩展命令模式,主要是执行vim内置命令的 vim三种模式之间的切换:如下图  (1)编辑模式 --> 输入模式 i: 在光标所在处输入 I:在当前光标所在行的行首 输入 a: 在光标所在处后面输入 A:在当前光标所在行的行尾 输入 o: 在

【vim小小记】vim的复制粘贴(包括系统剪贴板)

1.vim常用复制粘贴命令 Vim的复制粘贴命令无疑是y (yank),p(paster),加上yy,P PS: vim有个很有意思的约定(我觉得是一种约定),就是某个命令的大小写都是实现某种功能,只是方向不同,比如: w 跳转到下个word,W:跳转到上个word f 某行正向查找并跳转 F: 反向.... 然后部分双写的字母有行操作: yy 复制一行 dd 删除一行 所以, p就是在当前光标后粘贴,P就是在当前光标前粘贴 另外,说完p,其实还有几个命令有时也是很有用的 gp,和p的功能基本一

vim常用命令总结 (转)

  vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v    从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V    从光标当前行开始,光标经过的行都会被选中,再按一下V结束. Ctrl + v   从光标当前位置开始,选中光标起点和终点所构成的矩形区域,再按一下Ctrl + v结束. ggVG 选中全部的文本, 其中gg为跳到行首,V选中整行,G末尾 选中后就可以用编辑命令对其进行编辑,如 d   删除 y   复

0050 Linux VIM 命令

1.  模式切换 vim的模式 $ vi filename 进入normal 模式,这是命令模式,用于执行大多数常用的编辑命令,不能输入 敲i 进入 insert 模式,这是正常的编辑模式,按Esc 可以回到 normal 模式 在normal 模式下输入 : (冒号) 进入底行模式,也是执行命令的模式,用的最多的是查找 不管在 insert 模式还是底行模式,按 Esc 就能回到 normal 模式 insert 模式和底行模式是不能直接相互切换的,只能通过 normal 模式切换 2. 保存