Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to source .vimrc and .gvimrc

Tags:

vim

I generally use GVim, but most of my configuration is done via .vimrc (like keymappings) because I want them in vim and gvim. So when I edit my vimrc and then source it from gvim, I have to source my .gvimrc after that in order to get my colorscheme back (since it's gvim only). I tried to write a function to do this, and ran into the problems described in the comments below:

function ReloadConfigs()
    :source ~/.vimrc
    if has("gui_running")
        :source ~/.gvimrc
    endif
endfunction
command! Recfg call ReloadConfigs()
" error: function already exists, add ! to replace it

function! ReloadConfigs()
    :source ~/.vimrc
    if has("gui_running")
        :source ~/.gvimrc
    endif
endfunction
command! Recfg call ReloadConfigs()
" error: cannot replace function, it is in use

Is it possible to do something like this? Or, since my .gvimrc only has a few lines, should I just put its contents into an if has("gui_running") block?

like image 861
Daniel Avatar asked Aug 18 '11 22:08

Daniel


People also ask

Can you source Vimrc?

You basically just need to “source” the settings file: :source ~/. vimrc . You can use the abbreviated command, so it's just :so ~/. vimrc .

Where can I find .vimrc file?

The global or system-wide vim configuration file is generally located under the /etc/vim/vimrc . This configuration file is applied to all users and when Vim is started this configuration file is read and Vim is configured according to this file contents.


1 Answers

You've put your function somewhere in your .vimrc. This means that, while it's being executed, the :source .vimrc is trying to redefine it, which is a problem. You could try doing this:

if !exists("*ReloadConfigs")
  function ReloadConfigs()
      :source ~/.vimrc
      if has("gui_running")
          :source ~/.gvimrc
      endif
  endfunction
  command! Recfg call ReloadConfigs()
endif

If the function is already defined, this should skip redefining it, avoiding the issue.

like image 96
Andrew Radev Avatar answered Sep 21 '22 17:09

Andrew Radev