Skip to content

Commit

Permalink
Enable opening last used session when starting Vim (thanks to James C…
Browse files Browse the repository at this point in the history
…ox for suggestion)
  • Loading branch information
xolox committed May 26, 2011
1 parent 99d49ae commit 6855a81
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 54 deletions.
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -74,7 +74,7 @@ Execute this command to view the Vim script generated for a session. This comman

### The `sessionoptions` setting

Because the `session.vim` plug-in uses Vim's [:mksession][mksession] command you can change how it works by setting ['sessionoptions'](http://vimdoc.sourceforge.net/htmldoc/options.html#%27sessionoptions%27) in your [vimrc script] [vimrc]:
Because the `session.vim` plug-in uses Vim's [:mksession][mksession] command you can change how it works by setting ['sessionoptions'](http://vimdoc.sourceforge.net/htmldoc/options.html#%27sessionoptions%27) in your [vimrc script] [vimrc], for example:

" If you only want to save the current tab page:
set sessionoptions-=tabpages
Expand All @@ -94,6 +94,10 @@ By default this option is set to false (0). This means that when you start Vim w

By default this option is set to false (0). When you've opened a session and you quit Vim, the `session.vim` plug-in will ask whether you want to save the changes to your session. Set this option to true (1) to always automatically save open sessions when you quit Vim.

### The `g:session_default_to_last` option

By default this option is set to false (0). When you set this option to true (1) and you start Vim, the session plug-in will open your last used session instead of the default session. Note that the session plug-in will still show you the dialog asking whether you want to restore the last used session. To get rid of the dialog you have to set `g:session_autoload` to true (1).

### The `g:loaded_session` option

This variable isn't really an option but if you want to avoid loading the `session.vim` plug-in you can set this variable to any value in your [vimrc script] [vimrc]:
Expand All @@ -105,8 +109,8 @@ This variable isn't really an option but if you want to avoid loading the `sessi
Vim's [:mksession][mksession] command isn't fully compatible with plug-ins that create buffers with generated content and because of this `session.vim` includes specific workarounds for such plug-ins:

