如果像 ag 这样在长路径目录中搜索a/long/long/long/path/to/project/
,结果列表将包含长路径。有没有办法在结果列表中获取项目路径?
示例结果列表ag test a/long/long/long/path/to/project/
:
a/long/long/long/path/to/project/test.txt: test.....
a/long/long/long/path/to/project/js/test2.js: test: function() {
预期:
test.txt: test.....
js/test2.js: test: function() {
为什么我使用指定的目录路径搜索而不是cd
搜索?我以这种方式在 vim 中使用 ag 进行项目范围搜索:找到项目路径并将其传递给 vim 命令,该命令将用于使用ag
给定路径进行搜索。
解决方案
ps:我用Plug 'mhinz/vim-grepper
function! VimGrepperConfig()
function! s:ag_at_project_root(keyword)
let root = s:find_root()
if !empty(root)
execute 'cd' root
execute 'GrepperAg' a:keyword
execute 'cd -'
else
execute 'GrepperAg' a:keyword
endif
endfunction
command! -nargs=1 AgAtProjectRoot call s:ag_at_project_root('<args>')
" ag search
no <Leader>/ :AgAtProjectRoot
endfunction
call VimGrepperConfig()