Disclaimer: This content is not owned by the current webmaster nor do we claim the credit. Ownership of this domain has changed hands. This URL however as a courtesy to web users is a historic copy of what was originally published.
Conner Mcdaniel has created a new blog at https://connermcd.wordpress.com You can find all of his current publication there.
Easy vim session management
Mar 11, 2013
Until recently I haven’t really harnessed the power of vim’s session management capabilities. It can be a hassle somewhat to always use :mksession and :source, so I usually just neglect to do it; however, if you’re commonly working with large projects then it can be very useful to drop a session file into the root directory and make use of it. To aid me in utilizing this feature I added some customizations to my ~/.vimrc:
command! Mks let g:session = getcwd() call Mks(g:session)
au BufRead *.session let g:session = expand(‘%:p:h’) | so % | bd #
au VimLeave * if exists(‘g:session’) | call Mks(g:session) | endif
exe “mksession! “.a:path.”/”.fnamemodify(a:path, ‘:t’).”.session”
First, this sets a new custom user command called :Mks which will make a session file based on the current working directory. For instance, if :pwd is /home/connermcd/.bin/project/ then this will make a file inside that folder called project.session. It also sets a global variable (g:session) to the current working directory so that it can save the session here later. In the augroup a couple of autocommands are set. The first one (BufRead) executes when you open a file with the .session extension. This will set the g:session variable, source the session file, and delete it from the buffer list. The second one (VimLeave) executes when you close vim. If a g:session variable is found then it will override the session file with the new information. That’s pretty much it!
This works best if you aren’t using the autochdir option. I find that with it turned off it makes project navigation much more simple. Now when you create a new project just run the :Mks command to start session tracking, and when you want to access it later just open the .session file with vim.