Vim, NERDtree not recovered in session restore
Asked Answered
A

8

28

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?

Astrogation answered 19/11, 2010 at 16:11 Comment(0)
M
28

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.

Mallett answered 28/9, 2011 at 22:55 Comment(3)
simple and clear. btw, I used the session restore code here: #5142599Arginine
Instead of NERDTreeClose, I prefer using tabs NERDTreeTabsClose method used by vim-nerdtree-tabs. This combination also works flawlessly for multiple tabs sessions.Karissakarita
``` " Save session on quitting Vim autocmd VimLeave * NERDTreeClose autocmd VimLeave * mksession! ~/mysession.vim " Start NERDTree when Vim starts with a directory argument. autocmd StdinReadPre * let s:std_in=1 autocmd VimEnter * source ~/mysession.vim autocmd VimEnter * NERDTree ```Entomophilous
P
6

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

Permafrost answered 12/6, 2011 at 10:2 Comment(1)
+1 For: "session.vim", which has a basic support what is true! And besides session.vim has an autorestore last session option. Exactly what I want...Hunfredo
M
4

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

Meloniemelony answered 26/6, 2012 at 9:52 Comment(0)
C
2

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

Calcaneus answered 23/1, 2013 at 11:40 Comment(0)
L
2

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>)
Longhair answered 25/2, 2017 at 21:59 Comment(0)
T
1

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"
Tarkington answered 11/3, 2011 at 9:26 Comment(0)
W
1

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 with wincmd q
Waxler answered 5/5, 2022 at 22:46 Comment(0)
D
0

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
Delineator answered 29/6, 2022 at 15:30 Comment(2)
:NERDTreeClose | mksession! ~/mysession.vim | NERDTreeEntomophilous
:NERDTreeClose | source ~/mysession.vim | NERDTreeEntomophilous

© 2022 - 2024 — McMap. All rights reserved.