Skip to content

Commit 6855a81

Browse files
committed
Enable opening last used session when starting Vim (thanks to James Cox for suggestion)
1 parent 99d49ae commit 6855a81

File tree

4 files changed

+114
-54
lines changed

4 files changed

+114
-54
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Execute this command to view the Vim script generated for a session. This comman
7474

7575
### The `sessionoptions` setting
7676

77-
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]:
77+
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:
7878

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

9595
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.
9696

97+
### The `g:session_default_to_last` option
98+
99+
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).
100+
97101
### The `g:loaded_session` option
98102

99103
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]:
@@ -105,8 +109,8 @@ This variable isn't really an option but if you want to avoid loading the `sessi
105109
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:
106110

107111
* [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;
108-
* When [shell.vim](http://peterodding.com/code/vim/shell/) is installed Vim's full-screen state is persisted.
109-
* 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;
112+
* When [shell.vim](http://peterodding.com/code/vim/shell/) is installed Vim's full-screen state is persisted;
113+
* 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.
110114

111115
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.
112116

autoload/xolox/session.vim

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim script
22
" Author: Peter Odding
3-
" Last Change: May 12, 2011
3+
" Last Change: May 26, 2011
44
" URL: http://peterodding.com/code/vim/session/
55

66
let s:script = expand('<sfile>:p:~')
@@ -187,12 +187,14 @@ function! xolox#session#auto_load() " {{{2
187187
endif
188188
endfor
189189
endif
190-
" Check whether the default session should be loaded.
191-
let path = xolox#session#name_to_path('default')
190+
" Default to the last used session or the session named `default'?
191+
let session = s:last_session_recall()
192+
let path = xolox#session#name_to_path(session)
192193
if filereadable(path) && !s:session_is_locked(path)
193-
let msg = "Do you want to restore your default editing session?"
194-
if s:prompt(msg, 'g:session_autoload')
195-
OpenSession default
194+
let msg = "Do you want to restore your %s editing session?"
195+
let label = session != 'default' ? 'last used' : 'default'
196+
if s:prompt(printf(msg, label), 'g:session_autoload')
197+
execute 'OpenSession' fnameescape(session)
196198
endif
197199
endif
198200
endif
@@ -280,6 +282,7 @@ function! xolox#session#open_cmd(name, bang) abort " {{{2
280282
call s:lock_session(path)
281283
execute 'source' fnameescape(path)
282284
unlet! s:session_is_dirty
285+
call s:last_session_persist(name)
283286
call xolox#misc#timer#stop("%s: Opened %s session in %s.", s:script, string(name), starttime)
284287
call xolox#misc#msg#info("%s: Opened %s session from %s.", s:script, string(name), fnamemodify(path, ':~'))
285288
endif
@@ -315,6 +318,7 @@ function! xolox#session#save_cmd(name, bang) abort " {{{2
315318
let msg = "%s: Failed to save %s session to %s!"
316319
call xolox#misc#msg#warn(msg, s:script, string(name), friendly_path)
317320
else
321+
call s:last_session_persist(name)
318322
call xolox#misc#timer#stop("%s: Saved %s session in %s.", s:script, string(name), starttime)
319323
call xolox#misc#msg#info("%s: Saved %s session to %s.", s:script, string(name), friendly_path)
320324
let v:this_session = path
@@ -477,6 +481,31 @@ function! xolox#session#complete_names(arg, line, pos) " {{{2
477481
return map(names, 'fnameescape(v:val)')
478482
endfunction
479483

484+
" Default to last used session: {{{2
485+
486+
function! s:last_session_file()
487+
let directory = xolox#misc#path#absolute(g:session_directory)
488+
return xolox#misc#path#merge(directory, 'last-session.txt')
489+
endfunction
490+
491+
function! s:last_session_persist(name)
492+
if g:session_default_to_last
493+
if writefile([a:name], s:last_session_file()) != 0
494+
call xolox#misc#msg#warn("Failed to persist name of last used session!")
495+
endif
496+
endif
497+
endfunction
498+
499+
function! s:last_session_recall()
500+
if g:session_default_to_last
501+
let fname = s:last_session_file()
502+
if filereadable(fname)
503+
return readfile(fname)[0]
504+
endif
505+
endif
506+
return 'default'
507+
endfunction
508+
480509
" Lock file management: {{{2
481510

482511
if !exists('s:lock_files')

doc/session.txt

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ and the files they contain.
99
To persist your current editing session you can execute the |:SaveSession|
1010
command. If you don't provide a name for the session 'default' is used. You're
1111
free to use whatever characters you like in session names. When you want to
12-
restore your session simply execute |:OpenSession|. Again the name 'default' is
13-
used if you don't provide one. When a session is active, has been changed and
14-
you quit Vim you'll be prompted whether you want to save the open session
12+
restore your session simply execute |:OpenSession|. Again the name 'default'
13+
is used if you don't provide one. When a session is active, has been changed
14+
and you quit Vim you'll be prompted whether you want to save the open session
1515
before quitting Vim:
1616

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

38-
==============================================================================
39+
===============================================================================
40+
*session-installation*
3941
Installation ~
4042

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

47-
==============================================================================
49+
===============================================================================
50+
*session-commands*
4851
Commands ~
4952

50-
------------------------------------------------------------------------------
53+
-------------------------------------------------------------------------------
5154
The *:SaveSession* command
5255

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

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

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

67-
------------------------------------------------------------------------------
70+
-------------------------------------------------------------------------------
6871
The *:OpenSession* command
6972

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

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

92-
------------------------------------------------------------------------------
95+
-------------------------------------------------------------------------------
9396
The *:RestartVim* command
9497

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

106-
------------------------------------------------------------------------------
109+
-------------------------------------------------------------------------------
107110
The *:CloseSession* command
108111

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

117-
------------------------------------------------------------------------------
120+
-------------------------------------------------------------------------------
118121
The *:DeleteSession* command
119122

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

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

129-
------------------------------------------------------------------------------
131+
-------------------------------------------------------------------------------
130132
The *:ViewSession* command
131133

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

136-
==============================================================================
138+
===============================================================================
139+
*session-options*
137140
Options ~
138141

139-
------------------------------------------------------------------------------
142+
-------------------------------------------------------------------------------
140143
The *sessionoptions* setting
141144

142145
Because the 'session.vim' plug-in uses Vim's |:mksession| command you can change
143-
how it works by setting ||sessionoptions|| in your |vimrc| script:
146+
how it works by setting |'sessionoptions'| in your |vimrc| script, for example:
144147
>
145148
" If you only want to save the current tab page:
146149
set sessionoptions-=tabpages
147-
>
150+
148151
" If you don't want help windows to be restored:
149152
set sessionoptions-=help
150153
151-
------------------------------------------------------------------------------
154+
-------------------------------------------------------------------------------
152155
The *g:session_directory* option
153156

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

160-
------------------------------------------------------------------------------
163+
-------------------------------------------------------------------------------
161164
The *g:session_autoload* option
162165

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

169-
------------------------------------------------------------------------------
172+
-------------------------------------------------------------------------------
170173
The *g:session_autosave* option
171174

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

177-
------------------------------------------------------------------------------
180+
-------------------------------------------------------------------------------
181+
The *g:session_default_to_last* option
182+
183+
By default this option is set to false (0). When you set this option to true
184+
(1) and you start Vim, the session plug-in will open your last used session
185+
instead of the default session. Note that the session plug-in will still show
186+
you the dialog asking whether you want to restore the last used session. To
187+
get rid of the dialog you have to set |g:session_autoload| to true (1).
188+
189+
-------------------------------------------------------------------------------
178190
The *g:loaded_session* option
179191

180192
This variable isn't really an option but if you want to avoid loading the
@@ -183,23 +195,27 @@ script:
183195
>
184196
:let g:loaded_session = 1
185197
186-
==============================================================================
198+
===============================================================================
199+
*session-compatibility-with-other-plug-ins*
187200
Compatibility with other plug-ins ~
188201

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

193-
* NERD tree [6] and Project [7] windows are supported;
194-
* When shell.vim [8] is installed Vim's full-screen state is persisted.
195-
* The netrw (see |netrw-start|) and taglist.vim [9] plug-ins support sessions
196-
out of the box;
206+
- NERD tree [6] and Project [7] windows are supported;
207+
208+
- When shell.vim [8] is installed Vim's full-screen state is persisted;
209+
210+
- The netrw (see |netrw-start|) and taglist.vim [9] plug-ins support sessions
211+
out of the box.
197212

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

202-
==============================================================================
217+
===============================================================================
218+
*session-known-issues*
203219
Known issues ~
204220

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

213-
==============================================================================
229+
===============================================================================
230+
*session-contact*
214231
Contact ~
215232

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

221-
==============================================================================
238+
===============================================================================
239+
*session-license*
222240
License ~
223241

224-
This software is licensed under the MIT license [11].
225-
Copyright 2010 Peter Odding <peter@peterodding.com>.
242+
This software is licensed under the MIT license [11]. Copyright 2010 Peter
243+
Odding <peter@peterodding.com>.
226244

227-
==============================================================================
245+
===============================================================================
246+
*sample-session-script*
228247
Sample session script ~
229248

230249
Here's an example session script generated by the 'session.vim' plug-in while
@@ -323,13 +342,14 @@ I was editing the plug-in itself in Vim:
323342
doautoall SessionLoadPost
324343
unlet SessionLoad
325344
326-
==============================================================================
345+
===============================================================================
346+
*session-references*
327347
References ~
328348

329349
[1] http://peterodding.com/code/vim/session/autosave.png
330350
[2] http://peterodding.com/code/vim/session/autoopen.png
331351
[3] http://peterodding.com/code/vim/session/#sample_session_script
332-
[4] http://peterodding.com/code/vim/downloads/session
352+
[4] http://peterodding.com/code/vim/downloads/session.zip
333353
[5] http://peterodding.com/code/vim/reload/
334354
[6] http://www.vim.org/scripts/script.php?script_id=1658
335355
[7] http://www.vim.org/scripts/script.php?script_id=69
@@ -338,4 +358,4 @@ References ~
338358
[10] http://www.vim.org/scripts/script.php?script_id=3150
339359
[11] http://en.wikipedia.org/wiki/MIT_License
340360

341-
vim: syntax=help nospell
361+
vim: ft=help

plugin/session.vim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
" Vim script
22
" Author: Peter Odding
3-
" Last Change: May 25, 2011
3+
" Last Change: May 26, 2011
44
" URL: http://peterodding.com/code/vim/session/
5-
" Version: 1.4.1
5+
" Version: 1.4.2
66

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

25+
" The session plug-in can automatically open sessions in three ways: based on
26+
" Vim's server name, by remembering the last used session or by opening the
27+
" session named `default'. Enable this option to use the second approach.
28+
if !exists('g:session_default_to_last')
29+
let g:session_default_to_last = 0
30+
endif
31+
2532
" The default directory where session scripts are stored.
2633
if !exists('g:session_directory')
2734
if xolox#misc#os#is_win()

0 commit comments

Comments
 (0)