Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim, NERDtree not recovered in session restore

Tags:

vim

nerdtree

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?

like image 407
Erez Avatar asked Nov 19 '10 16:11

Erez


5 Answers

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.

like image 74
stevelove Avatar answered Nov 17 '22 20:11

stevelove


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

like image 25
Julian Weimer Avatar answered Nov 17 '22 19:11

Julian Weimer


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/.

like image 4
Embert Avatar answered Nov 17 '22 19:11

Embert


Or you could use Vimpanel, it has session support built in, among other features.

like image 2
mihai Avatar answered Nov 17 '22 20:11

mihai


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>)
like image 2
divx Avatar answered Nov 17 '22 19:11

divx