简体   繁体   English

函数来源.vimrc和.gvimrc

[英]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. 我通常使用GVim,但我的大部分配置都是通过.vimrc(如keymappings)完成的,因为我想在vim 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). 因此,当我编辑我的vimrc然后从gvim中获取它时,我必须在此之后获取我的.gvimrc以便恢复我的colorscheme(因为它只是gvim)。 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? 或者,因为我的.gvimrc只有几行,我应该把它的内容放到if has("gui_running")块中吗?

You've put your function somewhere in your .vimrc. 你已将函数放在.vimrc中的某个位置。 This means that, while it's being executed, the :source .vimrc is trying to redefine it, which is a problem. 这意味着,当它被执行时, :source .vimrc正在尝试重新定义它,这是一个问题。 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. 我会说,无论你在.vimrc中有什么东西搞乱gvim设置都应该被if !has("gui_running")块所包围。

An autocmd seems to be the easiest way of handling what you're trying to do: autocmd似乎是处理您尝试执行的操作的最简单方法:

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 . 或者,您可以使用映射来触发:source $MYVIMRC:source $MYGVIMRC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM