按列迭代搜索结果
Vim(或插件)中是否有一种方法可以按列而不是按行搜索术语并迭代搜索结果(按照 Vim 中的 n
)? 因此,如果我的文件是这样的:
foo1 bar bar
baz baz foo3
baz baz foo4
foo2 bar bar
如果我搜索 foo
我想按 1、2、3、4 的顺序迭代结果。 通常 n
会让我按 1、3、4、2 的顺序浏览它们。
例如,我希望能够浏览一个巨大的固定宽度数据文件或 CSV 文件中的搜索结果,其中列代表字段。
(我也会选择一种在 Emacs 中完成此操作的方法。:)
Is there a way in Vim (or a plugin) to search for a term and iterate through the search results (as per n
in Vim), by column, rather than row? So if my file was this:
foo1 bar bar
baz baz foo3
baz baz foo4
foo2 bar bar
If I search for foo
I want to iterate through the results in order 1,2,3,4. Normally n
would move me through them in order 1,3,4,2.
I want this for example to browse through search results in a huge fixed-width data file or CSV file, where columns represent fields.
(I'll also settle for a way to do it in Emacs instead. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看到这个问题后我决定尝试编写一个 vim 插件来处理它。 这是我的第一个 vim 插件,所以它可能非常糟糕。 但是,它确实适用于您给出的示例。
您可以从 vim.org 下载该插件: SearchCols.vim
它还需要来自同一站点的 multvals 插件: multvals.vim
它创建一个命令
:SearchCols
,可用于在固定宽度列中搜索
,以便搜索在首先是第一列,然后是第二列,依此类推,而不是 vim 标准的逐行搜索。 通过@:
重复该命令,然后通过@@
重复该命令,因为它是冒号命令。假设:
我能想到的明显改进是使搜索下一个项目比使用
@:
和@@
更容易,包括搜索突出显示(代码在那里,但已注释)因为它使用:match
并且您必须使用:match none
将其关闭),并消除对 multvals.vim 的依赖。After seeing this question I decided to try and write a vim plugin to handle it. It's my first vim plugin, so it's probably very bad. However, it does work for the example you gave.
You can download the plugin from vim.org: SearchCols.vim
Note that it also requires the multvals plugin from the same site: multvals.vim
It creates a command
:SearchCols <string>
, which can be used to search for the<string>
in fixed-width columns so that the search looks in the first column first, then the second column, etc. rather than the row-by-row search which is standard for vim. Repeat the command via@:
and then@@
since it's a colon-command.Assumptions:
Obvious improvements that I can think of would be to make searching for the next item easier than using
@:
and@@
, including search highlighting (the code is there but it's commented out because it uses:match
and you would have to use:match none
to turn it off), and eliminating the dependency on multvals.vim.编辑:我刚刚在此处找到了对表使用 vim 正则表达式的概述。 这比我的业余尝试干净得多(尽管这也有效;)):
搜索
^foo
将搜索第一列,而搜索.*\ foo
将搜索第二列,.*\ .*\ foo
将搜索第三列。当然这并不完整,因为它从行的开头匹配整个块,所以它会找到 foo3 和 foo4 但将光标放在位置 0。
但是,您可以使用记录功能自动执行此操作。 在正常模式下准确输入以下内容:(
我使用新行来指示您应该按回车键)。
这会将两次搜索放在 a 寄存器下(qa 在 a 下开始记录,qb 在 b 下开始记录,等等)。 现在点击
@a
,你应该只搜索第三列......我相信你可以将其变成一个带有列号的命令,但我现在离开了,抱歉:)
EDIT: I just found an overview of using vim regular expressions for tables here. That's a lot cleaner than my amateur attempt (although this works too ;)):
searching for
^foo
will search the first column, while searching for.*\ foo
will search the second column, and.*\ .*\ foo
will search the third.This is not complete of course since it matches a whole block from the start of the line, so it will find foo3 and foo4 but puts the cursor at position 0.
You can however automate this with the recording function. Type exactly the following in normal mode:
(where I have used new lines to indicate you should hit the return key).
This puts two searches under the a register (qa starts recording under a, qb under b, etc). Now hit
@a
and you should search only the third column....I'm sure you can turn this into a command that takes a column number, but I'm off now, sorry :)