Previous Section  < Day Day Up >  Next Section

Recipe 6.14. Picking Up Where You Left Off: Using Vim's Sessions

6.14.1 Problem

You get all involved in a project, then have to shut down for the night before you're finished. Wouldn't it be nice to save everything—options, mappings, open files, open windows, window positions and sizes—that normally goes away when you close out?

6.14.2 Solution

No problem—create sessions. A Vim session saves everything, and restores it exactly the way you left it the next time you start the session. When you reach a point where you would like to preserve the work environment, save all open files, then name the session:

:wall

:mksession myarticle.vim

If more than one file is open, use :wqall to save and close all of them at once. This example creates a file called myarticle.vim. To start up the same session, enter:

$ vim -S myarticle.vim

After working in this session, you have two choices. You can save all your changes under the same filename:

:mksession! myarticle.vim

or do your own quick-and-easy version control by changing the session name:

:mksession myarticle_rev2.vim

You can also go directly to another session without shutting down:

:wall

:mksession! myarticle_rev2.vim

:source myarticle.vim

Another cool thing you can do with sessions is create your perfect working environment, then save it for posterity:

$ vim

:help

^W w

:vertical split /~

Figure 6-1 shows what this looks like.

Figure 6-1. Create an ideal working environment


Enlarge the file tree window and shrink the help window until they're just right. Use ^W w to navigate between the windows and ^W + or - to adjust the window sizes. When it's all perfect, save it as :mksession 3pane.vim (Figure 6-2).

Figure 6-2. Make your adjustments and save the session


6.14.3 Discussion

Using sessions gives you a fast, powerful way to create custom working environments. It's a good idea to organize your session files in their own directory, so you can find them quickly.

6.14.4 See Also

  • Vim's online help (:help session, :help usr_08.txt)

  • Chapter 21 of Vi IMproved—Vim

    Previous Section  < Day Day Up >  Next Section