Skip to content

Commit

Permalink
New 'session_persist_globals' option (requested by Roman Byshko)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Nov 11, 2011
1 parent f56fe44 commit ff71e04
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
22 changes: 20 additions & 2 deletions README.md
Expand Up @@ -30,7 +30,7 @@ This command saves your current editing session just like Vim's built-in [:mkses

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 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`.
Your session script will be saved in the directory pointed to by the `g:session_directory` option.

### The `:OpenSession` command

Expand Down Expand Up @@ -74,14 +74,22 @@ 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], for example:
Because the `session.vim` plug-in uses Vim's [:mksession][mksession] command you can change how it works by setting ['sessionoptions'][sessionoptions] in your [vimrc script] [vimrc], 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

Note that the session.vim plug-in automatically and unconditionally executes the following changes just before saving a session:

" Don't persist options and mappings because it can corrupt sessions.
set sessionoptions-=options

" Always persist Vim's window size.
set sessionoptions+=resize

### The `g:session_directory` option

This option controls the location of your session scripts. Its default value is `~/.vim/sessions` (on UNIX) or `~\vimfiles\sessions` (on Windows). If you 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).
Expand All @@ -98,6 +106,15 @@ By default this option is set to `'prompt'`. When you've opened a session and yo

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 `'yes'`.

### The `g:session_persist_globals` option

The session.vim plug-in uses Vim's [:mksession] [mksession] command but it changes ['sessionoptions'][sessionoptions] so that Vim options and mappings are not persisted. The plug-in does this because persistence of options and mappings can break loading of sessions, in other words it's fragile (in my opinion).

If you want the plug-in to persist specific global variables or options you can add their names to the list `g:session_persist_globals` in your [vimrc script] [vimrc]:

" Persist the value of the global option 'makeprg'.
call add(g:session_persist_globals, '&makeprg')

### The `g:session_restart_environment` option

This option is a list of environment variable names (without the dollar signs) that the `:RestartVim` command will pass on to the new instance of Vim. This option is only useful on UNIX. By default the three environment variables `$TERM`, `$VIM` and `$VIMRUNTIME` are included in this list.
Expand Down Expand Up @@ -231,3 +248,4 @@ Here's an example session script generated by the `session.vim` plug-in while I
[mksession]: http://vimdoc.sourceforge.net/htmldoc/starting.html#:mksession
[source]: http://vimdoc.sourceforge.net/htmldoc/repeat.html#:source
[vimrc]: http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc
[sessionoptions]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27sessionoptions%27
11 changes: 9 additions & 2 deletions autoload/xolox/session.vim
@@ -1,9 +1,9 @@
" Vim script
" Author: Peter Odding
" Last Change: October 1, 2011
" Last Change: November 11, 2011
" URL: http://peterodding.com/code/vim/session/

let g:xolox#session#version = '1.4.20'
let g:xolox#session#version = '1.4.21'

" Public API for session persistence. {{{1

Expand All @@ -20,6 +20,7 @@ function! xolox#session#save_session(commands, filename) " {{{2
call add(a:commands, '')
call add(a:commands, 'set guioptions=' . escape(&go, ' "\'))
call add(a:commands, 'silent! set guifont=' . escape(&gfn, ' "\'))
call xolox#session#save_globals(a:commands)
call xolox#session#save_features(a:commands)
call xolox#session#save_colors(a:commands)
call xolox#session#save_qflist(a:commands)
Expand All @@ -29,6 +30,12 @@ function! xolox#session#save_session(commands, filename) " {{{2
call add(a:commands, '" vim: ft=vim ro nowrap smc=128')
endfunction

function! xolox#session#save_globals(commands) " {{{2
for global in g:session_persist_globals
call add(a:commands, printf("let %s = %s", global, string(eval(global))))
endfor
endfunction

function! xolox#session#save_features(commands) " {{{2
let template = "if exists('%s') != %i | %s %s | endif"
for [global, command] in [
Expand Down
28 changes: 26 additions & 2 deletions doc/session.txt
Expand Up @@ -64,8 +64,8 @@ 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 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|.
Your session script will be saved in the directory pointed to by the
|g:session_directory| option.

-------------------------------------------------------------------------------
The *:OpenSession* command
Expand Down Expand Up @@ -151,6 +151,15 @@ how it works by setting |'sessionoptions'| in your |vimrc| script, for example:
" If you don't want help windows to be restored:
set sessionoptions-=help
Note that the session.vim plug-in automatically and unconditionally executes
the following changes just before saving a session:
>
" Don't persist options and mappings because it can corrupt sessions.
set sessionoptions-=options
" Always persist Vim's window size.
set sessionoptions+=resize
-------------------------------------------------------------------------------
The *g:session_directory* option

Expand Down Expand Up @@ -188,6 +197,21 @@ 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 'yes'.

-------------------------------------------------------------------------------
The *g:session_persist_globals* option

The session.vim plug-in uses Vim's |:mksession| command but it changes
|'sessionoptions'| so that Vim options and mappings are not persisted. The
plug-in does this because persistence of options and mappings can break
loading of sessions, in other words it's fragile (in my opinion).

If you want the plug-in to persist specific global variables or options you
can add their names to the list |g:session_persist_globals| in your |vimrc|
script:
>
" Persist the value of the global option 'makeprg'.
call add(g:session_persist_globals, '&makeprg')
-------------------------------------------------------------------------------
The *g:session_restart_environment* option

Expand Down
7 changes: 6 additions & 1 deletion plugin/session.vim
@@ -1,6 +1,6 @@
" Vim script
" Author: Peter Odding
" Last Change: October 1, 2011
" Last Change: November 11, 2011
" URL: http://peterodding.com/code/vim/session/

" Support for automatic update using the GLVS plug-in.
Expand Down Expand Up @@ -34,6 +34,11 @@ if !exists('g:session_default_to_last')
let g:session_default_to_last = 0
endif

" List with global variables and &options to persist between sessions.
if !exists('g:session_persist_globals')
let g:session_persist_globals = []
endif

" On UNIX the :RestartVim command will pass the following environment
" variables on to the new instance of Vim.
if !exists('g:session_restart_environment')
Expand Down

0 comments on commit ff71e04

Please sign in to comment.