VIM - Fastest way to yank an awkward text block

发布于 2022-09-06 08:35:25 字数 446 浏览 20 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

·深蓝 2022-09-13 08:35:25

An easy way to do this is to shell out to an external program to help. For example:

:%!awk '{ print $3 }'

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, and u easily gets your original buffer back to its previous state.

ヅ她的身影、若隐若现 2022-09-13 08:35:25

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):

:%s/: [^ ]*/&                /

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 press Y to copy, then to your destination buffer and press P to paste.

冷月断魂刀 2022-09-13 08:35:25

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

 :Tabularize /(.*

and it will change your text file to look like this:

user 1: chris  (05/04/1984)
user 2: robert (11/12/1991)
user 3: ben    (5/25/1993)

Then you can simply use visual block to pull the text. It's a great plugin that saves an incredible amount of time.

标点 2022-09-13 08:35:25

Here is a pure viml solution that leaves the original buffer unchanged:

:let names= []
:g/^user \d/let names+= matchstr(getline('.'), 'user \d\+:\s*\zs\S\+')
:new
:put=names
作死小能手 2022-09-13 08:35:25

I would use macros.

  1. qeq (to clear the "e" register)
  2. qa03w"Eywjq (to register a macro yanking the name)
  3. 200000@a (repeat the macro a lots of time)

And then your names are in the "e" register. Type "ep to view them !

Notes :

  • maybe the yw is not correct, maybe a yt( or something, depending on names.
  • :set lazyredraw can help with macro performance !
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文