Skip to content

Instantly share code, notes, and snippets.

@jberkel
Created April 18, 2009 11:57
Show Gist options
  • Save jberkel/97573 to your computer and use it in GitHub Desktop.
Save jberkel/97573 to your computer and use it in GitHub Desktop.
session handling for vim
" how to use this: vim -S mksession.vim file_to_edit
" based on scripts from http://vim.wikia.com/wiki/Go_away_and_come_back
function! SessionFile(name)
let b:sessions = "sessions"
if has("gui_running")
let b:sessions = b:sessions . "-gvim"
endif
let b:sessiondir = $HOME . "/.vim/" . b:sessions . getcwd()
return b:sessiondir . '/' . a:name
endfunction
function! MakeSession()
let b:currentfile = expand("%")
if (filereadable(b:currentfile))
let b:sessionfile = SessionFile(b:currentfile)
let b:sessiondir = fnamemodify(b:sessionfile, ":p:h")
if (filewritable(b:sessiondir) != 2)
exe 'silent !mkdir -p ' b:sessiondir
redraw!
endif
if (!filereadable(b:sessionfile) || (getftime(b:sessionfile) < getftime(b:currentfile)))
exe "mksession! " . b:sessionfile
endif
endif
endfunction
function! LoadSession()
if argc() == 1
let b:file = argv(0)
let b:sessionfile = SessionFile(b:file)
if (filereadable(b:sessionfile))
exe 'source ' b:sessionfile
else
"echo "No session loaded."
endif
endif
endfunction
au VimEnter * :call LoadSession()
au VimLeave * :call MakeSession()
if has("autocmd")
"redetect filetype after session load
autocmd SessionLoadPost * filetype detect
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment