Ignore "Unknown option" errors in vimrc - vim

I tote the same .vimrc between machines that have both Vim 7.2 and 7.3 installed. The machines with Vim 7.2 complain about my 7.3-specific options every time I open a file:
Error detected while processing /home/spiffytech/.vimrc:
line 72:
E518: Unknown option: rnu
line 73:
E518: Unknown option: undofile
line 74:
E518: Unknown option: undodir=/tmp
line 75:
E518: Unknown option: cryptmethod=blowfish
Press ENTER or type command to continue
How can I make Vim ignore these errors and not prompt me to hit enter whenever I open files?

It might be worth doing more fine-grained checking for actual supported features rather than versions.
E.g.:
if has('persistent_undo')
set undofile
set undodir=/tmp
endif
" Some options can only be checked with exists('+option'); I'm not sure why
if exists('+relativenumber')
set rnu
endif
if has('cryptv')
set cryptmethod=blowfish
end

Wrap the new options in:
if version >= 703
set rnu ...
endif
Check the help for v:version for more info on the version number to use:
*v:version* *version-variable*
v:version Version number of Vim: Major version number times 100 plus
minor version number. Version 5.0 is 500. Version 5.1 (5.01)
is 501. Read-only. "version" also works, for backwards
compatibility.
Use |has()| to check if a certain patch was included, e.g.: >
if has("patch123")
< Note that patch numbers are specific to the version, thus both
version 5.0 and 5.1 may have a patch 123, but these are
completely different.

Sometimes an option is legitimate, but not available in the current environment. For example:
$ vi
Error detected while processing /home/username/.vimrc:
line 9:
Unknown option: indentexpr=
To test whether an option exists or not, and avoid an error if not available:
if exists("&indentexpr")
:set indentexpr=
endif

You can ignore any error with silent! ..., like silent! set undofile

In you .vimrc, you can test against the Vim version you are executing.
See help v:version
if v:version >= 703
"do something
set rnu
set undofile
...
endif
703 corresponds to Vim 7.3 (which is not really intuitive...)

I would say this question is not answered. Consider a Session.vim created on computer A which has the later vim version. In source control when another computer B tries to open the Session.vim the errors are triggered. It makes no sense to have to manually wrap a version number for what should be an automated process. With this behaviour the new version would have to automatically wrap a new command in a version number when saving a session - something which 7.3 does not do.

Related

Vimscript Operator-Pending Mappings yields "Trailing characters" error

