Prevent vim from loading sessions while reading from stdin

Recently, I have tuned my .vimrc lot and loved the power and convenience that gives me :mksession . I currently have .vimrc for startup 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 

Then I call this with au VimEnter * nested :call LoadSession() . This works great for most cases, except when vim reads with stdin . In this case, the session is still loaded, however I want this to not happen. I would have thought that the conditions argc() == 0 would be enough, but it seems that - , called by vim with a read from stdin , calls argc() so as not to return 0. Poop !;]

I tried all kinds of views from argv(0) (it is empty in this case - why?), Trying to find ways to determine what vim is reading from stdin (it shows a message saying that it does so, but I can not understand how to use it) etc., but so far no luck.

I'm sure I missed something terribly obvious here, but Googles and vim :help won't take me anywhere, so I hope some kind-hearted soul can shed some light on this for me.

+4
source share
3 answers

the session.vim plugin that I use offers advanced session handling. Among other things, he asks if a previously saved session should be restored when starting Vim.

But if you don’t need other functions of the plugin, your workaround with mappings that trigger recovery is probably fine too.

+1
source

What I found has

 autocmd StdinReadPre * let g:my_is_stdin = 1 

in .vimrc , and then check exists("g:my_is_stdin") for the session save / load function. Keep in mind that they must also be triggered through autocmd for VimLeave / VimEnter for this to work.

+3
source

I did a lot of loading Vim sessions, and eventually decided that this was not a good idea, mainly because it did not work with plugins.

I eventually added some mappings to save and restore the session. It has a bonus that you don’t have to bother with your session when you make quick changes.

 map <leader>ss :call CustomSessionSave()<CR> map <leader>sl :call CustomSessionRestore()<CR> map <leader>sd :call CustomSessionDelete()<CR> 

Maybe it helps

0
source

All Articles