简体   繁体   中英

How to set the root of git repository to vi/vim find path?

I want to set vim file search path to include git repository root (which can be found by git rev-parse --show-toplevel ). I can't figure out how to append the output of this git command to " set path=.,,** " in .vimrc.

Thanks!

You can use this command:

let &path .= "," . system("git rev-parse --show-toplevel | tr -d '\\n'")

That said, I usually start Vim from the top-level directory of the project and never change the working directory so that's one less setting to worry about.

See :help system() and :help :let

" Add the git dir only once, and check for errors.

function! MoshGitPath()
  let g:gitdir=substitute(system("git rev-parse --show-toplevel 2>&1 | grep -v fatal:"),'\n','','g')
  if  g:gitdir != '' && isdirectory(g:gitdir) && index(split(&path, ","),g:gitdir) < 0
    exe "set path+=".g:gitdir."/*"
  endif
endfunction
command! MoshGitPath :call MoshGitPath()
:MoshGitPath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM