As an old VIM user, attracted to it in the first place especially because of its straightforward regex substitutions that allow to work really fast, I'm not really expecting VIM to have such a feature. But, what the heck, maybe I overlooked something or some novelty.

Would you happen to know a way for vim, when reopening a huge list of files (all the cpp/h source files of a project namely) in n-column-split mode adapted to screen size (see bash command/alias below), to have previous open/selected/active buffers come back instead of resetting to the n first files across visible buffers? A bit like an IDE would do it, by saving the open files state.

# check the window size after each command and, if necessary,                   
# update the values of LINES and COLUMNS.                                       
shopt -s checkwinsize                                                           
alias vimcpp='find . \( -name '\''*.cpp'\'' -o -name '\''*.cc'\'' -o -name '\''*.cxx'\'' -o -name '\''*.c'\'' -o -name '\''*.hpp'\'' -o -name '\''*.hh'\'' -o -name '\''*.hxx'\'' -o -name '\''*.h'\'' \) -exec vim -O$(( ( ( $COLUMNS - 1 ) / 80 > 0 ) ? ( ( $COLUMNS - 1 ) / 80 ) : 1 )) \{} \+'

1 Answer 1


You can use vim sessions for that. Just run:

:mksession mysession.vim

and a file will be created in the current directory (called 'mysession.vim'). When you next open vim, you can do:

 :source mysession.vim

(or whatever you named it) and you will find yourself back to the state you were when you made the session file (all the splits will be there etc.).

From the help:

This is introduced in sections 21.4 and 21.5 of the user manual.

:mks[ession][!] [file]  Write a Vim script that restores the current editing
            When [!] is included an existing file is overwritten.
            When [file] is omitted "Session.vim" is used.

The resulting file, when executed with a ":source" command:

1. Restores global mappings and options, if 'sessionoptions' contains
   "options".  Script-local mappings will not be written.
2. Restores global variables that start with an uppercase letter and contain
   at least one lowercase letter, if 'sessionoptions' contains "globals".
3. Closes all windows in the current tab page, except the current one; closes
   all tab pages except the current one (this results in currently loaded
   buffers to be unloaded, some may become hidden if 'hidden' is set or
   otherwise specified); wipes out the current buffer, if it is empty
   and unnamed.
4. Restores the current directory if 'sessionoptions' contains "curdir", or
   sets the current directory to where the Session file is if 'sessionoptions'
   contains "sesdir".
5. Restores GUI Vim window position, if 'sessionoptions' contains "winpos".
6. Restores screen size, if 'sessionoptions' contains "resize".
7. Reloads the buffer list, with the last cursor positions.  If
   'sessionoptions' contains "buffers" then all buffers are restored,
   including hidden and unloaded buffers.  Otherwise only buffers in windows
   are restored.
8. Restores all windows with the same layout.  If 'sessionoptions' contains
   "help", help windows are restored.  If 'sessionoptions' contains "blank",
   windows editing a buffer without a name will be restored.
   If 'sessionoptions' contains "winsize" and no (help/blank) windows were
   left out, the window sizes are restored (relative to the screen size).
   Otherwise, the windows are just given sensible sizes.
9. Restores the Views for all the windows, as with |:mkview|.  But
   'sessionoptions' is used instead of 'viewoptions'.
10. If a file exists with the same name as the Session file, but ending in
   "x.vim" (for eXtra), executes that as well.  You can use *x.vim files to
   specify additional settings and actions associated with a given Session,
   such as creating menu items in the GUI version.

There's also a plugin from Tpope that tweaks the behaviour of sessions.

  • And you can even reopen the session with vim -c 'source mysession.vim' (Execute a command within Vim from the command line). Very good! Thanks! Now, automating sessions might be a next step (i.e. make exiting trigger an mksession command and write some shell alias that would check for session to call vim -c 'source whateversession.vim' when it exists instead of the default command). Nice! A new pan of vim science opens up (for me at least :-) )!
    – tomoyo255
    Commented Apr 24, 2021 at 0:33
  • 1
    Yeah - I would probably do that using an autocmd event like VimEnter to run the source Session.vim command and use an autocmd like VimLeave (or maybe VimLeavePre) to run the command :mksession! (with no session name, which the defaults to Session.vim) and use the ! to overwrite existing Session.vim files
    – mattb
    Commented Apr 24, 2021 at 8:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .