Reputation: 782
I've been customizing my .vimrc
a lot lately and love the power and convenience that :mksession
gives me. I currently have the following in my .vimrc
to autoload sessions:
function! LoadSession()
if argc() == 0 && ! &diff
let g:sessiondir = $HOME . "/.vim/sessions" . getcwd()
let g:sessionfile = g:sessiondir . "/session.vim"
if (filereadable(g:sessionfile))
exe 'source ' g:sessionfile
else
echo "No session loaded." + argc() + argv()
endif
else
let g:sessionfile = ""
let g:sessiondir = ""
call ResCur()
endif
endfunction
I then call this with au VimEnter * nested :call LoadSession()
. This works great for most cases, except when vim is reading from stdin
. In that case the session is still loaded, however I want to prevent that from happening. I would have thought the argc() == 0
conditions would be enough, but it appears that the -
that vim is being called with to read from stdin
causes argc()
to not return 0. Poop! ;]
I've tried all sorts of things from looking at argv(0)
(it's empty in this case - why?), trying to find ways of identifying that vim is reading from stdin
(it shows a message that it's doing so, but I can't figure out how to tap into that), etc., but no luck so far.
I'm sure I'm missing something terribly obvious here, but the Googles and vim :help
isn't getting me anywhere, so I'm hoping some kind soul here can shed some light on this for me.
Upvotes: 4
Views: 373
Reputation: 1096
What I found works is having
autocmd StdinReadPre * let g:my_is_stdin = 1
in your .vimrc
and then test for exists("g:my_is_stdin")
in your session saving/loading functions. Mind that these have to be run also via autocmd
on events VimLeave
/VimEnter
for this scheme to work.
Upvotes: 3
Reputation: 172580
The session.vim plugin that I'm using offers extended session handling. Among others, it asks whether a previously saved session should be restored on Vim startup.
But unless you need the other functionality of the plugin, your workaround with mappings triggering the restore is probably fine, too.
Upvotes: 1
Reputation: 38543
I've been fiddling a lot with loading sessions on Vim startup, and ultimately decided that's it's not a very good idea, mainly because it doesn't work well with plugins.
I ultimately added some mappings for saving and restoring a session. This has the bonus that you don't have to mess arround with your session when you're doing quick edits.
map <leader>ss :call CustomSessionSave()<CR>
map <leader>sl :call CustomSessionRestore()<CR>
map <leader>sd :call CustomSessionDelete()<CR>
Maybe this helps
Upvotes: 0