有人知道每次调用 vim 时如何强制 .vimrc 自动打开 NERDTree 吗?操作系统是*nix。
au VimEnter * NERDTree
在你的 vimrc 中应该这样做
:he autocmd.txt 作为背景
也可以只在命令行没有文件的情况下打开Nerd Tree:
function! StartUp()
if 0 == argc()
NERDTree
end
endfunction
autocmd VimEnter * call StartUp()
取自 Ovid 的博客文章。
在没有提供文件参数的情况下打开 NERDTree 的方式是
autocmd vimenter * if !argc() | NERDTree | endif
OR
au vimenter * if !argc() | NERDTree | endif
上面的代码只是检查是否没有提供参数然后打开
NERDTree
.
基于@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()
如果您正在寻找一种持久的 NERDTree 方法,即使您打开新选项卡时它仍然存在,您最好使用 jistr/vim-nerdtree-tabs 并添加您的
.vimrc
:
let g:nerdtree_tabs_open_on_console_startup=1
这个包不再维护了,但它可以工作,我不知道有什么等价物。
在你的 vim 配置文件中(我使用 nvim,所以对我来说它在
~/.config/nvim/init.vim
中),au VimEnter * NERDTree
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