I’ve spent the last couple of years using Sublime Text and consider myself pretty good at it. But throughout this time, I’ve always tried to optimize my workflow and do without the mouse as much as I can. Then, about a month ago, I decided to spend some time with Vim. I went through the whole motion of getting Vim installed on my machine and going through vimtutor a few times. All in all, I have to say I liked it.

Since then, I’ve really wanted to switch to Vim completely, but I realized there were things in Sublime Text that I just could not do without. I shall be writing a blog post about each of them as I discover ways to achieve them in Vim.

First up, project switching.

Projects in Sublime Text

Sublime text offers excellent project switching. You just load up a folder, save it as a project, and the next time you want to access it, all you do is hit Ctrl+Alt+P , type a little bit of the project name and hit enter. Not only that, your workspace is saved along with your project, with all your open files and even the position of your cursor intact. For someone like me who needs to copy-paste code from one project to another quite regularly, this is something I just couldn’t do without. So, I realized if I was ever to make a complete transition from Sublime Text to Vim, I’d definitely need a good solution for project switching in Vim.

The Solution

After hours of searching through the internet for an efficient way to switch between projects in Vim, this is what I’ve arrived at. I’m sure it’s not perfect, but it seems to be pretty close.

So, to get efficient project switching in Vim, here’s what I’ve finally settled on:

Sessions in Vim

Vim does not have the concept of “projects”. Instead, it calls them “sessions”. To make a session, you simply invoke the

:mksession

command and give it some additional parameters like where to save the session. Then, when you need to load the session, you simply source it.

Here’s an example:

:mksession ~/.vim/sessions/mysession.vim

This saves a session called mysession which you can then load up the next time you run Vim, as follows:

:source ~/.vim/sessions/mysession.vim

An Improvement — vim-session

Sessions are very cool. They save your project and your workspace and do everything Sublime Text projects do. But they’re just so hard to work with. Having switched around so easily between projects in Sublime Text, there was no way I was going to settle for this cumbersome system. There just had to be an improvement…

…and there was. Meet vim-session. It is to sessions what virtualenvwrapper is to virtualenv. Basically, it gives you a list of easy commands that you can invoke when using sessions.

Now, instead of going
:mksession ~/.vim/sessions/mysession.vim  (Yuck!),

you just type
:SaveSession mysession

…and your session is saved, ready to be loaded on your next Vim visit with
:OpenSession mysession

It’s really as easy as that. Now I had a base for project switching in Vim. But there was still something missing. It was still not as easy as Sublime Text. So, I found some great keyboard bindings for it.

Further Improvement — Keyboard Bindings

Great as they are, I’d hate to have to type those pesky SaveSession, OpenSession and CloseSession commands every time I needed to switch to an old project. Yes, even with tab completions, it would be annoying. So, here are some keyboard bindings I used in my vimrc to make my workflow faster:

As I mapped my leader key to comma (,) all I now need to do to save a project is type ,ss  really quickly and give it a name. Then, when I need to switch to another project, I just type ,so  then type a part of the session name and hit tab. To close the current session, I just go ,sc .

More Settings

I also added the following settings to my vimrc:

The first two lines prevent annoying dialog boxes from popping up and the third line allows you to use SessionOpen, SessionClose and so on in addition to OpenSession and CloseSession.

Finally…

I really like how this has turned out. I think it’s incredibly close to what Sublime Text projects offer, and this will definitely make it much easier for me to switch completely to Vim.

I do not claim that this is the best way to switch between projects in Vim. If you have some suggestions for me, or some other technique, I’d love to hear about it in the comments section below.

I’ll be documenting my shift from Sublime Text to Vim in the coming days. So, look out for that.