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

How do I set each window's local working directory to its buffer path?

Answer

:windo lcd %:p:h<CR>

Explanation

When multiple splits point at files from different projects, relative-path commands can become inconsistent and error-prone. This command applies a window-local directory change to every open window, using each buffer's own file path as the source. It is a strong intermediate-to-advanced workflow for polyrepo sessions, because each split keeps context without forcing one global working directory.

How it works

  • :windo executes the following Ex command in every window
  • lcd sets the local current directory for that window only
  • % expands to the current buffer's filename
  • :p turns it into a full absolute path
  • :h strips the filename, leaving the parent directory

So each window gets lcd set to its own buffer directory. Commands like :make, :read, or relative file opens then resolve per-window, not globally.

Example

Suppose you have two splits:

left:  ~/work/api/src/server.go
right: ~/work/web/src/App.tsx

Run:

:windo lcd %:p:h

Now the left window resolves relative paths under ~/work/api/src, while the right window resolves under ~/work/web/src.

Tips

  • Use :pwd inside each window to confirm the local directory took effect.
  • Prefer lcd here instead of cd; cd would collapse all windows to one shared directory and remove the per-window isolation.

Next

How do I enable matchit so % jumps between if/else/end style pairs?