dhruvasagar / vim-prosession

Handle vim sessions like a pro
254 stars 22 forks source link

Store the "alternative file" in the session #50

Closed Dbz closed 6 years ago

Dbz commented 6 years ago

Hello,

I'd like to store the alternative file in the session so that I can close vim, open it back up and type <c-^> and automatically switch to the previous buffer that I was using.

I found a vi stack exchange answer which can do 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>)

However, adding this to my vimrc doesn't solve the problem because vim-prosession isn't calling the new MkSession instead of mksession.

Is there a way for me to add something to my vimrc to make this work, or would it be possible to add an option to vim-prosession to save the alternative file so that it's available when opening up the session?

Perhaps I could change autocmd VimLeave * exec 'mksession!' g:prosession_dir . 'last_session.vim' to be autocmd VimLeave * exec 'MkSession' g:prosession_dir . 'last_session.vim' on my local copy? Alas, this didn't work for me, so there is probably more to it :(

dhruvasagar commented 6 years ago

I am looking into this.

Dbz commented 6 years ago

That is awesome, thank you! I appreciate the effort 🎉

dhruvasagar commented 6 years ago

The following works :

function! s:preserve_alt_buffer()
  let l:alternate = fnameescape(expand('#'))
  if !empty(l:alternate)
    call system('echo "let @#=\"'.l:alternate.'\"" >> '.v:this_session)
  endif
endfunction

augroup ProsessionPreserveAlt
  au!

  autocmd VimLeave * call s:preserve_alt_buffer()
augroup END

I don't think there are any side effects to doing this, but let me know if you face any issues.

NOTE : To give you some context, the actual session persisting logic is within tpope/vim-obsession. As of now I don't see a better API to achieve this other than just add your logic within VimLeave (not VimLeavePre) autocmd. But it works.

Dbz commented 6 years ago

This works! Thank you! Really, this is awesome. I'll make an issue on vim-obsession and see if tpope is willing to accept this added functionality.