Back

How to save vim sessions

Introduction

I was doing some research on how to save sessions on my laptop.

I used to be able to just use the hibernation feature of my previous laptop to continue my tty and graphical sessions, but unfortunately it’s not very reliable.

In my experience, you really have to have specific GPUs, CPUs and such for it to be stable enough to be an option.

I was looking into tmux which could have had some sort of session save feature and I just couldn’t find one.

So what else do we have?

A Solution

Then I found an article that explained how to save vim tab sessions.

It’s actually just a matter of adding script entries to your ~/.vimrc file.

The How

Do note that the code is not from me, I made some minor changes, but I took it in some article.

Here’s the link to the article : [Link missing in plain sight. The link was here, someone must have stolen it! Police!] (Yeah sorry, just can’t find it)

here’s the code which has to be appended to your ~/.vimrc :

" START Session saving for vim

" Creates a session
function! MakeSession()
  let b:sessiondir = $HOME . "/.vim_sessions" . getcwd()
  if (filewritable(b:sessiondir) != 2)
    exe 'silent !mkdir -p ' b:sessiondir
    redraw!
  endif
  let b:filename = b:sessiondir . '/session.vim'
  exe "mksession! " . b:filename
endfunction

" Updates a session, BUT ONLY IF IT ALREADY EXISTS
function! UpdateSession()
  if exists("g:sessionSaved")
      let b:sessiondir = $HOME . "/.vim_sessions" . getcwd()
      let b:sessionfile = b:sessiondir . "/session.vim"
      if (filereadable(b:sessionfile))
        let b:filename = b:sessiondir . '/session.vim'
        exe "mksession! " . b:filename
      endif
  endif
endfunction

" Loads a session if it exists
function! LoadSession()
  if argc() == 0
      let b:sessiondir = $HOME . "/.vim_sessions" . getcwd()
      let b:sessionfile = b:sessiondir . "/session.vim"
      if (filereadable(b:sessionfile))
        let g:sessionSaved=1
        exe 'source ' b:sessionfile
      else
        echo "No session loaded."
      endif
  endif
endfunction

au VimEnter * nested :call LoadSession()
au VimLeave * :call UpdateSession()
" Save a session by doing : \m
map <leader>m :call MakeSession()<CR>

" END Session saving for vim

Using The Vim Session script

At this point, you can save a session by doing : <leader> + m where <leader> is usually : Left Alt + \ It can also be just \ so the full thing would be : \ + m, so try both to see exactly which works. It will write save Session at the bottom when it works.

This saves the place you are in the current vim tab, the open tabs and the place you were in all the other tabs.

You have to leave your session by doing :

    :qa

From then on, the session will be saved automatically every time you quit with :qa.

When you want to recover your state, just change your directory to the one from where you opened your initial vim and just type :

    vim

This should reopen your previous state. Pretty neat!

Conclusion

Try it! Enough said!