20

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?

4 Answers 4

40

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.

2
  • Type :version to figure out which version of Vim it is. Mar 25, 2009 at 5:04
  • 1
    @anon, thanks this worked but when I do vi --version, it shows VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Feb 17 2012 10:24:10) so it's a VIM compiled vi with features off. As I can see further in description Small version without GUI. But why this worked? Am I missing something.
    – garg10may
    Apr 12, 2016 at 7:10
14

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
7

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

3
  • why this version>=500 works for vim and not vi, In my machine vim's version is 7.4, vi also shows 7.4. How come this check distinguish between the two? BTW it works. Jul 21, 2022 at 21:18
  • @PrabhatKumarSingh chances are your “vi” is actually a symlink to vim. But vim is smart enough if the executable name is vi, it reverts to vi compatibility mode. Jul 22, 2022 at 22:17
  • @paul-tombolin but how does this version check works and how can I see the difference for vi and vim manually because as I said above vim --version VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan 29 2020 08:09:16) i.e 7.4, so what is 500 here ? Jul 25, 2022 at 11:25
4

"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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.