当前位置: 动力学知识库 > 问答 > 编程问答 >

Skip auto loading last session when Vim is launched from git commit

问题描述:

I'm using auto saving and auto loading of sessions in my .vimrc file:

autocmd VimLeave * mksession! ~/.vim/last-session.vim

autocmd VimEnter * source ~/.vim/last-session.vim

During my work sometimes I commit changes. Git automatically opens Vim so I can type the commit message. In that case I want Vim to be opened without loading the last session (and without saving it when I exit).

I thought about editing the core.editor variable in Git, such that Vim will be opened without loading .vimrc. But I do want my custom configuration to be loaded, just skip the above two lines.

For this I imagine that I need some condition in .vimrc to execute these lines only if Vim is not loaded from git commit.

How can this be done?

网友答案:

You may want to take a look at the vim-session plugin. It's a better frontend to session management, but it also implements a couple of interesting features. I was worried about the default session being restored while editing a log message for git commit as well, and was happily surprised that vim-session handled it well. If you launch Vim with a file to be opened, then it does not auto load the default session. The code for that logic is here. I imagine you could put something similar together, and create a function that only loads the session when it's "safe" to do so. However, I really recommend using vim-session if you want this behavior--it's an excellent plugin.

Something a little less generic that you could do is simply prevent loading if Vim comes up and a specific filetype is already applied. In this case, the gitcommit filetype should be in effect, and you can skip loading:

autocmd VimEnter * if &ft != 'gitcommit' | source ~/.vim/last-session.vim | endif
autocmd VimLeave * if &ft != 'gitcommit' | mksession! ~/.vim/last-session.vim | endif

That'll solve the problem you face at the moment, but I think vim-session's behavior is really what you want.

分享给朋友:
您可能感兴趣的文章:
随机阅读: