如果 NERDTree 是最后且唯一的缓冲区,则自动退出 vim

发布于 2024-08-18 01:50:57 字数 694 浏览 7 评论 0原文

我的 .vimrc 中有以下内容:

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Open NERDTree by default
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p

因此,

% vim file.txt

打开 NERDTree 并将光标聚焦在 file.txt 缓冲区中。我进行编辑,然后在缓冲区上点击 :q ,然后剩下 . 。 。书呆子树。这很烦人。

我可以使用 :qa 关闭所有缓冲区,然后退出 vim,但我习惯了 :q 转义。所以我想知道是否有一种方法可以检测唯一剩余的缓冲区是 NERDTree,并“统一”这两个缓冲区,以达到以下目的:q

编辑

询问,你们将收到: https://github.com/scrooloose/nerdtree/issues#issue/21

I have the following in my .vimrc:

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Open NERDTree by default
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p

So,

% vim file.txt

opens NERDTree and focuses the cursor in the file.txt buffer. I make my edits, and hit :q on the buffer, and I'm left with . . . NERDTree. This is annoying.

I could use :qa to close all buffers, and exit vim, but I'm used to the :q trope. So I'm wondering if there's a way to detect that the only remaining buffer is NERDTree, and "unify" the two buffers, for purposes of :q

Edit

Ask and ye shall receive: https://github.com/scrooloose/nerdtree/issues#issue/21

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

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

发布评论

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

评论(5

过潦 2024-08-25 01:50:57

NERDTree 问题列表上已发布了用于执行此操作的脚本。在 GitHub 上查看 nerdtree 的 issue-21

这将导致您的 vimrc 此处使用单行命令:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

A script to do exactly this has been posted on the NERDTree issue list. Checkout issue-21 on GitHub for nerdtree.

This leads to the single line command for your vimrc here:

autocmd bufenter * if (winnr("
quot;) == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
将军与妓 2024-08-25 01:50:57
function! s:CloseIfOnlyControlWinLeft()
  if winnr("$") != 1
    return
  endif
  if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
        \ || &buftype == 'quickfix'
    q
  endif
endfunction
augroup CloseIfOnlyControlWinLeft
  au!
  au BufEnter * call s:CloseIfOnlyControlWinLeft()
augroup END

来自我的 vimrc,基于 janus 仓库

增强功能:如果仅留下快速修复窗口,也可以关闭。
它使用 BufEnter 自动命令来代替,这是 &bt 正常工作所必需的。

function! s:CloseIfOnlyControlWinLeft()
  if winnr("$") != 1
    return
  endif
  if (exists("t:NERDTreeBufName") && bufwinnr(t:NERDTreeBufName) != -1)
        \ || &buftype == 'quickfix'
    q
  endif
endfunction
augroup CloseIfOnlyControlWinLeft
  au!
  au BufEnter * call s:CloseIfOnlyControlWinLeft()
augroup END

From my vimrc, based on a version from janus repo.

Enhancements: also close if only a quickfix window is left.
It uses the BufEnter autocommand instead, which is required for &bt to work properly.

淡淡绿茶香 2024-08-25 01:50:57

需要实现的想法:

您可以编写一个函数,在调用时检查唯一剩余的缓冲区(或者可能是唯一的非帮助缓冲区,如果您愿意的话)是否是 NERDTree 缓冲区,如果是,则删除它(或就退出了)。

然后,每当缓冲区被删除/隐藏/无论您实际发生什么时,都让自动命令运行它:q(承认我不完全确定,这让我感到羞愧!)。

An idea in need of implementation:

You could write a function which, when called, checks if the only buffer remaining (or perhaps the only non-help buffer, if you prefer) is a NERDTree buffer and, if so, deletes it (or just quits).

Then have an autocmd run it whenever a buffer is deleted / hidden / whatever actually happens when you :q (it shames me to admit I'm not entirely sure!).

后知后觉 2024-08-25 01:50:57

您可以 :cabbrv q qa 但我建议不要这样做,因为当您真正想要 q 时您会忘记它。

You could :cabbrv q qa but I'd advise against that because you'll forget about it when you actually want q.

柠栀 2024-08-25 01:50:57

我喜欢这样做:cmap bq :bufdo q 在命令模式下通过两次按键关闭所有缓冲区。

I like to do this: cmap bq :bufdo q<CR> to close all buffers with two keystrokes in command mode.

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