6

When saving a session with :mksession and sourceing it back, I noticed that alternate files (accessed via C-^) are not persisted.

The docs don't mention anything about persisting alternate files. Is there a way/workaround/hack to do so?

2
  • 3
    They are persisted as long as your sessionoptions contains buffers, but they are not marked as alternate. Perhaps this is a bug. Commented Jun 15, 2016 at 17:23
  • 1
    Question asked on the vim mailing list, waiting for an answer :)
    – nobe4
    Commented Jun 15, 2016 at 17:48

2 Answers 2

2

Since there is no option to do this directly, you can hack around like this:

function! MkSession(filename)
  execute 'mksession! '.a:filename

  let l:alternate = fnameescape(expand('#'))

  if l:alternate != ''
    call system('echo "let @#=\"'.l:alternate.'\"" >> '.a:filename)
  endif

endfunction

command! -nargs=1 MkSession call MkSession(<f-args>)

This function will add the alternate file setting in the session, so when the session is loaded up, the alternate file is restored. (thanks to this answer)

You should be able to use it as is although I'm not sure it's 100% stable:

:MkSession session.vim
3
  • I'm interested in saving alternate files for all visible buffers in all tabs, not just the current buffer. Thanks, I'll play with that when I get a minute. Commented Jun 16, 2016 at 17:08
  • You can hook up any function here, so you can go through all your tabs/windows and use the same logic. I'm not sure though how to store this... I recommend looking at the session file, manipulating directly and loading. I did that before trying to automate the process.
    – nobe4
    Commented Jun 16, 2016 at 17:10
  • Yep! I tried to adapt the code to make it remember alternate buffers for every window of every tab, this felt too tedious and too hacky. Especially since my solution involved visually focusing all of these buffers, which would make Vim flicker (otherwise the only other solution is to parse the session file and inject lines of Vimscript at specific positions). I eventually switched to a more dedicated advanced session management tool (tmux). Commented Jun 21, 2016 at 22:12
0

Instead of shoehorning fully-fledged session management into Vim with hacky Vimscript code, I eventually decided to use an external session management tool dedicated for this task.

I chose tmux (which also does a lot of other stuff that one would not necessarily care about) and am happy with it. Not only does it save the state of my Vim, but also the state of all my other console tabs/windows.

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.