2

TL;DR: Can Vim store its swap files, and loads the same set of swap files and buffers on load every time?

I'm on a Mac (OS: Maverick), where a number of application keeps the data in some sort of local / out of way storage that's restored between machine restarts / application crashes.

By 'storage', take Sublime Text as an example, as soon as I open a new Tab, it saves the contents of the Tab even if I haven't explicitly saved the Tab as a file for permanent storage anywhere. So if I were to close down ST and reopen it (or it / machine crashes), it would restore all of the Tabs & content that I had before the closure.

This sort of fool-proof content retention (TM) can be seen across a number of Mac Applications (e.g. iMovie, QuickTime Player, TextEdit, etc), and is in line with Mac's design philosophy of always letting the user continue where they left off - without having to save anything explicitly.

Given the fact that:
1. Vim keeps swap files and updates every 200 strokes / 4 seconds of inactivity to recover
2. Vim has startup options that allow files / swap to be opened

there's sufficient foundation here for me to wonder if Vim / MacVim doesn't have this content retention (TM) feature already amongst it's hundreds of configuration options?

p.s. I currently use vim-sessions, purely for 'project like file grouping', but it does not retain contents for unsaved buffers

p.p.s. been playing with the :preserve option and cpoptions+=& in .vimrc. Doesn't seem to have impact, swap files are still deleted on MacVim closes, and nothing is opened when MacVim starts again

  • Can you post a link to your .vimrc? Especially any parts that reference the sessionoptions? – Aaron D Jan 24 '15 at 5:05
  • Since you're comparing against Sublime Text, why not make Sublime your default editor? If you export aliases to "/usr/local/bin/subl -nw" for EDITOR, vi, vim, GIT_EDITOR, etc., you can have what you love about Sublime available all the time. – SaxDaddy Jan 24 '15 at 6:46
  • @SaxDaddy. Two reasons. 1) Vintageous while a great ST plugin still lacks the level of vim comprehensiveness (e.g. macro editing) 2) Commercial cost. I edit most of my code in IntelliJ IDEA, which means I use VIM mostly for quick edits. Having to pay $70 for the privilege of ST when it's not my primary editor seems a little over the top. – snowbound Feb 5 '15 at 2:44
  • @SaxDaddy, in addition. ST3 has typing lag (on a 2011 Macbook Pro, i7, 8GB, SSD) compared to Vim. – snowbound Feb 5 '15 at 2:55
2

The sessions functionality you mentioned is what you want, but you need to pass it an option to make it save buffers as well as open files and positions. You can use :mksession for this.

However, :mksession is still manually controlled; although you can bind it to hotkeys for quick save/restore functionality, if you forget to run the command, you lose your session.

For this reason, I recommend the Session.vim vim plugin. This (along with a few extra options in your .vimrc) will have automatic session saving and restoration. The full list of options is on the page. Specifically, you want to tell it to pass the buffers option to sessionoptions, as per this page.

  • 1
    Hey @aaron D, I have Session.vim installed via vim-session (which is a mirror of Session.vim: github.com/xolox/vim-session). I'm not sure I'm using the wrong options. Are you saying that mksession, or Session.vim could save contents of unsaved buffers? – snowbound Jan 23 '15 at 13:15
  • 1
    Your suggestion, to make sure buffers is in 'sessionoptions', does not cause the contents of a “No Name” buffer to be saved and restored. In fact, buffers was already in the list. I added set sessionoptions+=buffers to my vimrc to be sure, and tried both :mksession/:source and :SaveSession/:OpenSession, but buffer contents are still lost. – Rory O'Kane Feb 10 '15 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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