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!
Comments
We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.
Sessions are indeed a nice feature when you are a serious and focused person ;-). If you are of the kind that is always tinkering with his .vimrc or playing with mappings/commands/functions/plugins, sessions can quickly become a double-edged sword.
FWIW, it went this way for me: Dreamweaver -> BBEdit -> TextMate -> 10/11 months spent trying every cross-platform editor/IDE available -> Vim.
For me, it went: Some online wysiwyg -> Some native wysiwyg -> Eclipse (tip: let Eclipse handle Java, not HTML) -> Vim
I’ve still yet to try DreamWeaver. Eventually, I guess everybody has to learn it, but I’m not really looking forward to it.
You know you can just load in the updated .vimrc, right?
:source ~/.vimrc
or if you are editing your vimrc you can \”so[urce] (current file)\”
:so %
Holy SHIT I wish I read man pages more often! Occasionally I create a tmux session with panes when I need to jump back into something. I also sometimes created bash scripts doing something like gvim -c ‘cd ~/path/to/project | e .’ to quickly jump into a project.
But this is the first time I have read about this awesome VIM feature! Thanks for sharing!
Hahaha, same. I’ve had a tmux window open on my VPS for the past three months for my Vim project. lmao
Thanks for the post! I feel ridiculous for not knowing about this before.
Glad it’s useful for ya forresthopkinsa !
thans a lot !
I really wish mksession used relative directories instead of absolute. My session files are pretty useless because I often move an entire project directory to a different location.
You could use a symbolic linked directory to set up your session then only have to resymlink the directory as you move it. ‘ln -s /targetdir /sourcedir’ in unix or the utility Link Shell Extension in Windows.
Great feature! But when I open a saved session, my Status Line and Tab Line are in their default look, and not in the customised look that I defined in ~/.vimrc. The other commands defined in ~/.vimrc work fine, however.
Sourcing ~/.vimrc in vim, or starting vim with \”-S ~/.vim/sessions/session.vim -S ~/.vimrc\” fixes the issue, but I’m still curious what might be the reason for this.
Although I’d like to swoop in with a solution, I personally use the vim status line and tab line in their default configuration, so I’ve never experienced this problem :/
Haha, true! But so much better to customise them, like making the status line of the active window a distinct colour, so you can easily see which window is active 😉
Works for me with `-S session.vim`. Maybe you should check your :scriptnames ?
This is awesome, thanks for the post.
Can someone explain why this is such a big deal ?
Just saving your tabs and split screens doesn’t seem to be
greatly useful.
Thanks for your comment, Malhar.
I can say that in my case having multiple files open at the same time and in view together greatly helps my workflow. What this post addresses is how to be able to save your multi-tab, mult-file split state in a session for later so that you can close it and so that it will stop taking your computer resources to stay open.