vimtricks.wiki Concise Vim tricks, one at a time.

How do I switch back to the previous working directory in Vim?

Answer

:cd -

Explanation

The :cd - command switches Vim's global working directory back to the previous one, just like cd - in the shell. This is useful when toggling between two project roots or directories without retyping paths.

How it works

  • :cd {path} changes the global working directory for all windows and tabs
  • :cd - is the shorthand for "return to the directory I was in before the last :cd"
  • Vim stores one level of directory history, so the toggle works reliably between two directories

Example

:pwd             " /home/user/project-a
:cd /home/user/project-b
:pwd             " /home/user/project-b
:cd -
:pwd             " /home/user/project-a

Tips

  • Use :lcd - to toggle the previous directory for only the current window without affecting other windows
  • :pwd shows the current working directory at any time
  • Vim only tracks one level of history with :cd -, unlike a full directory stack
  • If you opened Vim from a different directory than your project root, :cd - can quickly return to where you launched from

Next

How do I duplicate every line matching a pattern, placing a copy directly below each one?