* [NERD tree](http://www.vim.org/scripts/script.php?script_id=1658) and [Project](http://www.vim.org/scripts/script.php?script_id=69) windows are supported;
* When [shell.vim](http://peterodding.com/code/vim/shell/) is installed Vim's full-screen state is persisted.
* The [netrw](http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw-start) and [taglist.vim](http://www.vim.org/scripts/script.php?script_id=273) plug-ins support sessions out of the box;
* When [shell.vim](http://peterodding.com/code/vim/shell/) is installed Vim's full-screen state is persisted;
* The [netrw](http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw-start) and [taglist.vim](http://www.vim.org/scripts/script.php?script_id=273) plug-ins support sessions out of the box.

If your favorite plug-in doesn't work with `session.vim` drop me a mail and I'll see what I can do. Please include a link to the plug-in in your e-mail so that I can install and test the plug-in.

Expand Down
41 changes: 35 additions & 6 deletions autoload/xolox/session.vim
@@ -1,6 +1,6 @@
" Vim script
" Author: Peter Odding
" Last Change: May 12, 2011
" Last Change: May 26, 2011
" URL: http://peterodding.com/code/vim/session/

let s:script = expand('<sfile>:p:~')
Expand Down Expand Up @@ -187,12 +187,14 @@ function! xolox#session#auto_load() " {{{2
endif
endfor
endif
" Check whether the default session should be loaded.
let path = xolox#session#name_to_path('default')
" Default to the last used session or the session named `default'?
let session = s:last_session_recall()
let path = xolox#session#name_to_path(session)
if filereadable(path) && !s:session_is_locked(path)
let msg = "Do you want to restore your default editing session?"
if s:prompt(msg, 'g:session_autoload')
OpenSession default
let msg = "Do you want to restore your %s editing session?"
let label = session != 'default' ? 'last used' : 'default'
if s:prompt(printf(msg, label), 'g:session_autoload')
execute 'OpenSession' fnameescape(session)
endif
endif
endif
Expand Down Expand Up @@ -280,6 +282,7 @@ function! xolox#session#open_cmd(name, bang) abort " {{{2
call s:lock_session(path)
execute 'source' fnameescape(path)
unlet! s:session_is_dirty
call s:last_session_persist(name)
call xolox#misc#timer#stop("%s: Opened %s session in %s.", s:script, string(name), starttime)
call xolox#misc#msg#info("%s: Opened %s session from %s.", s:script, string(name), fnamemodify(path, ':~'))
endif
Expand Down Expand Up @@ -315,6 +318,7 @@ function! xolox#session#save_cmd(name, bang) abort " {{{2
let msg = "%s: Failed to save %s session to %s!"
call xolox#misc#msg#warn(msg, s:script, string(name), friendly_path)
else
call s:last_session_persist(name)
call xolox#misc#timer#stop("%s: Saved %s session in %s.", s:script, string(name), starttime)
call xolox#misc#msg#info("%s: Saved %s session to %s.", s:script, string(name), friendly_path)
let v:this_session = path
Expand Down Expand Up @@ -477,6 +481,31 @@ function! xolox#session#complete_names(arg, line, pos) " {{{2
return map(names, 'fnameescape(v:val)')
endfunction

" Default to last used session: {{{2

function! s:last_session_file()
let directory = xolox#misc#path#absolute(g:session_directory)
return xolox#misc#path#merge(directory, 'last-session.txt')
endfunction

function! s:last_session_persist(name)
if g:session_default_to_last
if writefile([a:name], s:last_session_file()) != 0
call xolox#misc#msg#warn("Failed to persist name of last used session!")
endif
endif
endfunction

function! s:last_session_recall()
if g:session_default_to_last
let fname = s:last_session_file()
if filereadable(fname)
return readfile(fname)[0]
endif
endif
return 'default'
endfunction

" Lock file management: {{{2

if !exists('s:lock_files')
Expand Down
106 changes: 63 additions & 43 deletions doc/session.txt
Expand Up @@ -9,9 +9,9 @@ and the files they contain.
To persist your current editing session you can execute the |:SaveSession|
command. If you don't provide a name for the session 'default' is used. You're
free to use whatever characters you like in session names. When you want to
restore your session simply execute |:OpenSession|. Again the name 'default' is
used if you don't provide one. When a session is active, has been changed and
you quit Vim you'll be prompted whether you want to save the open session
restore your session simply execute |:OpenSession|. Again the name 'default'
is used if you don't provide one. When a session is active, has been changed
and you quit Vim you'll be prompted whether you want to save the open session
before quitting Vim:

Screenshot of auto-save prompt, see reference [1]
Expand All @@ -32,10 +32,12 @@ Vim plug-ins:
The session scripts created by this plug-in are stored in the directory
'~/.vim/sessions' (on UNIX) or '~\vimfiles\sessions' (on Windows) but you can
change the location by setting |g:session_directory|. If you're curious what the
session scripts generated by 'session.vim' look like see the sample below [3].
change the location by setting |g:session_directory|. If you're curious what
the session scripts generated by 'session.vim' look like see the sample below
[3].

==============================================================================
===============================================================================
*session-installation*
Installation ~

Unzip the most recent ZIP archive [4] file inside your Vim profile directory
Expand All @@ -44,27 +46,28 @@ restart Vim and execute the command ':helptags ~/.vim/doc' (use ':helptags
~\vimfiles\doc' instead on Windows). After you restart Vim the following
commands will be available to you:

==============================================================================
===============================================================================
*session-commands*
Commands ~

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:SaveSession* command

This command saves your current editing session just like Vim's built-in
|:mksession| command does. The difference is that you don't pass a full pathname
as argument but just a name, any name really. Press <Tab> to get completion of
existing session names. If you don't provide an argument the name 'default' is
used, unless an existing session is open in which case the name of that
as argument but just a name, any name really. Press '<Tab>' to get completion
of existing session names. If you don't provide an argument the name 'default'
is used, unless an existing session is open in which case the name of that
session will be used.

If the session you're trying to save is already active in another Vim instance
you'll get a warning and nothing happens. You can use use a bang (!) as in
you'll get a warning and nothing happens. You can use a bang (!) as in
':SaveSession! ...' to ignore the warning and save the session anyway.

As mentioned earlier your session script will be saved in the directory
pointed to by |g:session_directory|.

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:OpenSession* command

This command is basically |:source| in disguise, but it supports tab completion
Expand All @@ -82,14 +85,14 @@ a list:
Type number and <Enter> or click with mouse (empty cancels):
If the session you're trying to open is already active in another Vim instance
you'll get a warning and nothing happens. You can use use a bang (!) as in
you'll get a warning and nothing happens. You can use a bang (!) as in
':OpenSession! ...' to ignore the warning and open the session anyway.

Note also that when you use a bang (!) right after the command name existing
tab pages and windows are closed, discarding any changes in the files you were
editing!

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:RestartVim* command

This command saves your current editing session, restarts Vim and restores
Expand All @@ -103,7 +106,7 @@ after Vim is restarted and your session has been restored. This makes it easy
to perform manual tests which involve restarting Vim, e.g. ':RestartVim | edit
/path/to/file | call MyTest()'.

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:CloseSession* command

This command closes all but the current tab page and window and then edits a
Expand All @@ -114,41 +117,41 @@ Note that when you use a bang (!) right after the command name existing tab
pages and windows are closed, discarding any changes in the files you were
editing!

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:DeleteSession* command

Using this command you can delete any of the sessions created by this plug-in.
If the session you are trying to delete is currently active in another Vim
instance you'll get a warning and nothing happens. You can use use a bang (!)
as in ':DeleteSession! ...' to ignore the warning and delete the session
anyway.
instance you'll get a warning and nothing happens. You can use a bang (!) as
in ':DeleteSession! ...' to ignore the warning and delete the session anyway.

Note that this command only deletes the session script, it leaves your open
tab pages and windows exactly as they were.

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *:ViewSession* command

Execute this command to view the Vim script generated for a session. This
command is useful when you need to review the generated Vim script repeatedly,
for example while debugging or modifying the 'session.vim' plug-in.

==============================================================================
===============================================================================
*session-options*
Options ~

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *sessionoptions* setting

Because the 'session.vim' plug-in uses Vim's |:mksession| command you can change
how it works by setting ||sessionoptions|| in your |vimrc| script:
how it works by setting |'sessionoptions'| in your |vimrc| script, for example:
>
" If you only want to save the current tab page:
set sessionoptions-=tabpages
>
" If you don't want help windows to be restored:
set sessionoptions-=help
------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *g:session_directory* option

This option controls the location of your session scripts. Its default value
Expand All @@ -157,7 +160,7 @@ don't mind the default you don't have to do anything; the directory will be
created for you. Note that a leading '~' is expanded to your current home
directory ('$HOME' on UNIX, '%USERPROFILE%' on Windows).

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *g:session_autoload* option

By default this option is set to false (0). This means that when you start Vim
Expand All @@ -166,15 +169,24 @@ without opening any files and the 'default' session script exists, the
session. When you set this option to true (1) and you start Vim without
opening any files the default session will be restored without a prompt.

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *g:session_autosave* option

By default this option is set to false (0). When you've opened a session and
you quit Vim, the 'session.vim' plug-in will ask whether you want to save the
changes to your session. Set this option to true (1) to always automatically
save open sessions when you quit Vim.

------------------------------------------------------------------------------
-------------------------------------------------------------------------------
The *g:session_default_to_last* option

By default this option is set to false (0). When you set this option to true
(1) and you start Vim, the session plug-in will open your last used session
instead of the default session. Note that the session plug-in will still show
you the dialog asking whether you want to restore the last used session. To
get rid of the dialog you have to set |g:session_autoload| to true (1).

-------------------------------------------------------------------------------
The *g:loaded_session* option

This variable isn't really an option but if you want to avoid loading the
Expand All @@ -183,23 +195,27 @@ script:
>
:let g:loaded_session = 1
==============================================================================
===============================================================================
*session-compatibility-with-other-plug-ins*
Compatibility with other plug-ins ~

Vim's |:mksession| command isn't fully compatible with plug-ins that create
buffers with generated content and because of this 'session.vim' includes
specific workarounds for such plug-ins:

* NERD tree [6] and Project [7] windows are supported;
* When shell.vim [8] is installed Vim's full-screen state is persisted.
* The netrw (see |netrw-start|) and taglist.vim [9] plug-ins support sessions
out of the box;
- NERD tree [6] and Project [7] windows are supported;

- When shell.vim [8] is installed Vim's full-screen state is persisted;

- The netrw (see |netrw-start|) and taglist.vim [9] plug-ins support sessions
out of the box.

If your favorite plug-in doesn't work with 'session.vim' drop me a mail and
I'll see what I can do. Please include a link to the plug-in in your e-mail so
that I can install and test the plug-in.

==============================================================================
===============================================================================
*session-known-issues*
Known issues ~

Recently this plug-in switched from reimplementing |:mksession| to actually
Expand All @@ -210,21 +226,24 @@ it turns out that bolting on support for these after the fact is going to
complicate the plug-in significantly (in other words, I'm working on it but it
might take a while...)

==============================================================================
===============================================================================
*session-contact*
Contact ~

If you have questions, bug reports, suggestions, etc. the author can be
contacted at peter@peterodding.com. The latest version is available at
http://peterodding.com/code/vim/session/ and http://github.com/xolox/vim-session.
If you like the script please vote for it on Vim Online [10].

==============================================================================
===============================================================================
*session-license*
License ~

This software is licensed under the MIT license [11].
Copyright 2010 Peter Odding <peter@peterodding.com>.
This software is licensed under the MIT license [11]. Copyright 2010 Peter
Odding <peter@peterodding.com>.

==============================================================================
===============================================================================
*sample-session-script*
Sample session script ~

Here's an example session script generated by the 'session.vim' plug-in while
Expand Down Expand Up @@ -323,13 +342,14 @@ I was editing the plug-in itself in Vim:
doautoall SessionLoadPost
unlet SessionLoad
==============================================================================
===============================================================================
*session-references*
References ~

[1] http://peterodding.com/code/vim/session/autosave.png
[2] http://peterodding.com/code/vim/session/autoopen.png
[3] http://peterodding.com/code/vim/session/#sample_session_script
[4] http://peterodding.com/code/vim/downloads/session
[4] http://peterodding.com/code/vim/downloads/session.zip
[5] http://peterodding.com/code/vim/reload/
[6] http://www.vim.org/scripts/script.php?script_id=1658
[7] http://www.vim.org/scripts/script.php?script_id=69
Expand All @@ -338,4 +358,4 @@ References ~
[10] http://www.vim.org/scripts/script.php?script_id=3150
[11] http://en.wikipedia.org/wiki/MIT_License

vim: syntax=help nospell
vim: ft=help
11 changes: 9 additions & 2 deletions plugin/session.vim
@@ -1,8 +1,8 @@
" Vim script
" Author: Peter Odding
" Last Change: May 25, 2011
" Last Change: May 26, 2011
" URL: http://peterodding.com/code/vim/session/
" Version: 1.4.1
" Version: 1.4.2

" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3150 1 :AutoInstall: session.zip
Expand All @@ -22,6 +22,13 @@ if !exists('g:session_autosave')
let g:session_autosave = 0
endif

" The session plug-in can automatically open sessions in three ways: based on
" Vim's server name, by remembering the last used session or by opening the
" session named `default'. Enable this option to use the second approach.
if !exists('g:session_default_to_last')
let g:session_default_to_last = 0
endif

" The default directory where session scripts are stored.
if !exists('g:session_directory')
if xolox#misc#os#is_win()
Expand Down

0 comments on commit 6855a81

Please sign in to comment.