Edit: As it usually goes, I’ve realized I might want to work on this some more. So, the rudimentary sessions is now a Vim bundle, available on GitHub: https://github.com/casastorta/vim-rudimentary-sessions
My development tools are basically split into 3 categories:
- Eclipse for big projects,
- Sublime Text 2, for most of the open source projects and smaller job projects,
- Vim for “shotgun-development“, when I need to do something quickly and immediately.
That being said, through the years, I’ve got to get used to Eclipse way of handling projects (in terms of keeping record of project files and project specific configurations on the filesystem). Eclipse creates
file inside base directory of your project where everything related to the project is kept. I find it useful, because I don’t have to think extra of what to include in backup process. :-)
So today I pretty much expect every editor I use on part-time basis to be able to store project/session data in similar way. Sublime does it – although it doesn’t come with built-in centralized project repository, when you save main project file and project workspace file, they can be saved pretty much anywhere. I tend to keep them in project’s directory (with obligatory entries for
in .gitignore/.hgignore of course!).
Because of that requirement I’ve put on myself and my text editors, I’ve been trying out various session management Vim scripts (or “plugins”/”bundles” if you want). But it seems that most of the people have different approach to session/project management through the editor than me.
So, basically, today, inspired by Lucas’ approach, I’ve put down rudimentary session management for Vim which is up to my taste:
" Rudimentary session management
if version >= 700
let s:sessionautoloaded = 0
if s:sessionautoloaded == 0
let s:sessionautoloaded = 1
if s:sessionautoloaded == 1
echo "Session saved."
echo "No session to save. Please create session with ':mksession .session.vim' first!"
" Session is saved with ss ( is by default)
nmap ss :call SaveSession()
" Uncomment the following like if you want to save session(s) automatically on
"autocmd VimLeave * call SaveSession()
Logic behind it goes like this:
- On Vim startup, check if file named
(notice the dot prefix, change to your liking if needed) exists
- If it exists, source (interpret) it.
to “save session” functionality
- If there is no session file in current directory, just warn user and tell him how to save the session. Do not write anything on your own.
- Uncomment the last line if you want automatic session saving when you exit Vim.
This pretty much covers what I expect. The requirement for session auto-loading is that you start vim from the project’s own root directory. Which is, accidentally, just how I usually do it.