2

Whenever I do source! %MYVIMRC it gets reloaded, but it is copied inside the file I am editing at the cursor location and comments are inserted all over the place and multiple times (.vimrc is about 200 lines, but I get over 600 lines inserted into whatever file I am editing) and it's all indented in a strange way.

I can't pinpoint the location of the error in vimrc. I've been experimenting with some fancy plugins lately like neocomplete and nerdcommenter, but removing them did not help. Commented out everything from vimrc and sourcing it did the same thing.

The thing is vim works perfectly as expected. I understand more or less everything I have in my vimrc (exceptions are some plugin configs), but it behaves 100% when I start it and just work in it. However, when I source it while working all hell breaks loose. I am on a slightly older version of vim on Debian stable.

Here is my vimrc just in case: https://github.com/sergenikov/vim-tmux-config/blob/master/.vimrc.

  • 3
    Your vimrc file contains Ex commands (commands that you type on the command line and which begin with a colon). The Ex command :source $MYVIMRC sources all the Ex commands written in your vimrc. However, if you add a bang :source! $MYVIMRC, it doesn't read Ex commands anymore but Vim commands (executed from normal mode, like dd for example). So when you type :source! $MYVIMRC, Vim probably searches for anything which looks like a normal command. – saginaw Feb 5 '16 at 19:05
  • 1
    And as soon as it encounters a character which makes you go into insert mode (a, i, A, I...), the rest of your vimrc file is inserted in your current buffer. So maybe try to remove the bang after the :source command to see if that solves your issue. – saginaw Feb 5 '16 at 19:05
  • 2
    I may be wrong but I think you have defined functions without putting a bang after the keyword function. If you don't put a bang after function, when you reload your vimrc, Vim will complain because you are trying to define a new function whose name is already taken by a previous version. To avoid this, you should always put a bang after every keyword function in your vimrc: function! MyFunction() This way the new version will overwrite the older one. – saginaw Feb 5 '16 at 21:36
  • 1
    Alright, I think following @saginaw advice I can do the following: both command and function declarations have a bang attribute !, which I can put after function! Foo() or command! Foo. When vimrc is sourced, it will source the function. If there is not bang attribute, then it will not. – Serge Poele Feb 5 '16 at 21:37
  • 1
    Great! I posted this at the same time literally. :) I will write an answer then. Thank you! – Serge Poele Feb 5 '16 at 21:37
4

This is for the sake of completeness, in case someone else runs into this issue. And thanks to @saginaw for the answer above in comments and a pointer to where to look.

Like @saginaw said I was doing source!, which was not reading Ex commands causing issues. Here it is form the comment:

if you add a bang :source! $MYVIMRC, it doesn't read Ex commands anymore but Vim commands (executed from normal mode, like dd for example). So when you type :source! $MYVIMRC, Vim probably searches for anything which looks like a normal command.

Following @saginaw advice I looked up the doc on source function and then on function and command. They all have bang attribute !, which means every function declaration I have I can do this:

function! Foo()
   ...
endfunction

Or

command! Foo ...

When I source vimrc with source, it will redefine these again. My mistake was that I was using source! thinking that it will redefine those functions. So the lesson is that when you write function in vimrc remember that they have a bang attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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