Vim, NERDtree not recovered in session restore
When I have a NERDtree panel and I save a Vim session (mksession [filename]), then open the session (vim -S filename), the panel is opened and tagged "NERDtree" but is not populated. If I try ":NERDtree" from the commandline, the window does get populated, but another panel now opens.
Any ideas wrt this weird behaviou开发者_运维知识库r?
Just decided to deal with this very issue myself. In my case, the session is created when I quit Vim with the following in my vimrc:
autocmd VimLeave * mksession! [filename]
I was also trying to open NERDTree automatically when Vim opened with:
autocmd VimEnter * NERDTree
The result was that my session opened with two instances of NERDTree, like described in the original post. My solution was to simply close NERDTree before saving the session, that way my auto-open call would only open the one instance of NERDTree.
My Solution
" Save session on quitting Vim
autocmd VimLeave * NERDTreeClose
autocmd VimLeave * mksession! [filename]
" Restore session on starting Vim
autocmd VimEnter * call MySessionRestoreFunction()
autocmd VimEnter * NERDTree
It's working like a charm for me so far. Hope this helps.
I had the same problem and during my research i found two solutions:
You can use a plugin called "session.vim", which has a basic support for restoring the NERDTree. You can find it here: http://www.vim.org/scripts/script.php?script_id=3150
I found out for myself that this plugin is not for me, so i took another approach. You can configure vim to automatically set the directory of your buffer equal to your working directory.
autocmd BufEnter * lcd %:p:h
Since the NERDTree opens the working directory when you open it up the first time, you are already where you want to be!
Just open the NERDTree after you opened up your file or your session in this case.
However, since the magic will work only for the first time within one tab, you can use the following command to let the NERDTree find your file in the tree.
map <leader>r :NERDTreeFind<cr>
To unclutter the tree just go up a few directorys as you wish using the "p" command and then type "C".
I found out the commands thanks to the guys in this post:
https://superuser.com/questions/195022/vim-how-to-synchronize-nerdtree-with-current-opened-tab-file-path
To fix the NERDTress session with session plugin, the new session commands embedded in session plugin: "SaveSession" and "OpenSession" should be used, instead of "mksession" and "source".
According to the session plugin author's comments: Vim’s :mksession command isn’t really compatible with plug-ins that create buffers with generated content and because of this session.vim includes specific workarounds for such plug-ins: •BufExplorer, Project and NERD tree windows are supported; •When shell.vim is installed Vim’s full-screen state is persisted; •The netrw and taglist.vim plug-ins support sessions out of the box.
Maybe it is why we should use the new commands to overcome NERDTree session issue.
For more details, please refer to http://peterodding.com/code/vim/session/.
Or you could use Vimpanel, it has session support built in, among other features.
Another solution based on stevelove's:
fun! Mksession(name)
let need_tree = g:NERDTree.IsOpen()
NERDTreeClose
execute "mksession! " . a:name
if need_tree
call writefile(readfile(a:name)+['NERDTree'], a:name)
NERDTree
endif
endfun
command! -nargs=1 Mksession call Mksession(<f-args>)
The easiest way to make NERDTree act as expected with sessions is probably to patch NERD_tree.vim, adding an autocommand to the NERDTree autocommand group (right after the augroup NERDTree
line):
exec "autocmd BufEnter ". s:NERDTreeBufName ."* call <SID>initNerdTreeInPlace(\"\")"
It's not extensively tested, but this seems to work for me with several layouts and with different numbers of NERDTree windows open.
Edit: Actually, this doesn't work so great because NERDTree has primary and secondary windows, and there's no indication in the session file whether a NERDTree buffer is one or the other. It looks like adding reliable support for this to the plugin wouldn't be that big a challenge, but it's more than trivial. In the meantime, for simple cases, adding the following autocommand (instead of the one above) might do what you want:
exec "autocmd BufEnter ". s:NERDTreeBufName ."* :NERDTreeToggle | :NERDTreeToggle"
In my case, the NERDTree was opened after loading a session, but it didn't display any files. The following script fixed that:
" Automatically save the session when leaving vim
set sessionoptions=blank,buffers,curdir,help,tabpages,winsize
autocmd VimLeave * NERDTreeClose
autocmd! VimLeave * mksession! ~/Session.vim
" Automatically load the session when entering vim when no arguments were provided
if argc() == 0 && filereadable(expand('~/Session.vim'))
autocmd! VimEnter * source ~/Session.vim
autocmd VimEnter * :NERDTreeToggle | wincmd l | wincmd q
endif
i.e.:
- before saving session - close the NERDTree
- after loading session - open the NERDTree (now there are two), go to the other with
wincmd l
and close with withwincmd q
Example:
" Save session on quitting Vim but we have to first close NERDTree
autocmd VimLeave * NERDTreeClose
autocmd VimLeave * mksession! ~/mysession.vim
" Open session first and then start NERDTree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * source ~/mysession.vim
autocmd VimEnter * NERDTree
精彩评论