4

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I'm trying out session.vim right now, can't make the autoload work the way I want to yet, but this is looking promising, thanks.
0

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

2 Comments

Thanks, but I am looking for an autoloading solution. By the way: what are those CustomSessionXXX() functions you're calling?
nothing special, I'm just closing NERDTree before calling mksession (on Save) and then opening it on Restore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.