2016-10-21 111 views
0

如果使用ag搜索類似a/long/long/long/path/to/project/的長路徑目錄,結果列表將包含長路徑。有沒有辦法在結果列表中獲取項目路徑?從ag結果中刪除相對目錄下的目錄

例結果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() { 

爲什麼我specfied目錄路徑,而不是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() 

回答

2

如果ag產生其結果在quickfix窗口中,可以在quickfix窗口做一個:cd {path}更新提出的路徑。您可能需要:cclose:copen qf窗口才能看到簡化操作。

我經常做一個cd .:make當我&makeprg包含之類的東西(cd build/path && make target)

+0

但我的解決辦法邀請一個新的問題。我搜索後切換回最後一個'pwd',現在如果我從quickfix列表中打開文件,它將在當前'pwd'創建一個新文件,而不是打開項目中的文件。啊... – tjfdfs