While using :mksession feature, I found that it works well with files and netrw, but not so much with neovim terminal windows. The problem is that, for terminal, it never preserves working directory inside the shell, even if I :lcd before saving the session. It does restore working directory inside vim itself, but the shell directory is always the one that was specified when terminal was created.

I'm wondering if there's a way to work around this. I'm thinking about something like calling extra function after session restore to run cd getcwd() in every terminal window, but I'm not sure how to do it correctly.

1 Answer 1


I've managed to write something that achieves desired effect.

function! Terminal_cd()
        if &buftype == 'terminal'
                call chansend(b:terminal_job_id, 'NVIM= cd "' . getcwd() . "\"\<cr>")

function! Terminal_restore()
        let curtab = tabpagenr()
        let curwin = winnr()
        tabdo windo call Terminal_cd()
        exec curtab . 'tabn'
        exec curwin . 'wincmd w'

I call Terminal_restore() after sourcing the session file. Since I already have vim automatically change working directory along with the shell cd, this seems to be good enough. Unsetting NVIM during restore is done because it is used to detect nvim session and calls :lcd automatically from shell function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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