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
:windoexecutes the following Ex command in every windowlcdsets the local current directory for that window only%expands to the current buffer's filename:pturns it into a full absolute path:hstrips 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
:pwdinside each window to confirm the local directory took effect. - Prefer
lcdhere instead ofcd;cdwould collapse all windows to one shared directory and remove the per-window isolation.