How to detect vi (not vim) in .vimrc?

2019-04-03 16:59发布

I carry a vimrc to all the machines that I work on and it naturally contains options that are not present in old vi.

If I accidentally start a vi session on a machine where vi is not an alias to vim and/or vim is not installed, vi reads vimrc and throws a bunch of annoying errors to let me know that option such and such are unsupported.

I know I can just always type "vim" instead of "vi" and set the EDITOR variable to vim (for visudo etc...), but is there a line I can add to the top of the vimrc that will exit the script early if the file is read by vi?

标签: vi vim
4条回答
可以哭但决不认输i
2楼-- · 2019-04-03 17:32

"vi" reads vimrc because it's surely Vim compiled with name "vi". And it's likely compiled "to be very Vi compatible", so you can try to check feature "compatible" to detect "vi":

if !has("compatible")
   let g:loaded_matchparen=1
   syntax off
endif
查看更多
迷人小祖宗
3楼-- · 2019-04-03 17:34

If vi is not actually a link to vim, it should not read .vimrc, it should read .exrc. The fact that it is reading .vimrc indicates it is actually an earlier version of vim. If that is the case, you can use the vim "if" construct to bracket newer features, like this:

:if version >= 500
:  version-5-specific-commands
:endif

Type:

:help if

when in vim for more info.

查看更多
戒情不戒烟
4楼-- · 2019-04-03 17:48

Non-vim doesn't read a .vimrc, it's looking for a .exrc. You can detect older versions of vim using "if version >= 500"

查看更多
我命由我不由天
5楼-- · 2019-04-03 17:53

If you want to get more specific in your checks you can check for individual features too.

I have this in my .vimrc:

if has("eval")
    " Syntax stuff
    let java_highlight_all=1
endif


if has("autocmd")
    " Buffers
    autocmd BufEnter * cd %:p:h
endif
查看更多
登录 后发表回答