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?

up vote 71 down vote accepted

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.

It's not my vimrc!

Hurray! Go and ask your question. Be sure to mention that you tried starting Vim without a vimrc file!

So it's my vimrc, now what?

If you haven't already, you probably want to save a backup copy of your vimrc file first.

Check the plugins

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:

  1. Comment out (or remove) about half your vimrc file.
  2. Restart Vim, or open a new Vim (reloading the vimrc is not good enough, as settings aren't unset).
  3. Is the problem now gone? Put back the part you removed out (keeping Vim open and using undo is useful here) and repeat step 1 on the part you added back.
  4. Does the problem still occur? Go to step 1.

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 ;-)

Debugging a single plugin

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.

  1. 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
    
  2. 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
    
  3. 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.

  • 6
    This technique is called Binary Search Fault Localization. – tommcdo Feb 19 '15 at 23:53
  • 2
    @tommcdo Yeah, I had it in math classes at high school, I even looked up the name of it when I wrote this, but then I remembered Feynman :-) – Martin Tournoij Feb 19 '15 at 23:59
  • 2
    After you figure out your bug, this would be a great time to start keeping your .vimrc under version control :) It's almost always going to be one of the last changes you made to it that made stuff behave strangely. – escrafford Jun 17 '15 at 21:02
  • You don't need -U NONE if you have -u NONE. – Antony Jul 4 '16 at 7:59
  • 1
    Thanks @jpaugh; I think that order makes more sense. – Martin Tournoij Sep 19 at 11:48

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:

  • This was most helpful in finding a simple mistake in .vimrc file that was causing an error message. Worth trying this first as it's quick to do. – RichVel Jul 21 '16 at 17:09
  • 1
    @kenorb this command is freaking awesome, save me from going to another editor. – TheLazyChap Nov 22 '16 at 14:23

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.

  • 6
    And if you use 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
  • This is almost what I needed, I want to see what the value of cpoptions is at a point in my vimrc, how do I echo the output of ":set cpoptions?" ? – Mike Lippert Jun 15 at 14:26

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

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.

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.

  • Welcome to the site! This is a useful tidbit, but it's quite a bit more specific than the very general question asked. It might be better added as an answer to e.g. this question, or, if you're just looking to share knowledge, you could perhaps ask-and-answer your own question? – Rich Oct 3 at 10:22

Here is a vey simple procedure to get you started.

  1. comment out contents of your entire .vimrc file
  2. reload your .vimrc by running :source % or :so $MYVIMRC
  3. uncomment a discreet part of the file (generally starting from the top)
  4. Repeat steps 2 and 3 until you find the error.
  5. fix the error.
  • 1
    This answer could be a lot better if you properly capitalised and punctuated it. – user579 Sep 12 at 6:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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