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

How do I change the working directory for only the current window without affecting other windows or tabs?

Answer

:lcd

Explanation

:cd changes the global working directory, affecting every window and tab in the session. :lcd {path} does the same thing but scoped to the current window only. This lets you have different windows rooted at different project directories simultaneously — handy when editing files from two separate repositories at once.

How it works

  • :lcd {path} sets a window-local CWD; the global CWD is unchanged
  • :lcd - navigates to the previous window-local CWD (like cd - in a shell)
  • :pwd shows the effective CWD for the current window
  • If no local CWD is set, the window inherits the global CWD
  • :tcd {path} is the tab-scoped equivalent — applies to all windows in a tab but not others

Example

Open two vertical splits and give each its own project root:

:vsplit
:lcd ~/projects/frontend
:wincmd h
:lcd ~/projects/backend

Now :e tab-completion, gf, :find, and shell commands via :! all respect the per-window directory.

Tips

  • Use :lcd %:h to switch to the directory of the current file — useful when opening a file and immediately wanting to navigate relative to it
  • :cd (no args) returns to $HOME; :lcd (no args) in some versions removes the local override
  • :tcd is useful when a tab represents a logical project and you want all its splits to share the same root

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?