在vim中自动打开NERDTree

发布于 2024-08-11 05:57:23 字数 59 浏览 10 评论 0 原文

有人知道如何强制 .vimrc 每次调用 vim 时自动打开 NERDTree 吗?操作系统是*nix。

Does someone know how to force .vimrc to auto-open NERDTree each time vim is invoked? The operation system is *nix.

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

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

发布评论

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

评论(7

傾城如夢未必闌珊 2024-08-18 05:57:23
 au VimEnter *  NERDTree

在你的 vimrc 中应该这样做

:he autocmd.txt 作为背景

 au VimEnter *  NERDTree

in your vimrc should do it

:he autocmd.txt for background

自在安然 2024-08-18 05:57:23

您也可以仅在命令行上没有文件时打开 Nerd Tree:

function! StartUp()
    if 0 == argc()
        NERDTree
    end
endfunction

autocmd VimEnter * call StartUp()

取自 奥维德的博客文章

You can also only open Nerd Tree when there was no file on the command line:

function! StartUp()
    if 0 == argc()
        NERDTree
    end
endfunction

autocmd VimEnter * call StartUp()

Taken from a blog post by Ovid.

执手闯天涯 2024-08-18 05:57:23

当没有提供文件参数时打开 NERDTree 的一个衬垫是

autocmd vimenter * if !argc() | NERDTree | endif
OR
au vimenter * if !argc() | NERDTree | endif

上面的代码只是检查是否没有提供参数然后打开 NERDTree。

One liner to open NERDTree when no file argument provided would be

autocmd vimenter * if !argc() | NERDTree | endif
OR
au vimenter * if !argc() | NERDTree | endif

The above code just checks if no argument is provided then open NERDTree.

怕倦 2024-08-18 05:57:23

基于@zoul的答案,在我的例子中,如果我指定一个目录或者什么都不指定,我希望 NERDTree 默认打开,如果我指定单个文件则不打开,所以我最终得到:

function! StartUp()
    if !argc() && !exists("s:std_in")
        NERDTree
    end
    if argc() && isdirectory(argv()[0]) && !exists("s:std_in")
        exe 'NERDTree' argv()[0]
        wincmd p
        ene
    end
endfunction

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * call StartUp()

Building on @zoul's answer, I in my case I wanted NERDTree to be open by default if I specify a directory or if I specify nothing, and not be open if I specify a single file, so I ended up with:

function! StartUp()
    if !argc() && !exists("s:std_in")
        NERDTree
    end
    if argc() && isdirectory(argv()[0]) && !exists("s:std_in")
        exe 'NERDTree' argv()[0]
        wincmd p
        ene
    end
endfunction

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * call StartUp()
陌路终见情 2024-08-18 05:57:23

如果您正在寻找一种方法来拥有持久的 NERDTree,即使您打开新选项卡,它仍然存在,您最好使用 jistr/vim-nerdtree-tabs 并添加到您的 .vimrc 中:

let g:nerdtree_tabs_open_on_console_startup=1

该包不再维护,但它可以工作,我不知道任何等效项。

If you are looking for a way to have a persistent NERDTree, that remains even when you open new tabs, you'd better use jistr/vim-nerdtree-tabs and add in your .vimrc :

let g:nerdtree_tabs_open_on_console_startup=1

The package is not maintained anymore, but it works and I don't know any equivalent.

苏别ゝ 2024-08-18 05:57:23

NERDTree文档中有官方答案。

https://github.com/ preservim/nerdtree#vim 启动时如何自动打开 nerdtree

" Start NERDTree and leave the cursor in it.
autocmd VimEnter * NERDTree

" Start NERDTree and put the cursor back in the other window.
autocmd VimEnter * NERDTree | wincmd p

" Start NERDTree when Vim is started without file arguments.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif

" Start NERDTree. If a file is specified, move the cursor to its window.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif

" Start NERDTree, unless a file or session is specified, eg. vim -S session_file.vim.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') && v:this_session == '' | NERDTree | endif

" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif

There is an official answer in NERDTree Document.

https://github.com/preservim/nerdtree#how-do-i-open-nerdtree-automatically-when-vim-starts

" Start NERDTree and leave the cursor in it.
autocmd VimEnter * NERDTree

" Start NERDTree and put the cursor back in the other window.
autocmd VimEnter * NERDTree | wincmd p

" Start NERDTree when Vim is started without file arguments.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') | NERDTree | endif

" Start NERDTree. If a file is specified, move the cursor to its window.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif

" Start NERDTree, unless a file or session is specified, eg. vim -S session_file.vim.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists('s:std_in') && v:this_session == '' | NERDTree | endif

" Start NERDTree when Vim starts with a directory argument.
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') |
    \ execute 'NERDTree' argv()[0] | wincmd p | enew | execute 'cd '.argv()[0] | endif
铜锣湾横着走 2024-08-18 05:57:23

在你的 vim 配置文件中(我使用 nvim,所以对我来说它在 ~/.config/nvim/init.vim 中),
在文件中的任意位置添加以下行:
au VimEnter * NERDTree

In your vim config file (I use nvim, so for me its in ~/.config/nvim/init.vim),
Add the line anywhere in the file:
au VimEnter * NERDTree

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