Vim Tips Wiki
(Created page with "video|right|300px right|300px Daily I need to switch between different [http://git-scm.com/ git] projects. This script automatically...")
 
(Fix.)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
  +
{{TipProposed
[[File:Placeholder|video|right|300px]] [[File:Placeholder|right|300px]]
 
  +
|id=0
Daily I need to switch between different [http://git-scm.com/ git] projects. This script automatically saves vim sessions per git directory.
 
  +
|previous=0
  +
|next=0
  +
|created=February 5, 2013
  +
|complexity=basic
  +
|author=
  +
|version=7.0
  +
|subpage=/2013
  +
|category1=
  +
|category2=
  +
}}
 
Daily I need to switch between different [http://git-scm.com/ git] projects. This script automatically saves Vim sessions per git directory.
   
== Usage ==
+
==Usage==
* Starting vim with no filename argument in a git directory will load a previously stored session if one exists.
+
*Starting Vim with no filename argument in a git directory will load a previously stored session if one exists.
* Exiting vim (with no filename argument given at invocation) in a git directory will store the current session.
+
*Exiting Vim (with no filename argument given at invocation) in a git directory will store the current session.
* Starting vim with a filename argument doesn't restore nor save a session.
+
*Starting Vim with a filename argument does not restore or save a session.
 
*<code>$HOME/.vim/sessions</code> must exist.
   
== Caveats ==
+
==Script==
* <code>$HOME/.vim/sessions</code> must exist.
 
 
== 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
let s:name = matchstr(s:name, ".*", strridx(s:name, "/") + 1)
 
 
return s:name
end
 
 
return s:name
 
 
endfunction
 
endfunction
   
" Sessions only restored if we start vim without args.
+
" 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
 
end
 
end
 
endfunction
 
endfunction
   
" Sessions only saved if we start vim without args.
+
" 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==

Latest revision as of 03:38, 1 February 2014

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created February 5, 2013 · complexity basic · version 7.0

Daily I need to switch between different git projects. This script automatically saves Vim sessions per git directory.

Usage[]

  • Starting Vim with no filename argument in a git directory will load a previously stored session if one exists.
  • Exiting Vim (with no filename argument given at invocation) in a git directory will store the current session.
  • Starting Vim with a filename argument does not restore or save a session.
  • $HOME/.vim/sessions must exist.

Script[]

function! FindProjectName()
  let s:name = getcwd()
  if !isdirectory(".git")
    let s:name = substitute(finddir(".git", ".;"), "/.git", "", "")
  end
  if s:name != ""
    let s:name = matchstr(s:name, ".*", strridx(s:name, "/") + 1)
  end
  return s:name
endfunction

" Sessions only restored if we start Vim without args.
function! RestoreSession(name)
  if a:name != ""
    if filereadable($HOME . "/.vim/sessions/" . a:name)
      execute 'source ' . $HOME . "/.vim/sessions/" . a:name
    end
  end
endfunction

" Sessions only saved if we start Vim without args.
function! SaveSession(name)
  if a:name != ""
    execute 'mksession! ' . $HOME . '/.vim/sessions/' . a:name
  end
endfunction

" Restore and save sessions.
if argc() == 0
  autocmd VimEnter * call RestoreSession(FindProjectName())
  autocmd VimLeave * call SaveSession(FindProjectName())
end

Comments[]