VIM - 拉出尴尬文本块的最快方法

发布于 2024-10-15 22:51:23 字数 258 浏览 12 评论 0原文

我正在使用 VIM 7.1.314,并且想尽快提取下面所示代码中的名称 (chris, robert, ben) - 我该如何实现这一点?请注意,名称始终对齐(无论用户数量多少)。

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

另请注意,我正在寻找一个有数百个名称的解决方案,因此可扩展性很重要。

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

倦话 2024-10-22 22:51:23

一个简单的方法就是求助于外部程序来提供帮助。例如:

:%!awk '{ print $3 }'

这会将整个编辑缓冲区替换为对当前缓冲区的内容运行 awk 命令的结果。然后,您可以将结果复制到另一个文件或其他文件中,u 可以轻松地将原始缓冲区恢复到之前的状态。

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.

坏尐絯 2024-10-22 22:51:23

鉴于您的名称不会超过 16 个字符,请使用正则表达式搜索和替换在每个名称后添加 16 个空格,然后使用块视觉模式进行复制。首先,执行(在命令行上):

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

该空白区域为 16 个空格。然后,转到第一个名称的开头并按 Control-V,然后向右移动 15 个字符并到达名称列表的最后一行,然后按 Y复制,然后复制到目标缓冲区并按 P 进行粘贴。

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.

葮薆情 2024-10-22 22:51:23

您可以使用名为 Tabularize 的插件,您可以从 https://github.com/godlygeek/tabular 获取该插件。在您列出的情况下,您可以这样做

 :Tabularize /(.*

,它会将您的文本文件更改为如下所示:

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

然后您可以简单地使用视觉块来提取文本。这是一个很棒的插件,可以节省大量时间。

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.

左岸枫 2024-10-22 22:51:23

这是一个纯 viml 解决方案,保持原始缓冲区不变:

:let names= []
:g/^user \d/let names+= matchstr(getline('.'), 'user \d\+:\s*\zs\S\+')
:new
:put=names

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
情仇皆在手 2024-10-22 22:51:23

我会使用宏。

  1. qeq(清除“e”寄存器)
  2. qa03w"Eywjq(注册提取名称的宏)
  3. 200000@a(重复宏)很多时间)

然后你的名字就在“e”寄存器中输入“ep”来查看它们!

注意:

  • 也许yw不正确,也许是yt(或其他东西,具体取决于名称。
  • :setlazyredraw可以帮助提高宏性能!

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 和您的相关数据。
原文