Tip: very rudimentary session management in Vim

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
.project
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
*.sublime-*
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:

Logic behind it goes like this:

  1. On Vim startup, check if file named
    .session.vim
    (notice the dot prefix, change to your liking if needed) exists
  2. If it exists, source (interpret) it.
  3. Assign
    <leader>ss
    to “save session” functionality
  4. 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.
  5. 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.

Author: Vedran Krivokuca

A developer living and working in Germany. Wannabe opensource contributor. Feeling strong of some social issues.

Leave a Reply