开发者

Function to source .vimrc and .gvimrc

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 ha开发者_开发技巧ve 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?


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.


I would say that whatever you have in your .vimrc that's messing up gvim settings should be surrounded by an if !has("gui_running") block.

An autocmd seems to be the easiest way of handling what you're trying to do:

autocmd BufWritePre .gvimrc,.vimrc source <amatch>

This way you get your configuration file automatically reloaded when you save it without having to mess around with functions. Alternatively, you could use a mapping to trigger :source $MYVIMRC or :source $MYGVIMRC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