Vim 搜索类
如何定义 vim 函数,以便在调用
Foo
它时通过 vimgrep 搜索
\s*class Foo
or
\s*struct Foo
?
[这是穷人的cscope/ctag;我希望能够输入类名,并让它搜索该类。]
如果这很容易,有没有办法告诉它在我的光标下查看以使用该“单词”作为搜索名称为了?
谢谢!
How do I define a vim function such that when called with
Foo
it searches via vimgrep for
\s*class Foo
or
\s*struct Foo
?
[This is poorman's cscope/ctag; I want to be able to type in a class name, and have it search for the class.]
If this is easy, is there a way I can tell it to look under my cursor to use that 'word' as the name to search for?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是来自 vim 新手的一个 hack,它似乎有效:
当您输入 \fi 时,它应该搜索光标下的单词。
解释一下代码:
如果你在函数中正常调用 vimgrep 并且它没有找到任何结果,它会抛出一个看起来相当难看的错误,因此我将它包装在 try/catch/endtry 块中。当发生错误时,我们认为是因为没有匹配项,然后显示一条消息,然后短暂暂停,这样它就不会立即消失。
“nmap”在“正常模式”下映射按键序列来执行某些操作。在本例中,它调用我们刚刚定义的函数。您必须在行尾键入
Ctrl-V、Ctrl-M
才能创建 ^M,因此它会模拟您按 Return 键。如果您想更改函数以接受任何参数,您可以像这样更改它:
然后您可以通过键入来调用它
但使用内置的 似乎更容易特征。
Here's a hack from a vim novice which seems to work:
This should search for the word under the cursor when you type \fi.
Explicating the code:
If you call vimgrep normally in a function and it doesn't find any results, it throws an error which looks fairly ugly, hence I wrapped it inside of a try/catch/endtry block. When an error happens, we presume it's because there was no match, and we display a message, then pause briefly so it doesn't immediately disappear.
"nmap" maps a key sequence in "normal mode" to do something. In this case, it calls the function we just defined. You have to type
Ctrl-V, Ctrl-M
at the end of the line to create the ^M, so it simulates you pressing return.If you'd like to change the function to take any argument you could change it like this:
Then you can call it by typing
But it would seem easier to just use the built-in <cword> feature.
检查
:h Expand()
函数中的
。Check
<cword>
in:h expand()
function.