Created
April 18, 2009 11:57
-
-
Save jberkel/97573 to your computer and use it in GitHub Desktop.
session handling for vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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