Efficient Project Switching in Vim

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.

Ashish Acharya
I am an experienced full-stack web developer, specializing in Python and JavaScript, with a penchant for efficient, readable code. I have worked in, as well as led, software development teams and am comfortable within a modern Scrum/Agile-based team setting. I am passionate about automation, GIS, and data science.

2 Comments

  1. Interesting. Honestly, I did not know about :mksession until this point. In my defense, I never needed to. For me vim and tmux go hand in hand, and I’ve been a faithful user of tmuxinator ever since I’ve known about it.

    For example, I’d have a tmux session for api server in one gnome-terminal window, and the front end project in another gnome-terminal window, handled by another tmux session; each window running it’s own server and an instance of vim.

    Of course you don’t have shared vim buffers (at least not out-of-the-box); which means no copy-paste. While there is a tmux hack that enables you to share vim clipboards between tmux sessions, (and you could also use the system clipboard as the vim default), I am more comfortable using tabs in vim. If there’s something I need to copy-paste between multiple projects, I open them up in new vim tabs. And you could configure it such that each tab would have it’s own buffer list. Also, I think this is the equivalent of having two projects open simultaneously within vim, if that’s what you need. But I like the different-tmux-sessions approach better than this.

    I think it’s worthwhile mentioning, for those starting out, that vim tabs != sublime text tabs.

    Again, this is just the way I do things. I can definitely see how your approach could be useful for certain use cases.

Leave a Comment

Your email address will not be published. Required fields are marked *