Vi/Vim restore opened files

ViVim

Vi Problem Overview


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

Vi Solutions


Solution 1 - Vi

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

Solution 2 - Vi

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.

Solution 3 - Vi

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.

Solution 4 - Vi

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.

Solution 5 - Vi

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.

Solution 6 - Vi

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)

Solution 7 - Vi

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. ;-)

Solution 8 - Vi

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.

Solution 9 - Vi

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) >3. Unloads all currently loaded buffers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomView Question on Stackoverflow
Solution 1 - VihgmnzView Answer on Stackoverflow
Solution 2 - ViChristian C. SalvadóView Answer on Stackoverflow
Solution 3 - ViJESiiView Answer on Stackoverflow
Solution 4 - ViklokopView Answer on Stackoverflow
Solution 5 - VijluebbertView Answer on Stackoverflow
Solution 6 - ViautodidaktoView Answer on Stackoverflow
Solution 7 - VijtukiView Answer on Stackoverflow
Solution 8 - Vir.vView Answer on Stackoverflow
Solution 9 - ViChrisView Answer on Stackoverflow