5

I've just added this line to my .vimrc to be able to open chrome without leaving vim.

command GoogleChromeA execute "!google-chrome > /dev/null 2>&1 &"

It works fine however, when I use the command source $MYVIMRC, I get this error:

E174: command already exists: add ! to replace it

How can I fix that?

Edit:
I've tried command!. It works fine but for some reason the statusline from vim-airline changes:
This is how it usually looks like: enter image description here

This is how it looks like after saving (sourcing) the file: enter image description here

I could live with it since it just happens when sourcing my .vimrc but... it would be better if this could be fixed :)

3
  • 3
    Try command! instead of command. Do you usually source your vimrc multiple times in a session?
    – muru
    Commented Mar 27, 2016 at 19:45
  • I have set an autocommand that source my vimrc every time I save it. This way I can check the changes without having to exit vim and opening it again.
    – Wylex
    Commented Mar 27, 2016 at 20:04
  • 1
    Why exit Vim? Open a new instance in another terminal, or background Vim and start a new one. For testing vimrc, I'd strongly recommend running a new instance.
    – muru
    Commented Mar 28, 2016 at 4:56

2 Answers 2

7

command will give an error if a command with that name already exists. Vim has no way of knowing that the command in your vimrc is actually the same command; from Vim's viewpoint it's a "new" command.

You have to use command! to override any existing commands. The same applies to functions; use function! to override existing functions. This also works for the shorthand versions (e.g. comm!, fun!).

Another caveat are autocommands. If you use:

autocmd BufNew * some_command()

and you reload your vimrc then you will have two autocommands defined! Vim has no way to "see" that this autocommand was already executed and installed on startup.

To fix this use augroup:

" Define a new autocommand group
augroup my_autocmd_name
    " Clear all existing autocommands in this group
    autocmd!

    " This will now always be installed once only
    autocmd BufNew * some_command()
augroup end

A third caveat is realizing that not all autocommands are run when you reload your vimrc. Specifically, things like VimEnter, BufReadPost, Filetype WinEnter, etc. aren't executed when you type :source $VIMRC. This is especially important if you have something like:

" Real men use real tabs
set expandtab

" Python programmers aren't real men (augroup omitted for brevity)
autocmd Filetype *.py setlocal noexpandtab

If you now reload your vimrc while you have a buffer with a Python file, the :set command will reset expandtab and the Filetype autocommand is never executed to override it for Python files.


As for your airline issue − I don't know. The FAQ recommends running the :AirlineRefresh command.

1
  • 1
    AirlineRefresh with autocmd SourceCmd .vimrc AirlineRefresh should work
    – laktak
    Commented Jan 16, 2017 at 16:42
0

Since I discovered that my comment for the airline issue does not quite work, here's an expanded (and hopefully correct) answer:

augroup my_autocmd_name
  ...
  autocmd SourceCmd .vimrc execute "source" expand("<afile>") | AirlineRefresh

augroup end

autocmd SourceCmd will actually prevent vim from sourcing the file so we have to call it ourselves before doing the refresh.

Your Answer

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

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