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

How do I stop Vim sessions from changing my working directory when restored?

Answer

:set sessionoptions-=curdir

Explanation

When you restore a session with :source Session.vim, Vim can also restore the working directory that was active when the session was saved. That behavior is controlled by the curdir flag inside 'sessionoptions'. Removing it keeps session restore focused on buffers, windows, and tabs while preserving your current directory strategy.

This is especially useful if you rely on project-local tooling, wrapper scripts, or shell workflows that set the working directory before opening Vim. Without this tweak, session restore can silently move you into an older path and make relative commands run in the wrong place.

How it works

:set sessionoptions-=curdir
  • 'sessionoptions' defines what :mksession writes into the session file
  • -=curdir removes the working-directory item from that list
  • Future session files omit directory restore commands, so :source won't override your current cwd

Example

Before:

Current shell cwd: /work/app
Saved session cwd: /work/old-app
:source Session.vim -> cwd becomes /work/old-app

After :set sessionoptions-=curdir:

Current shell cwd: /work/app
Saved session cwd: /work/old-app
:source Session.vim -> cwd stays /work/app

Tips

  • Add this to your vimrc/init.vim so it applies consistently
  • Run :set sessionoptions? to inspect your final value
  • If you need tab-local or window-local directories, use :tcd or :lcd explicitly instead of session side effects

Next

How do I list only search-pattern history entries in Vim?