Vim、NERDtree 在会话恢复中未恢复

发布于 2024-10-03 17:57:49 字数 168 浏览 3 评论 0原文

当我有一个 NERDtree 面板并保存一个 Vim 会话(mksession [文件名]),然后打开该会话(vim -S 文件名)时,该面板会打开并标记为“NERDtree”,但不会填充。如果我从命令行尝试“: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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南街女流氓 2024-10-10 17:57:49

我刚刚决定自己解决这个问题。就我而言,当我在 vimrc 中使用以下内容退出 Vim 时,会创建会话:

autocmd VimLeave * mksession! [filename]

当 Vim 使用以下命令打开时,我还尝试自动打开 NERDTree:

autocmd VimEnter * NERDTree

结果是我的会话使用两个 NERDTree 实例打开,如原始帖子中所述。我的解决方案是在保存会话之前简单地关闭 NERDTree,这样我的自动打开调用只会打开 NERDTree 的一个实例。

我的解决方案

" Save session on quitting Vim
autocmd VimLeave * NERDTreeClose
autocmd VimLeave * mksession! [filename]

" Restore session on starting Vim
autocmd VimEnter * call MySessionRestoreFunction()
autocmd VimEnter * NERDTree

到目前为止,它对我来说就像一个魅力。希望这有帮助。

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.

是你 2024-10-10 17:57:49

我遇到了同样的问题,在我的研究过程中我发现了两个解决方案:

你可以使用一个名为“session.vim”的插件,它对恢复 NERDTree 提供基本支持。您可以在这里找到它: http://www.vim.org/scripts/ script.php?script_id=3150

我自己发现这个插件不适合我,所以我采取了另一种方法。
您可以将 vim 配置为自动将缓冲区目录设置为等于您的工作目录。

autocmd BufEnter * lcd %:p:h

由于 NERDTree 在您第一次打开时会打开工作目录,因此您已经位于您想要的位置了!

在这种情况下,只需在打开文件或会话后打开 NERDTree 即可。

但是,由于该魔法仅在一个选项卡中第一次起作用,因此您可以使用以下命令让 NERDTree 在树中找到您的文件。

map <leader>r :NERDTreeFind<cr>

要整理树,只需使用“p”命令根据需要向上移动几个目录,然后输入“C”。

感谢这篇文章中的人,我找到了这些命令:

https://superuser.com/questions/195022/vim-how-to-synchronize-nerdtree-with-current-opened-tab-file-path

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

巨坚强 2024-10-10 17:57:49

要使用会话插件修复 NERDTress 会话,应使用会话插件中嵌入的新会话命令:“SaveSession”和“OpenSession”,而不是“mksession”和“source”。

根据会话插件作者的评论:
Vim 的 :mksession 命令与使用生成内容创建缓冲区的插件并不真正兼容,因此 session.vim 包含此类插件的特定解决方法:
•支持BufExplorer、Project和NERD树形窗口;
•安装shell.vim后,Vim的全屏状态保持不变;
•netrw 和taglist.vim 插件支持开箱即用的会话。

也许这就是为什么我们应该使用新命令来克服 NERDTree 会话问题。

更多详情请参考 http://peterodding.com/code/vim/session/

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

著墨染雨君画夕 2024-10-10 17:57:49

或者您可以使用 Vimpanel,它内置了会话支持等特征。

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

挥剑断情 2024-10-10 17:57:49

另一个基于 stevelove 的解决方案:

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>)

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>)
绿萝 2024-10-10 17:57:49

让 NERDTree 在会话中按预期运行的最简单方法可能是修补 NERD_tree.vim,将自动命令添加到 NERDTree 自动命令组中(就在 augroup NERDTree 行之后):

exec "autocmd BufEnter ". s:NERDTreeBufName ."* call <SID>initNerdTreeInPlace(\"\")"

它没有经过广泛测试,但是这个似乎对我来说适用于多种布局和打开不同数量的 NERDTree 窗口。

编辑:实际上,这效果不太好,因为 NERDTree 有主窗口和辅助窗口,并且会话文件中没有指示 NERDTree 缓冲区是其中之一。看起来向插件添加对此的可靠支持并不是那么大的挑战,但它不仅仅是微不足道的。同时,对于简单的情况,添加以下自动命令(而不是上面的命令)可能会满足您的要求:

exec "autocmd BufEnter ". s:NERDTreeBufName ."* :NERDTreeToggle | :NERDTreeToggle"

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"
柏林苍穹下 2024-10-10 17:57:49

就我而言,加载会话后打开了 NERDTree,但它没有显示任何文件。以下脚本修复了这个问题:

" 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

即:

  • 保存会话之前 - 加载会话后关闭 NERDTree
  • - 打开 NERDTree(现在有两个),使用 wincmd l 转到另一个,然后使用 关闭wincmd q

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
通知家属抬走 2024-10-10 17:57:49

例子:

" 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文