从ag结果中删除相对目录的目录

时间:2016-10-21 08:59:35

标签: vim ag

如果使用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中进行项目范围搜索:找到项目路径并将其传递给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()

1 个答案:

答案 0 :(得分:2)

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

当我的cd .包含:make

之类的内容时,我经常在&makeprg之后(cd build/path && make target) {{1}}