如何定义一个vim函数,使其在调用时使用以下命令:
Foo
它通过vimgrep搜索
\s*class Foo
或者
\s*struct Foo
这是一个简易版的cscope/ctag工具,我希望能够输入类名并搜索该类。
如果这很容易实现,那么是否有一种方法可以让它查找光标下的单词作为要搜索的名称?
谢谢!
以下是一位vim新手的技巧,似乎可以起作用:
function! SearchFunc()
let l:filenames = substitute(glob("*.c") . glob("*.cpp") . glob("*.h"), '\n', ' ', 'g')
try
execute 'vimgrep /^\s*\(struct\|class\)\s*' . expand("<cword>") . '/ ' . l:filenames
catch
echon 'No results found.'
sleep 800m
endtry
endfunction
nmap <Leader>fi :call SearchFunc()^M
function! SearchFunc(findme)
let l:filenames = substitute(glob("*.c") . glob("*.cpp") . glob("*.h"), '\n', ' ', 'g')
try
execute 'vimgrep /^\s*\(struct\|class\)\s*' . a:findme . '/ ' . l:filenames
catch
echon 'No results found.'
sleep 800m
endtry
endfunction
:call SearchFunc('foo')
但是使用内置的<cword>功能似乎更加容易。
在:h expand()
函数中检查<cword>
。
fun! MySearch()
exe 'vimgrep /\s*class '.expand('<cword>').'/ *'
endfun