为什么有些人在.gvimrc中使用'if has(“gui_running”)'?

时间:2010-11-19 21:43:49

标签: vim

我一直在阅读一些dotfiles(.vimrc .gvimrc)来学习一些巧妙的技巧,我遇到了this one

if has("gui_running")
    set fuoptions=maxvert,maxhorz
    au GUIEnter * set fullscreen
endif

如果这已经是.gvimrc(仅在加载gVim时加载)为什么它具有条件if has("gui_running")?这不是多余的吗?是否存在特殊问题/原因?

我知道if has("gui_running")在脚本中使用很有意思,我会特别询问它在.gvimrc中的用法,因为它只在我使用gvim时才会出现,因此理论上不需要。< / p>

4 个答案:

答案 0 :(得分:50)

OP链接到的gvimrc文件是我的,所以我最好拥有并承认它是没有充分理由的。

我从Hacking without distractions复制了该代码段,建议将其放入vimrc中。然后在某些时候我意识到将它移动到gvimrc文件会更简洁,但我没有清楚地认为它已经完成了if has('gui_running')检查。你是正确的指出它是不必要的,所以我现在已经删除了它。

为了子孙后代,这是我的gvimrc beforeafter的变化。

答案 1 :(得分:13)

保留一个配置文件而不是两个配置文件更容易(特别是如果您在多台计算机上工作,并且需要保持其配置同步)。因此,有些人可能更愿意将其全部放入.gvimrc文件并使用警卫,而不是创建.vimrc .vimrc

然后有人在互联网上共享此文件,并且人们将GUI重新发送的部分复制到.gvimrc。这就是它最终的结果。

答案 2 :(得分:7)

从vim-documentation开始,基本上它允许你根据正在运行的gui进行各种设置。

- To check in a Vim script if the GUI is being used, you can use something
  like this:

    if has("gui_running")
       echo "yes, we have a GUI"
    else
       echo "Boring old console"
    endif

                            *setting-guifont*
- When you use the same vimrc file on various systems, you can use something
  like this to set options specifically for each type of GUI:

    if has("gui_running")
        if has("gui_gtk2")
        :set guifont=Luxi\ Mono\ 12
        elseif has("x11")
        " Also for GTK 1
        :set guifont=*-lucidatypewriter-medium-r-normal-*-*-180-*-*-m-*-*
        elseif has("gui_win32")
        :set guifont=Luxi_Mono:h12:cANSI
        endif
    endif

更新:

            *gui-init* *gvimrc* *.gvimrc* *_gvimrc* *$MYGVIMRC*
The gvimrc file is where GUI-specific startup commands should be placed.  It
is always sourced after the |vimrc| file.  If you have one then the $MYGVIMRC
environment variable has its name.

答案 3 :(得分:1)

如果您使用gvim调用启动程序,则会读取.gvimrc 而不是vim? 我能看到的唯一区别是,如果你在一个设置中调用gvim 它无法启动(例如,您在没有X的ssh会话中)。因为vim将会运行,但文件仍可能来源。

(我真的不知道是不是这样,在我的系统中我已经编译了vim而没有X,所以我无法测试它)