简体   繁体   English

为什么.vimrc中的函数会被自动调用?

[英]Why my function in .vimrc got called automatically?

As I know, we could define function in .vimrc as below: 据我所知,我们可以在.vimrc中定义函数,如下所示:

function Fun()
    do something here
endfunction

Then we could call it like :call Fun() . 然后我们可以这样称呼:call Fun()

For one of my system, there's only the function definition in my .vimrc, however the function is always exectued once I get into vim. 对于我的一个系统, 我的.vimrc中只有函数定义,但是一旦进入vim,函数总是被激活。 Why? 为什么?

Below is version of my vim: 下面是我的vim的版本:

# vi --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Feb 17 2012 10:24:10)
Included patches: 1-411
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Small version without GUI.  Features included (+) or not (-):
-arabic -autocmd -balloon_eval -browse +builtin_terms -byte_offset -cindent 
-clientserver -clipboard -cmdline_compl +cmdline_hist -cmdline_info -comments 
-cryptv -cscope -cursorshape -dialog -diff -digraphs -dnd -ebcdic -emacs_tags 
-eval -ex_extra -extra_search -farsi -file_in_path -find_in_path -float 
-folding -footer +fork() -gettext -hangul_input +iconv -insert_expand +jumplist
 -keymap -langmap -libcall -linebreak -lispindent -listcmds -localmap -menu 
-mksession -modify_fname -mouse -mouse_dec -mouse_gpm -mouse_jsbterm 
-mouse_netterm -mouse_sysmouse -mouse_xterm +multi_byte -multi_lang -mzscheme 
-netbeans_intg -osfiletype -path_extra -perl -printer -profile -python 
-quickfix -reltime -rightleft -ruby -scrollbind -signs -smartindent -sniff 
-startuptime -statusline -sun_workshop -syntax -tag_binary -tag_old_static 
-tag_any_white -tcl +terminfo -termresponse -textobjects -title -toolbar 
-user_commands -vertsplit -virtualedit +visual -visualextra -viminfo -vreplace 
+wildignore -wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp 
-xterm_clipboard -xterm_save 
   system vimrc file: "/etc/virc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -O2 -g -pipe -Wall  -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64  -D_FORTIFY_SOURCE=1        
Linking: gcc   -L/usr/local/lib -o vim       -lm  -lselinux -lncurses -lacl 

And my .vimrc: 而我的.vimrc:

# cat ~/.vimrc
function Fun1()
        :!ls
endfunction

Function name doesn't matter, I've changed the name as a test. 功能名称无关紧要,我已将名称更改为测试。

Update on July 8, 2015: 2015年7月8日更新:

According to the answer from Ben, and since I'd like my .vimrc be compatible over multiple environments, I wrap the function definition with if has('eval')...endif as my finial solution: 根据Ben的回答,因为我希望我的.vimrc在多个环境中兼容,所以我用if has('eval')...endif作为我的最终解决方案来包装函数定义:

if has('eval')
    function! Fun1()
        do something
    endfunction
endif

Your vim is compiled without "eval" support. 您的vim编译时没有“eval”支持。 That means your Vim does not support defining functions. 这意味着你的Vim不支持定义功能。 The opening function Fun1() command is therefore invalid, and ignored. 因此,打开function Fun1()命令无效,并被忽略。 Then the function definition is executed because those are valid commands. 然后执行函数定义,因为它们有效的命令。

You will need to install a more fully-featured Vim, or compile your own Vim, or find an alternate Vim installation on your system that has more features. 您需要安装功能更全面的Vim,或者编译自己的Vim,或者在系统上找到具有更多功能的备用Vim安装。 "eval" comes with a NORMAL or larger feature set. “eval”附带NORMAL或更大的功能集。

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

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