8

Use case:

You have 2-3 files displayed in your MacVim window(s). You press ctrl+Q and MacVim closes. When you restart MacVim it opens displaying the same files you had when you closed it.

How can you do that?

-- ANSWER

I added the following to my .vmirc

  "save and close all files and save global session
nnoremap <leader>q :mksession! ~/.vim/Session.vim<CR>:wqa<CR>
  "close all files without saving and save global session
nnoremap <leader>www :mksession! ~/.vim/Session.vim<CR>:qa!<CR>

function! RestoreSession()
  if argc() == 0 "vim called without arguments
    execute 'source ~/.vim/Session.vim'
  end
endfunction
autocmd VimEnter * call RestoreSession()

With this I almost have the exact behavior I wanted, the only minor nuisance is that I still need to press command-q afterwards but that's MacOs, not vim....

gws
  • 767
  • 1
  • 8
  • 15

3 Answers3

13

Install this plugin: https://github.com/xolox/vim-session

And add this following two lines to your .vimrc :

let g:session_autosave = 'yes'
let g:session_autoload = 'yes'

Now if you close your MacVim/GVim it saves and loads the session automatically.

Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55
9

I suggest you read about sessions :help session,that should answer your question. There are even nice scripts to deal with sessions.

mb14
  • 22,276
  • 7
  • 60
  • 102
  • Thank you mb14, I was able to devise what I needed after following your lead – gws Oct 31 '11 at 20:56
  • @gws can you please write your solution ? Thanks. – J4cK Mar 25 '15 at 00:16
  • @Trickster it is described at the bottom of the answer. I have since stopped using MacVim and moved to terminal vim but the solution was working fine at the time – gws Mar 25 '15 at 09:45
3

I suggest that you use session.vim Extended session management for Vim for session management as this plugin just works. You can automatically save your current session everytime you close vim or create a new session, close/open vim and load that session.

afarazit
  • 4,907
  • 2
  • 27
  • 51