0

如果像 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()
4

1 回答 1

2

如果ag在 quickfix 窗口中产生结果,您可以:cd {path}在 quickfix 窗口中更新显示的路径。您可能必须:cclose:copenqf 窗口才能看到简化操作。

当我包含类似的东西时,我经常做一个cd .之后:make&makeprg(cd build/path && make target)

于 2016-10-21T09:06:45.163 回答