Search code examples
unixvimvim-plugin

How to always have the same current directory in VIm and in Terminal?


I would like to my terminal current directory follows my VIM one.

Example:

In TERMINAL:

> pwd
=> /Users/rege
> vim

Then in VIM

:cd /Users/rege/project
<Ctrl-z>(for suspend)

In terminal

> pwd
=> /Users/rege/project

I`m using MacOS, zsh, tmux.

I need this because when Im trying to use tags in VIM, tags are check in project from my terminal directory not vim one.

So I need to change terminal current directory always when I change VIM current directory.


Solution

  • What kind of command do you issue in your shell after you suspend Vim? Isn't Vim's :!command enough?

    With set autochdir, Vim's current directory follows you as you jump from file to file. With this setting, a simple :!ctags -R . will always create a tags file in the directory of the current file.

    Another useful setting is set tags=./tags,tags;$HOME which tells Vim to look for a tags file in the directory of the current file, then in the "current directory" and up and up until it reaches your ~/. You might modify the endpoint to suit your needs. This allows you to use a tags at the root of your project while editing any file belonging to the project.

    So, basically, you can go a long way without leaving Vim at all.

    If you really need to go back to the shell to issue your commands, :shell (or :sh) launchs a new shell with Vim's current directory. When you are done, you only have to $ exit to go back to Vim:

    $ pwd
    /home/romainl
    $ vim
    :cd Projects
    :sh
    $ pwd
    /home/romainl/Projects
    $ exit