Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim cd'ing to git root

Tags:

git

vim

I'm trying to cd into my project's git root from vim so that my Ctrl-P works properly. I often work in project's where I don't want Ctrl-P to look up to the git root so I have it set up to only look for the nearest folder with a .gitignore.

On the command line I can do something like cd $(git rev-parse --show-toplevel) but when I try the same thing in vim with :!cd $(git rev-parse --show-toplevel) it hides vim and just shows a blank output. I was told the ! in the vim command line would allow me to run bash commands. Am I missing something.

like image 632
adam-beck Avatar asked Dec 25 '22 04:12

adam-beck


1 Answers

You can use backticks to run shell commands and pipe the output into Vim commands like :cd:

:cd %:h | cd `git rev-parse --show-toplevel`

This sets the current directory to the directory of the current file and then runs :cd with the git directory from the git rev-parse --show-toplevel command.

Alternatively, if you use fugitive.vim you can use the :Gcd command.

For more help see:

:h :cd
:h backtick-expansion
:h :bar
like image 195
Peter Rincker Avatar answered Jan 25 '23 23:01

Peter Rincker