可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 behaviour?
回答1:
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.
回答2:
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
回答3:
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/.
回答4:
Or you could use Vimpanel, it has session support built in, among other features.
回答5:
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"
回答6:
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>)