The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
+ | [[File:Placeholder|video|right|300px]] [[File:Placeholder|right|300px]] |
||
− | {{TipProposed |
||
⚫ | |||
− | |id=0 |
||
− | |previous=0 |
||
− | |next=0 |
||
− | |created=February 5, 2013 |
||
− | |complexity=basic |
||
− | |author= |
||
− | |version=7.0 |
||
− | |subpage=/2013 |
||
− | |category1= |
||
− | |category2= |
||
− | }} |
||
⚫ | |||
− | ==Usage== |
+ | == Usage == |
− | *Starting |
+ | * Starting vim with no filename argument in a git directory will load a previously stored session if one exists. |
− | *Exiting |
+ | * Exiting vim (with no filename argument given at invocation) in a git directory will store the current session. |
− | *Starting |
+ | * Starting vim with a filename argument doesn't restore nor save a session. |
⚫ | |||
− | == |
+ | == Caveats == |
⚫ | |||
+ | |||
+ | == Script == |
||
<pre> |
<pre> |
||
function! FindProjectName() |
function! FindProjectName() |
||
− | let s:name = getcwd() |
+ | let s:name = getcwd() |
− | if !isdirectory(".git") |
+ | if !isdirectory(".git") |
− | let s:name = substitute(finddir(".git", ".;"), "/.git", "", "") |
+ | let s:name = substitute(finddir(".git", ".;"), "/.git", "", "") |
− | end |
+ | end |
+ | |||
− | if s:name != "" |
||
− | + | if s:name != "" |
|
+ | let s:name = matchstr(s:name, ".*", strridx(s:name, "/") + 1) |
||
− | end |
+ | end |
⚫ | |||
+ | |||
⚫ | |||
endfunction |
endfunction |
||
− | " Sessions only restored if we start |
+ | " Sessions only restored if we start vim without args. |
function! RestoreSession(name) |
function! RestoreSession(name) |
||
− | if a:name != "" |
+ | if a:name != "" |
− | if filereadable($HOME . "/.vim/sessions/" . a:name) |
+ | if filereadable($HOME . "/.vim/sessions/" . a:name) |
− | execute 'source ' . $HOME . "/.vim/sessions/" . a:name |
+ | execute 'source ' . $HOME . "/.vim/sessions/" . a:name |
⚫ | |||
end |
end |
||
⚫ | |||
endfunction |
endfunction |
||
− | " Sessions only saved if we start |
+ | " Sessions only saved if we start vim without args. |
function! SaveSession(name) |
function! SaveSession(name) |
||
− | if a:name != "" |
+ | if a:name != "" |
− | execute 'mksession! ' . $HOME . '/.vim/sessions/' . a:name |
+ | execute 'mksession! ' . $HOME . '/.vim/sessions/' . a:name |
− | end |
+ | end |
endfunction |
endfunction |
||
+ | " |
||
" Restore and save sessions. |
" Restore and save sessions. |
||
+ | " |
||
if argc() == 0 |
if argc() == 0 |
||
− | autocmd VimEnter * call RestoreSession(FindProjectName()) |
+ | autocmd VimEnter * call RestoreSession(FindProjectName()) |
− | autocmd VimLeave * call SaveSession(FindProjectName()) |
+ | autocmd VimLeave * call SaveSession(FindProjectName()) |
end |
end |
||
</pre> |
</pre> |
||
− | |||
− | ==Comments== |