78

I have several tasks I am working on in one repository. I would like to create "workspaces" for working on them.

One "workspace" or "buffer layout" would have all files I need to work on open in the layout I want (using ^w+v, ^w+s to split window).

Then, when I start working on another task I would like to save my current layout, move to another one, but I would like to be able to get back to previous layouts.

What is the best way to achieve this?

1

4 Answers 4

73

You can use vim's :mksession and write each "workspace" to a different file, then reopen a session using vim -S session_file, however, if you're open to using a plugin then I find the vim-startify plugin is exactly what I need for this sort of scenario.

It does a great job of managing sessions, in vim and mccvim, and it gives you a list of recent files as well when you open vim. It will auto-change to the right working directory for you too. It also lets you pin files to the startify opening screen as well, which is nice for easy access to things like your .vimrc

33

You can use tpope's vim-obsession plugin to easily manage sessions. It is like a wrapper to Vim's in-built mksession, but provides a set of other niceties as well.

You can save the current session (or buffer layout) by giving the command :Obsession. If you don't supply an argument, it writes a session file called Session.vim by default.

To reload a session, either use vim -S <session-name> or :source <session-name> if you're inside Vim already. By default, vim -S with no arguments will read Session.vim.

The nice thing is, you don't have to remember to save the session each time you exit Vim. It is automatically managed by the plugin.

Another very important feature of vim-obession is that it does not save options and maps. mksession captures the current options and maps, which you don't want to happen if you just want to save the buffer layout. Also it interferes when a plugin is updated, etc.

5
  • Can it manage multiple session files?
    – Nebril
    Commented Feb 4, 2015 at 11:32
  • 1
    @Nebril It can. When you save the session, use the format :Obsess <session-name>. Use the particular session name you want when reloading.
    – thameera
    Commented Feb 4, 2015 at 11:35
  • I found that Obsession didn't remember vertical splits, whereas mksession did. I've only been messing about with this tonight though, is there something that I've missed?
    – baxx
    Commented Apr 17, 2015 at 0:07
  • 1
    @baxx Obsession does remember all kinds of splits. Maybe you closed the split before quitting vim?
    – thameera
    Commented Sep 6, 2015 at 4:27
  • 6
    @baxx This is old but I wanted to add a note in case this helps anyone else. The reason it looks as if obsession is forgetting splits is probably because you are quitting all your splits to leave vim. That means the final state is your last window when you close vim, that's what obsession stores. I had the same problem with tabs and the answer is to use :qa to quit all open windows, splits, tabs. If someone has a better option please let us know.
    – TC0072
    Commented Dec 9, 2018 at 15:12
16

As a side note, I'd like to point out that I built yet another plugin dhruvasagar/vim-prosession as an extension to tpope/vim-obsession that enhances it even further to create & manage vim sessions by default in a centralised repository as per configuration on a per directory basis and loads them automatically when you launch vim without any arguments on the directory. It also allows you to switch between different sessions for convenience.

1
  • 1
    This sounds awesome. If I could manage session files in a folder similar to undo and backup files to keep my projects from getting cluttered with swap files that might just blow my mind.
    – dragon788
    Commented May 15, 2018 at 17:06
3

You can try the vim-workspace plugin, its session management features are automated, scoped for your current working directory, and is pretty simple (only need to run ToggleWorkspace once).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.