VIM - Fastest way to yank an awkward text block
I am using VIM 7.1.314 and would like to yank the names (chris, robert, ben)
in the code shown below as fast as possible - how would I achieve this? Note the names are always aligned (regardless of users number).
user 1: chris (05/04/1984)
user 2: robert (11/12/1991)
user 3: ben (5/25/1993)
Also note, I'm looking for a solution where there is hundreds of names so scalability is important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
An easy way to do this is to shell out to an external program to help. For example:
That will replace your entire edit buffer with the results of running that
awk
command on the contents of the current buffer. You can then copy the result to another file or whatever, andu
easily gets your original buffer back to its previous state.Given that your names are not going to be longer than 16 characters, use a regex search and replace to add 16 spaces after each name, then use block visual mode to do the copy. First, do (on the command line):
That blank area is 16 spaces. Then, go to the start of the first name and press
Control-V
, then go 15 characters to the right and to the last line of the list of names and pressY
to copy, then to your destination buffer and pressP
to paste.You can use a plugin called Tabularize which you can get from https://github.com/godlygeek/tabular. In the case you listed you could do
and it will change your text file to look like this:
Then you can simply use visual block to pull the text. It's a great plugin that saves an incredible amount of time.
Here is a pure viml solution that leaves the original buffer unchanged:
I would use macros.
qeq
(to clear the "e" register)qa03w"Eywjq
(to register a macro yanking the name)200000@a
(repeat the macro a lots of time)And then your names are in the "e" register. Type "ep to view them !
Notes :
yw
is not correct, maybe a yt( or something, depending on names.:set lazyredraw
can help with macro performance !