Tom
Tom

Reputation: 45104

Vi/Vim restore opened files

I was wondering if this common IDE feature is available.

Suppose I open many files using Vim, using vsplit and split. Then, I close everything.

The next day, I want to recover those files. That is, recover the way they were opened, not having to open each one (using split and vsplit) again.

Is that possible?

UPDATE:

Using mksession! and source commands, mapping commands in .vimrc file, is there a way to parameterize mappings so as to write a specific file?

for example:

map <F2> :mksession! ~/vim_session @INSERT_HERE<cr> "Save session to @INSERTHERE file

Thanks in advance

Upvotes: 52

Views: 24799

Answers (9)

JESii
JESii

Reputation: 4937

The best approach I've found is to use the vim-session plugin, http://github.com/xolox/vim-session which can be installed by adding:

Plugin 'xolox/vim-session'

to your .vimrc, followed by:

:PluginInstall

This plugin supports multiple sessions: you identify a session by name and then manage it with that name. Sessions can be added, deleted, updated, listed, ...

I've been using it for several months now; highly recommended.

Upvotes: 5

Chris
Chris

Reputation: 379

Based on hgmnz's answer and expanding for the update.

You can drop the <cr> from the mapping and this will put you into command mode to finish the command.

map <F2> :mksession! ~/<will stop here and you can type>

When going in the command mode you can type % then press tab and it will expand to the current file's absolute path, you can also type %:p:h and press tab to expand to the current file's directory.

You can also save each session to the working directory and by using local buffer directories you can have multiple sessions for working directories.

map <F2> :mksession! ./.vim_sessions <cr> " Will save session to current buffers directory.
map <F3> :source ./.vim_sessions <cr>
:lcd " Changes the current's buffer directory

Also be careful with reloading sessions as it does unload all current loaded buffers (http://vimdoc.sourceforge.net/htmldoc/starting.html#%3Amksession)

  1. Unloads all currently loaded buffers.

Upvotes: 0

r.v
r.v

Reputation: 4877

I suggest using vim-obsession. It is much better than the default mksession as it stays out of your while still doing what you want.

Upvotes: 0

jtuki
jtuki

Reputation: 415

My solution is as below, put them in .vimrc file. HTH.

" session related.
" Default session is located `~/.session_'. The suffix `_' is a dirty
" solution, just like the one-element tuple `(tuple_eliment,)' in Python..
cnoremap <C-O> source ~/.session_
cnoremap <C-S> mksession! ~/.session_
nnoremap <silent> <C-S><C-S> :mksession! ~/.session_ <CR>

Seems to be more complex, but very useful if you have multiple sessions to save and load.

P.S.
Here, I adopt the familiar <Ctrl-S> and <Ctrl-O> shortcuts, to save and load sessions. IMHO, this is more comfortable than reaching out my hand to <Fn> key. ;-)

Upvotes: 1

autodidakto
autodidakto

Reputation: 321

SessionMan http://www.vim.org/scripts/script.php?script_id=2010 offers easier functionality. Be careful with miniBufExplorer. It's incompatible unless you use the modified version made by andrew on vim_use (and even then, it's only halfway compatible)

Upvotes: 1

klokop
klokop

Reputation: 2410

You could consider using GNU Screen. In short: it's a command line Window Manager that allows a user to access multiple separate terminal sessions inside a single terminal session. The main advantage to me is that you can detach the session, close your terminal window, and later re-attach the session, and continue working.

Upvotes: 2

jluebbert
jluebbert

Reputation: 260

You might be interested in this book:

http://www.swaroopch.com/notes/Vim#Download

It's one of the first few things they show you as an example of how great Vim is. ;)

Also, <cr> stands for Carriage Return.

Upvotes: 1

hgmnz
hgmnz

Reputation: 13306

You can map using :mksession and :source to a set of keys for easy saving and restoring. Here's an example from my .vimrc that uses F2 and F3:

map <F2> :mksession! ~/vim_session <cr> " Quick write session with F2
map <F3> :source ~/vim_session <cr>     " And load session with F3

Upvotes: 108

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827218

Give a look at the :mksession command, to create a session:

A Session keeps the Views for all windows, plus the global settings. You can save a Session and when you restore it later the window layout looks the same. You can use a Session to quickly switch between different projects, automatically loading the files you were last working on in that project.

Upvotes: 11

Related Questions