I have a problem in Vim, and I think it may be in my vimrc
file (or have been told it could be my vimrc
file).
How do I verify this? If it is my vimrc
file, how do I know where exactly the problem lies?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI have a problem in Vim, and I think it may be in my vimrc
file (or have been told it could be my vimrc
file).
How do I verify this? If it is my vimrc
file, how do I know where exactly the problem lies?
The first thing you want to do is to start Vim with the default settings:
vim -u NONE -U NONE -N
The -u NONE
prevents Vim from loading your vimrc, -U NONE
prevents Vim from
loading your gvimrc, and -N
tells Vim to use no-compatible mode (this isn't
required, but most Vim users are not used to "compatible" mode).
Note that the NONE
is required to be in all-caps.
In Windows you can add these flags by creating a new shortcut1.
If the problem stays, then you know it's not something in your vimrc.
If the problem disappears, you now it's caused by something in your vimrc file.
Hurray! Go and ask your question. Be sure to mention that you tried starting Vim without a vimrc file!
If you haven't already, you probably want to save a backup copy of your vimrc file first.
The next thing you probably want to do is disable all plugins first; plugins can
alter quite a bit in Vim. If this fixes the problem, then try to find out
which plugin by re-enabling them one-by-one. After you've found out which
plugin exactly causes the problem, you can try & fix it by reading this plugin's
documentation, and/or by asking a question tagged with plugin-<name>
.
If it's not a plugin, and you don't have any idea what's causing your problem, then it's a trial-and-error procedure. Comment out one or more lines in your vimrc, start Vim, check if the problem occurs, and repeat this procedure until the problem stops occurring. The fastest way of doing this is:
In the end you should have a single option or a combination of a few options that causes your problem. You can find out more about any option in Vim by using:
:help 'option_name'
The quotes are important here, it usually works without them, but sometimes you end up on the wrong page if you omit them.
If you're still confused after reading the help page, you know where to ask a question ;-)
If you want to isolate a single plugin, perhaps to ask a question about it, you want to load as little as possible but still load the plugin; you can easily do this with Vim's packages feature. This requires Vim 8 or a reasonably recent version of Neovim.
Create a new empty directory; we'll use the ~/plugin
path in this example.
Now put the plugin in the regular pack/plugins/start/$name
directory. For
example:
git clone https://github.com/fatih/vim-go.git ~/plugin/pack/plugins/start/vim-go
Create a test-vimrc
file with the following contents; this will ensure that
Vim will load plugins from the ~/plugin
directory and not the ~/.vim
directory:
set nocompatible
set packpath=~/plugin,/usr/share/vim/vimfiles,/usr/share/vim/vim80,/usr/share/vim/vimfiles/after,~/plugin/after
packloadall!
syntax on
filetype plugin indent on
Start Vim with:
vim -U NONE -u ~/test-vimrc
You now have a minimal vimrc with just this single plugin.
Footnotes
1 For example: on 64 bit Windows, the shortcut would look something like this: "C:\Program Files (x86)\Vim\vim74\vim.exe" -u NONE -U NONE -N
. To create it, right click in File Explorer where you want the shortcut, then select New -> Shortcut and paste the shortcut text. You may need to change the Vim path if your Vim is installed in another location.
vim --clean
for many situtations. Besides being easier to type, it's equivalent to -u DEFAULTS -U NONE -i NONE
plus a few more bits like excluding $HOME from $RUNTIMEPATH. -u DEFAULTS
uses defaults.vim
as vimrc...which means Vim is actually usable while you debug.
– B Layer
Jul 8 '19 at 11:39
There is -D
Vim parameter specially for debugging which will go to debugging mode after executing the first command from a script.
E.g. to run Vim in debug mode without any plugins, run as:
vim --noplugin -D
Type n
/next
to parse the next line and keep pressing Enter.
And cont
or q
to go back to vim
interface.
If you're using a GUI version, put a gui
command in your vimrc to start the debugging right after that command.
Press Ctrl+d for list of available commands
Read more:
:help debug-scripts
As already mentioned, first try vim -u NONE -U NONE -N
to make sure your vanilla vim is working fine.
Then start vim normally and check
:messages
from inside vim after the problem, which will show all warnings and errors.
Finally start vim with the following command
vim -V9logfile.log
which will create a logfile called logfile.log
, -V9
is the logging level and try to reproduce your problem.
One more tip: as caveman as it may seem, I do like adding :echom "message" to my .vimrc to see whats being executed and what is not.
For example
if executable('ag')
:echom "AG FOUND"
...
endif
This helps me quickly see if the if block is being executed, etc. The message will be printed on the console next time you start vim.
echom
another interesting command is :messages
to get the full log of the messages echoed. It can sometimes be useful to look at them after the startup.
– statox♦
Mar 4 '16 at 8:43
echom &cpoptions
. Interactively, :verbose set cpoptions?
is a bit nicer
– D. Ben Knoble♦
Oct 17 '20 at 16:19
One thing that I've found helps is keeping a collection of variables that toggle parts of your .vimrc
so you can disable or enable portions of it without having to comment stuff out. It also has the pleasant side effect of forcing you to think about the organization of your .vimrc
.
" feature toggles in vimrc
let g:vimrc_feat_pathogen = 1
let g:vimrc_feat_core_minimal = 1
let g:vimrc_feat_matchit = 1
let g:vimrc_feat_options = 1
let g:vimrc_feat_colors_and_highlight = 1
let g:vimrc_feat_key_rebinding_arpeggio = 1
let g:vimrc_feat_key_rebinding = 1
let g:vimrc_feat_autocommand_group = 1
let g:vimrc_feat_custom_definitions = 1
let g:vimrc_feat_load_opam = 1
Here's an example of a section guarded by an if
:
if g:vimrc_feat_core_minimal
set nocompatible
filetype plugin indent on
syntax on
endif
My vimrc loading time was very slow, around 400ms, and i reduced it to 20ms only by removing this line:
silent !mkdir ~/.config/nvim/backups > /dev/null 2>&1
Make sure you don't have calls to external commands like that.
Here is a vey simple procedure to get you started.
:source %
or :so $MYVIMRC