I am trying to reproduce the example in chapter 15 of Steve Losh's "Learn Vimscript the Hard Way" (http://learnvimscriptthehardway.stevelosh.com/chapters/15.html), in the section called "Changing the Start."
First, I start vim with the following command:
vim -u NONE foo
to ensure that no plugins are loaded.
Next I type
print foo(bar)
in the buffer and then enter
:onoremap in( :<c-u>normal! f(vi(<cr>
Finally, I place the cursor over the "p" in "print" and enter cin(. The status line displays:
:<c-u>normal! f(vi(<cr>
with a blinking cursor at the end. I hit enter and the status line then displays:
E488: Trailing characters
The expected outcome, according to the book, is that vim will delete the contents of the parentheses and place you in insert mode between them.
Here is the version info:
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled May 2 2017 03:55:34)
MacOS X (unix) version
Included patches: 1-596
Compiled by Homebrew
TL;DR: use vim -u DEFAULTS foo instead.
That should keep Vim in 'nocompatible' mode, which is needed for <Key> codes to be recognized in mappings.
The symptoms you describe are consistent to having < included in 'cpoptions', which is documented to:
Disable the recognition of special key codes in <> form in mappings, abbreviations, and the "to" part of menu commands. For example, the command :map X <Tab> results in X being mapped to "<Tab>" (5 characters) when < is included; but "^I" (^I is a real <Tab>) when < is excluded.
That is consistent with you getting the literal result of your mapping in your command-line, without proper expansion of the <c-u> and <cr> key codes.
The defaults for 'cpoptions' are:
Vim default: "aABceFs",
Vi default: all flags
So this means < (along with all other flags) will be included by default in Vi mode, which is triggered when 'compatible' is set (or, perhaps more accurately, when 'nocompatible' is not set.)
It turns out that when you use -u NONE in your command-line starting Vim, it will start in 'compatible' mode.
You can use instead -u DEFAULTS, which is quite similar to -u NONE, but it will still load the defaults.vim script shipped with Vim runtime, which will among other things set 'nocompatible'.
From the docs:
When -u DEFAULTS (all uppercase) is passed, this has the same effect as -u NONE, but the defaults.vim script is loaded, which will also set 'nocompatible'.
Using the -u argument with another argument than DEFAULTS has the side effect that the 'compatible' option will be on by default. This can have unexpected effects.

How to jump back to netrw when splitbelow is on

If I open a file in a current directory using netrw and then reopen the netrw using :Sexplore the netrw window opens below (I have splitbelow set), but the cursor remains in the file being edited and I cannot move to the netrw window using CTRL-W combinations.
Am I doing something wrong or is this a bug?
Steps to reproduce
run vim .
netrw opens, highlight a file in the current directory (let's say test.txt) and press Return, file test.txt opens
run :Sexplore cmd
netrw opens below but the cursor remains in the file opened previously.
My .vimrc
set nocompatible
set splitbelow
VIM version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 8 2015 23:11:08)
MacOS X (unix) version
Included patches: 1-944
This is how it looks like:
I cannot reproduce with version 154a (use :echo g:loaded_netrwPlugin to check your netrw version).
Try with the latest version.
Edit:
It is possible that the problem is cause by some other plugin interference or settings. You could try following the approach described at Vim FAQ 2.5:
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
problem with my setup or with Vim? / Have I found a bug in Vim?
First, you need to find out, whether the error is in the actual
runtime files or any plugin that is distributed with Vim or whether it
is a simple side effect of any configuration option from your .vimrc
or .gvimrc. So first, start vim like this:
vim -u NONE -U NONE -N -i NONE
This starts Vim in nocompatible mode (-N), without reading your
viminfo file (-i NONE), without reading any configuration file (-u
NONE for not reading .vimrc file and -U NONE for not reading a .gvimrc
file) or even plugin.
(...)
If the error does not occur when starting Vim this way, then the
problem is either related to some plugin of yours or some setting in
one of your local setup files. You need to find out, what triggers the
error, you try starting Vim this way:
vim -u NONE -U NONE -N
If the error occurs, the problem is your .viminfo file. Simply delete
the viminfo file then. If the error does not occur, try:
vim -u ~/.vimrc --noplugin -N -i NONE
This will simply use your .vimrc as configuration file, but not load
any plugins. If the error occurs this time, the error is possibly
caused by some configuration option inside your .vimrc file. Depending
on the length of your vimrc file, it can be quite hard to trace the
origin within that file.
The best way is to add :finish command in the middle of your .vimrc.
Then restart again using the same command line. If the error still
occurs, the bug must be caused because of a setting in the first half
of your .vimrc. If it doesn't happen, the problematic setting must be
in the second half of your .vimrc. So move the :finish command to the
middle of that half, of which you know that triggers the error and
move your way along, until you find the problematic option. If your
.vimrc is 350 lines long, you need at a maximum 9 tries to find the
offending line (in practise, this can often be further reduced, since
often lines depend on each other).
Obviously you will have to load netrw manually (which can be done by sourcing plugin/netrwPlugin.vim -- if you are using some plugin manager you will have to move the plugin to ~/.vim/ or change your runtime path due to the autoload functions) and set nocompatible and splitbelow.
Actually there is an adapted version of this procedure in :help netrw-debug:
Step 2: assuming that you've installed the latest version of netrw,
check that your problem is really due to netrw. Create a file called
netrw.vimrc with the following contents:
set nocp
so $HOME/.vim/plugin/netrwPlugin.vim
Then run netrw as follows:
vim -u netrw.vimrc --noplugins [some path here]
Perform whatever
netrw commands you need to, and check that the problem is still
present. This procedure sidesteps any issues due to personal .vimrc
settings and other plugins. If the problem does not appear, then you
need to determine what setting in your .vimrc is causing the conflict
with netrw or which plugin.
Step 3: If the problem still is present, then get a debugging trace
from netrw:
(...)

vim "syntax on" does not work

Here is my .vimrc
1 syntax on
2 set ts=4
3 set number
4 set smartindent
5 set shiftwidth=4
However, I tried to edit HelloWorld.java and HelloWorld.c. Both have pure regular black font. No any highlighting!
I also tried :syntax on after the vim is open, but no luck.
\>vim -version
VIM - Vi IMproved 7.3 (2010 Aug 15)
\>cat /etc/*-release
openSUSE 11.4 (x86_64)
VERSION = 11.4
CODENAME = Celadon
When you edit the file, are you using
vim filename
This can matter. In some server configurations, if you do vi filename you get vim, but it's a very stripped down version of vim that is very much like the original vi (which does not, among other things, do syntax coloring). On a system configured in this way, if you instead type vim filename, you get the full featured vim.
I just worked through this with a person who was on a server that had the vim-minimal package installed as well as another vim package. I suspect (but did not verify that) the vim-minimal package installed its executable as /bin/vi.
The difference was very clear when you looked at the actual files (i.e. ls -l /bin/vi vs ls -l /usr/bin/vim)--one was about ten times the size. Both of them were actually vim, same version number and everything, but the /bin/vi one was compiled with very few features enabled.
To make it even more confusing:
vi existing.pl
opened the .pl file, gave no syntax coloring
vi [enter]
gave the vim splash screen, and from there
:e existing.pl
opened the file with syntax coloring on.
A comment from Jan Wilamowski suggests checking by doing:
vi --version
If that shows that the syntax feature was not compiled in, try
vim --version
and see if it is compiled in there.
You'll need to install the vim-data package on openSUSE for vim syntax colouring to work.
Sounds strange, I know that this is not pulled in by default with the vim package but AFAIK it's for people who want to create tiny base installs.
Package vim-data contains the runtime files.
Also make sure your remote environment has an appropriate TERM variable set TERM=screen-256color, TERM=xterm, TERM=xterm-256color should all work just fine with ssh and ssh with screen/tmux.
If all above have been done and you see some underlines and bold instead of actual colors... this might work for you:
export TERM=xterm-color
in your .vimrc, I don't see filetype setting. you could try to add:
filetype plugin indent on
to your vimrc.
if you don't have set nocp, add this line too.
if you read :h filetype
:filetype on
Each time a new or existing file is edited, Vim will try to recognize the type
of the file and set the 'filetype' option. This will trigger the FileType
event, which can be used to set the syntax highlighting, set options, etc.
For some strange reason on MacOS, 'syntax on' must be the first line in your .vimrc file. The line appears to be ignored if placed elsewhere in the file.
One item not mentioned is :set syntax=<type>, e.g. :set syntax=markdown.
This has been successful in instances where other techniques above were not.

How to tell VIM to store the viminfo file somewhere else?

Is it possible to tell vim to save its viminfo file somewhere else?
Such as in the .vim folder
Try adding set viminfo+=n~/.vim/viminfo to your ~/.vimrc file. From :help 'viminfo':
n Name of the viminfo file. The name must immediately follow
the 'n'. Must be the last one! If the "-i" argument was
given when starting Vim, that file name overrides the one
given here with 'viminfo'. Environment variables are expanded
when opening the file, not when setting the option.
set viminfo='1000,nc:\\users\\abcdef\\_viminfo
This works for me in Windows 7.
I had a similar problem but had no write permission to my home folder (thus could not create the ~/.vimrc).
I solved it by modifying (through the Administrator) the _vimrc in C:\Program Files\VIM to include the line:
let $HOME = $USERPROFILE
I placed it at the beginning of the file and it worked well. Just in case someone comes across this question and this answer didn't work.
I know its quite a while now, but I am just mentioning this for future readers.
I was facing the same issue while trying to change the location for viminfo file in vimrc. At last setting the value of viminfofile option worked for me.
Added the following to my vimrc:
set viminfofile=D:\vim\viminfo
It works for me on windows with vim 8.2
TL;DR
This seems to be a bug in vim where set nocompatible is not idempotent and doesn't follow the principle of least astonishment.
As a workaround, either:
Ensure that you set nocompatible (or the equivalent set nocp) only once, and at the top of your vimrc.
Don't set it again if it's already set:
if &compatible | set nocompatible | endif " Avoid side effects if `nocp` already set
Explanation and bug illustration
From :help compatible (empahsis mine):
This is a special kind of option, because when it's set or reset,
other options are also changed as a side effect. CAREFUL: Setting or
resetting this option can have a lot of unexpected effects: Mappings
are interpreted in another way, undo behaves differently, etc. If you
set this option in your vimrc file, you should probably put it at the
very start.
Note that &viminfo is not listed in the side-effects, however the following lines clearly show the side effect upon &viminfo:
set nocompatible
set viminfo+=nWatch-my-viminfo-file-location-be-ignored
echom &viminfo
set nocompatible " do side effects even though nocomptible is already set
echom 'After 2nd "set nocompatible":'
echom &viminfo
Output:
'100,<50,s10,h,nWatch-my-viminfo-file-location-be-ignored
After 2nd "set nocompatible":
'100,<50,s10,h
vim --version | head -1
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 05 2016 16:48:20)
Resolution
I have raised two GitHub issues regarding this:
Undocumented "set nocompatible" side effect upon &viminfo
set nocompatible not idempotent - setting produces side effects when already set
See also Vi StackExchange's question: Can't move viminfo file - &viminfo reverts upon loading vim which I raised before seeing this one.
The ordering of settings in the .vimrc affects the use of :set viminfo. The setting of viminfo should be after the setting of nocompatible. After reordering my .vimrc, the above solution to have the viminfo file be located in the .vim directory worked for me.
In gvim8.2 on Windows10, following option works to change path of viminfo :
set viminfofile=$VIM/.viminfo
Write this in .vimrc .
It could to change viminfo file path to under the $VIM.

vim can't open syntax.vim

In my .vimrc I have the following snippet:
if has('autocmd')
filetype plugin indent on
syntax on
but when I start vim it shows the following error:
Can't open file /usr/share/vim/syntax/syntax.vim
error seems to have occurred on syntax on line in .vimrc.
I do not have syntax.vim in path /usr/share/vim/syntax though I have others like clojure.vim cpp.vim java.vim etc...
I searched but can't seem to find the default syntax.vim if it comes with default version of vim.
I've been stuck with this for a while and would like some help.
I use Arch and build vim from the AUR using the vim-git PKGBUILD. For me, it was the case of using vim-git, but vim-runtime from the official repos, not vim-runtime-git from the AUR.
Switching to vim-runtime-git solved this issue for me and provided the syntax file (and colours, and...).
It should be there so there may be other files missing. I would suggest you re-install!.
For a QDF here is what the
syntax.vim file should look like this:-
" Vim syntax support file
" Maintainer: Bram Moolenaar <Bram#vim.org>
" Last Change: 2001 Sep 04
" This file is used for ":syntax on".
" It installs the autocommands and starts highlighting for all buffers.
if !has("syntax")
finish
endif
" If Syntax highlighting appears to be on already, turn it off first, so that
" any leftovers are cleared.
if exists("syntax_on") || exists("syntax_manual")
so <sfile>:p:h/nosyntax.vim
endif
" Load the Syntax autocommands and set the default methods for highlighting.
runtime syntax/synload.vim
" Load the FileType autocommands if not done yet.
if exists("did_load_filetypes")
let s:did_ft = 1
else
filetype on
let s:did_ft = 0
endif
" Set up the connection between FileType and Syntax autocommands.
" This makes the syntax automatically set when the file type is detected.
augroup syntaxset
au! FileType * exe "set syntax=" . expand("<amatch>")
augroup END
" Execute the syntax autocommands for the each buffer.
" If the filetype wasn't detected yet, do that now.
" Always do the syntaxset autocommands, for buffers where the 'filetype'
" already was set manually (e.g., help buffers).
doautoall syntaxset FileType
if !s:did_ft
doautoall filetypedetect BufRead
endif
I had this problem as well after upgrading to 7.3 (I'm using cygwin). Check whether running "vi" instead of "vim" has the same problem. I found that vim was actually the old 7.2 executable, but the syntax files were in the location expected by 7.3; vi was the correct 7.3 executable.
I have been using compiled vim from source without explicitly installing it on my system (due to missing sudo privileges).
I was able to solve the issue by adding the following line to my .bashrc:
export VIMRUNTIME=<path to cloned vim repo>/runtime
Thus: Pointing this env variable to the runtime director in the git tree, makes vim find the needed file(s).

Resources