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

限于喜欢 提交于 2019-12-23 18:09:01

问题


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!


回答1:


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




回答2:


" 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


来源:https://stackoverflow.com/questions/30171512/how-to-set-the-root-of-git-repository-to-vi-vim-find-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!