Last active
August 23, 2021 14:21
-
-
Save dlaub3/5c540f3314f4d447dd3ee1237e445881 to your computer and use it in GitHub Desktop.
Automatically manage saving Vim sessions and resoring the layout
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
function SaveSession() | |
let session_dir = $HOME . "/.vimsessions/" | |
let session_file = "dir" . substitute(getcwd(), '/', '_', 'g') | |
if v:this_session | |
execute "mks! " . v:this_session | |
echo "saved " . v:this_session | |
return 0 | |
endif | |
let branch_name = "_git_" . substitute(substitute(system("git branch --show-current"), '\n', '', 'g'), '/', '_', 'g') | |
if v:shell_error == 0 | |
let session_name = session_file . branch_name . ".vim" | |
execute "mks! " . session_dir . session_name | |
echo "saved " . session_name | |
return 0 | |
endif | |
execute "mks! " . session_dir . session_file . ".vim" | |
endfunction | |
command! -nargs=0 SaveSession call SaveSession() | |
function LoadSession() | |
if argc() == "" | |
let session_dir = $HOME . "/.vimsessions/" | |
let session_file = "dir" . substitute(getcwd(), '/', '_', 'g') | |
let branch_name = "_git_" . substitute(substitute(system("git branch --show-current"), '\n', '', 'g'), '/', '_', 'g') | |
if v:shell_error == 0 | |
let session_name = session_file . branch_name . ".vim" | |
execute "so " . session_dir . session_name | |
echo "Loaded: " . session_name | |
return 0 | |
endif | |
let session_file = "dir" . substitute(getcwd(), '/', '_', 'g') | |
let session_name = session_file . ".vim" | |
let session_path = session_dir . session_name | |
if filereadable(session_path) | |
execute "so " . session_path | |
echo "Loaded: " . session_name | |
return 0 | |
endif | |
echo "No saved sessions to load." | |
endif | |
endfunction | |
command! -nargs=0 LoadSession call LoadSession() | |
au VimEnter * :call LoadSession() | |
au VimLeave * :call SaveSession() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment