I have a neovim session saved using :mks which by default saves to a subdirectory in my project ~/project/subdir/Session.vim. I want it to save to ~/project/Session.vim, which I can do by running :cd to change directories and then running :mks, but every time I load up the session, it changes the working directory back to ~/project/subdir. How can I get it to save the changed working directory?


The behavior of :mksession is driven by the flags you have set in 'sessionoptions' (alias 'ssop'). The two flags that are likeliest to be factors here are curdir and sesdir. The former is simple, the current directory will be saved in the session data. The entry for sesdir in Help says:

 sesdir     the directory in which the session file is located
            will become the current directory

The two are not compatible (you shouldn't have them both set). curdir is set by default but your explanation suggests that you have sesdir enabled. Check your current configuration (set ssop?).

If you need to remove a default flag use set ssop-=curdir (for example) in your vimrc file.

Update: Though OP has things working as needed and has accepted my answer, they still are describing behavior that doesn't jibe with how I believe Vim actually works. So I want to add some additional details with a more specific solution suggestion in case OP needs to revisit this.

My recommendation is to use sesdir and then you'll see the behavior described below.

When you do :mks /path/to/sess.vim the working dir doesn't matter as you've specified a path. When you later do vim -S /path/to/sess.vim then /path/to will be the working dir. That's it.

What if you don't specify a path? Then sess.vim will be saved to the working dir of the window from which you issue the :mks command. It doesn't matter how or when the working dir was configured. It can be the "default" (whatever was set when you started Vim). Or it can be a dir you specified with either :cd or :lcd. Regardless, when you later start Vim with -S /some/path/sess.vim then, thanks to sesdir, the working dir will be /some/path.

I have verified that all of the above is accurate.

  • Removed curdir and added sesdir in my init.vim file. This didn't fix the problem. But then I also discovered the :lcd command. If I change directory with that instead of :cd, then it saves with the session. I'm not sure if changing ssop contributed to this or not, but either way, I managed to solve the problem. Thanks!
    – howardh
    Jun 20 '19 at 17:49
  • @howardh Glad you found a resolution though what you're saying sounds strange. :lcd doesn't get treated specially. Though you've accepted my answer (thanks!) I've added some important new details to it...in case your current solution causes problems in the future.
    – B Layer
    Jun 21 '19 at 5:57
  • Ah, yes, you're right. This didn't actually fix the problem. It just appeared like it did because when I change the window's working directory, it gets saved in the session file. But it only affects that one window. So if I'm reading this correctly, there's no way to make :mks always save to the same directory when no destination is specified, is that right?
    – howardh
    Jun 22 '19 at 3:42
  • Not natively. But it would be trivial to create a command that "wraps", let's say, a :lcd/:cd yourdir call and an :mks call into one action. If you do give an argument it can jjust call :mks. Then start using that instead of :mks. Don't know if that would work for you.
    – B Layer
    Jun 22 '19 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.