tag:ryankinderman.net,2005:/tag/vimkinderman.net : Tag vim, everything about vimcode code code2010-07-19T18:52:46-07:00Typotag:ryankinderman.net,2005:Article/1162010-07-19T18:40:19-07:002010-07-19T18:52:46-07:00ryanEasily add/remove Vim scripts<p>I like being able to easily add and remove Vim scripts, whether it's to try one out or easily upgrade to a newer version down the line. Since the directory structure of a script almost always follows the standard runtime directory structure, I simply wrote a script that adds each directory under $HOME/.vim/vendor to Vim's <code>runtimepath</code>, so that Vim includes the vendor directories in its script-searching behavior. That way, I can simply download something like <a href="http://www.vim.org/scripts/script.php?script_id=1567">rails.vim</a>, which has files in <code>autoload</code>, <code>doc</code>, and <code>plugin</code>, and would be very annoying to remove manually, uncompress it into its own directory under <code>$HOME/.vim/vendor</code>, restart Vim, and the script is loaded. Removing the script is as easy as removing the directory under <code>vendor</code> and restarting Vim.</p>
<p>To add this behavior, simply put the following in your <code>$HOME/.vimrc</code> file:</p>
<pre><code>let vendorpaths = globpath("$HOME/.vim", "vendor/*")
let vendorruntimepaths = substitute(vendorpaths, "\n", ",", "g")
set runtimepath^=vendorruntimepaths
let vendorpathslist = split(vendorpaths, "\n")
for vendorpath in vendorpathslist
if isdirectory(vendorpath."/doc")
execute "helptags ".vendorpath."/doc"
endif
endfor
</code></pre>
<p>For the latest and greatest version of this code, refer to <a href="http://github.com/ryankinderman/dotfiles/blob/master/vimrc">my vimrc</a>.</p>tag:ryankinderman.net,2005:Article/1152010-07-19T17:41:40-07:002010-07-19T17:43:00-07:00ryanAuto-saving sessions in Vim<p>Back when I used Textmate, I liked that you could save projects to a file so that you could quit Textmate and come back later, load the project file up, and have all the files and tabs set up and open the way you had them. Since switching to Vim, I've gotten the same functionality (and more!) using the <code>:mksession</code> command. One thing that's missing in Vim, however, is the ability to auto-save a session. There are a few add-ons that do this, but add a bunch of other functionality that I find unnecessary. Below is a little script I wrote that adds auto-saving sessions to Vim:</p>
<pre><code>function! AutosaveSessionOn(session_file_path)
augroup AutosaveSession
au!
exec "au VimLeave * mks! " . a:session_file_path
augroup end
let g:AutosaveSessionFilePath = a:session_file_path
echo "Auto-saving sessions to \"" . a:session_file_path . "\""
endfunction
function! AutosaveSessionOff()
if exists("g:AutosaveSessionFilePath")
unlet g:AutosaveSessionFilePath
endif
augroup AutosaveSession
au!
augroup end
augroup! AutosaveSession
echo "Auto-saving sessions is off"
endfunction
command! -complete=file -nargs=1 AutosaveSessionOn call AutosaveSessionOn(<f-args>)
command! AutosaveSessionOff call AutosaveSessionOff()
augroup AutosaveSession
au!
au SessionLoadPost * if exists("g:AutosaveSessionFilePath") != 0|call AutosaveSessionOn(g:AutosaveSessionFilePath)|endif
augroup end
</code></pre>
<p>To begin auto-saving sessions, simply run:</p>
<pre><code>:AutosaveSessionOn <session filename>
</code></pre>
<p>Your session will then be automatically saved to the given session filename when Vim exits. Also, if you have <code>globals</code> in your <code>sessionoptions</code> list, when you load the auto-saved session, auto-saving will continue to occur to that session file. To turn auto-saving to the session file off, run:</p>
<pre><code>:AutosaveSessionOff
</code></pre>
<p>For the latest and greatest version, refer to <a href="http://github.com/ryankinderman/dotfiles/blob/master/vimrc">my vimrc</a>.</p>