Skip To Main Content

Sessions: The vim Feature You Probably Aren’t Using

Posted by Adam Sontag

Feb 08 2013

I may as well come out and say it. In 2010, I leapt directly from Dreamweaver to vim as my primary editor. (I’ve been told that I may be the only person ever to have made this jump, but have no concrete evidence to substantiate this claim.) I had grown tired of Dreamweaver’s engorged appetite for RAM, and had grappled with vim enough in the context of my dealings with my VPS and git that I had already needed to at least kinda learn how it worked. One of the major things that pushed me over the edge was catching a glimpse — I’m not sure where, maybe in MacVim’s menu — of the word “session.”

Part of the reason Dreamweaver was so gluttonous was because I kept every file from every project that I was working on at a time open at once. I had begun to dabble with TextMate and e, and found it incredibly convenient (read: sane) to be able to keep separate projects grouped together. Surrounded as I was, however, by a bunch of vocal vim partisans, its allure was irresistible, espcially once I realised it could provide me this very same convenience.

After a couple of hours (or even days) of working on a project in vim, it starts to have its own character. You get a bunch of tabs open, and then each one ends up with its own set of splits, and sure enough you know to go over here when you need to take a look at the config, and to go over there when you need to iterate on the new component you’ve been working on that week. You’ve built yourself a place to live, but you’re just renting. vim’s session features will let you make that house a home.

When you’ve been working for awhile and you’re all moved in to your new digs — maybe it’s the end of the day, maybe it’s been 71 days and you finally have to give in and restart your computer, you just save the session file to anywhere you’d like (I like ~/.vim/sessions/) using :mksession (or :mks).

:mks ~/.vim/sessions/rooster.vim

The next time you’re ready to start working on that project, source that session file within vim:

:source ~/.vim/sessions/rooster.vim

Or open it when you run vim using the -S flag:

vim -S ~/.vim/sessions/rooster.vim

Boom. Everything will be exactly as you left it: the working directory, your windows, splits, and buffers, and any options you’ve :set. When the cycle repeats as you rearrange the furniture, just overwite the old session by using :mks!.

Sessions don’t save marks, registers, or command line history (which are stored in viminfo), but this hasn’t been a hindrance so far, and should you want to persist it, you can. If you want to further bake the use of sessions into your vim experience, Peter Odding’s session.vim plugin adds a number of helpers for working with session files, and integrates session saving into the default vim workflow. I’ve only just discovered it in the process of researching this post, but I’m certainly looking forward to giving it a shot; it’s an epic pain when you forget to save a long-running session and you’re left traveling back in time the next time you resume your project.

Having my editor become a comfortable and familiar space, on a project-by-project basis, has been a huge boon. It’s immensely useful when you’re neck-deep in a project, and it’s a downright pleasure when stepping back into something you worked on a year earlier feels like returning to an old friend’s pad.

While this may be old hat to some, I’m sure it’s news to others. I’m not exactly the Gandalf of vim, but I couldn’t help the feeling that a lot of people are missing out on the magic here. What do you think? Excited to go cozy up in the comfort of your new digs? Dying to point out some subtlety that I’ve missed? Let’s continue the discussion in the comments!

Posted by
Adam Sontag
on February 8th, 2013

Comments

We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.

Contact Us

We'd love to hear from you. Get in touch!