vim使用NERDTree之后打开目录出现两个目录树窗口

版本号:

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 24 2021 14:13:28)
macOS version - x86_64

截图

请添加图片描述

原因:

vim有自带的 Netrw 文件浏览器,打开目录的时候默认会开启该窗口;因为只有Netrw支持打开远程文件,所以需要处理 NERDTree 和Netrw 的兼容问题。

解决:

NERDTree本身有兼容 Netrw 的方法:
NERDTree README
NERDTree and Netrw

但其中会有一个问题:使用这种方式会导致用vim命令创建新文件时无法设置文件名:

问题issue

通过修改NERDTree提供的.vimrc来暂时解决这个问题:

" Function to open the file or NERDTree or netrw.
"   Returns: 1 if either file explorer was opened; otherwise, 0.
function! s:OpenFileOrExplorer(...)
    if a:0 == 0 || a:1 == ''
        NERDTree
    elseif filereadable(a:1)
"        execute 'edit '.a:1
        return 0
    elseif a:1 =~? '^\(scp\|ftp\)://' " Add other protocols as needed.
        execute 'Vexplore '.a:1
	return 1
    elseif isdirectory(a:1)
        execute 'NERDTree '.a:1
        return 1
    else
        NERDTree
    endif
    return 0
endfunction

" Auto commands to handle OS commandline arguments
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc()==1 && !exists('s:std_in') | if <SID>OpenFileOrExplorer(argv()[0]) | wincmd p | enew | wincmd p | endif | endif

" Command to call the OpenFileOrExplorer function.
command! -n=? -complete=file -bar Edit :call <SID>OpenFileOrExplorer('<args>')

" Command-mode abbreviation to replace the :edit Vim command.
cnoreabbrev e Edit
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值