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

How do I set a different working directory for each split window in Vim?

Answer

:lcd {dir}

Explanation

:lcd (local cd) sets the working directory for the current window only, leaving other windows at their previous directory. This is the window-level counterpart to :cd (global) and :tcd (tab-local), giving you fine-grained control when editing files from multiple projects simultaneously.

How it works

  • :lcd {dir} — change the working directory for the current window
  • :lcd %:h — set the window's cwd to the directory of the current file (%:h expands to file's parent)
  • :lcd - — switch back to the previous window-local directory
  • :pwd — shows the effective working directory (local if set, else global)

After :lcd, relative paths in :edit, gf, :terminal, shell expansions, and expand('%:h') all resolve against the local directory.

Example

You have two vertical splits — a backend Go file and a frontend JS file in separate repos:

" In the left window (Go project)
:lcd ~/projects/backend

" Switch to right window
<C-w>l

" In the right window (JS project)
:lcd ~/projects/frontend

" Now :e src/App.js opens relative to frontend/
" while :e cmd/main.go in the left window opens relative to backend/

Tips

  • Check all effective directories with :windo pwd
  • :tcd {dir} sets a tab-local directory (shared by all windows in that tab)
  • If a window has an :lcd set, :cd (global) still changes the global cwd but the window's local directory takes precedence
  • Useful in autocmd BufEnter * lcd %:p:h to auto-follow the file's directory (though this can be disruptive — scope it to file types you want)

Next

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