0

If search in a long path directory like a/long/long/long/path/to/project/ with ag, the results list will contain the long path. Is there a way to get ride of project path in result list?

Example results list of 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() {

what expected:

test.txt: test.....
js/test2.js: test: function() {

Why I search with specfied directory path instead of cd and search? I do the project wide search in vim with ag this way: find the project path and pass it to vim command which will use ag to search with given path.

Solution

Ps: I use 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()
tjfdfs
  • 729
  • 10
  • 26

1 Answers1

2

If ag produces its result in the quickfix window, you can do a :cd {path} in the quickfix window to update the paths presented. You may have to :cclose and :copen the qf window to see the simplification operates.

I often do a cd . after :make when my &makeprg contains things like (cd build/path && make target)

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • but my solution invite a new issue. I switch back to last `pwd` after search, now if I open file from quickfix list, it will create a new file at current `pwd` instead of open the file in project. Ah... – tjfdfs Oct 22 '16 at 08:41