Vi被别名为vim,但以代码1退出。

3

即使我已经将vi别名为vim,这个返回代码还是很奇怪。

如果有人能解释一下原因,我会非常感激。

以下是我的命令和结果,请查看。

  • which vi returns /usr/bin/vi
  • which vim returns /usr/bin/vim
  • ls -al /usr/bin/vi /usr/bin/vim returns

    lrwxr-xr-x  1 root  wheel        3 Dec 15 03:34 /usr/bin/vi -> vim
    -rwxr-xr-x  1 root  wheel  1745984 Dec 10 18:03 /usr/bin/vim
    
  • cat ~/.vimrc returns

    set nocompatible
    silent! unmap <C-E>
    
  • vim then :q to quit immediately. Then echo $? returns 0

  • vi then :q to quite immediately. But problem is here, echo $? returns 1
  • And if second line in .vimrc, i.e. silent! unmap <C-E>, is removed, vi, :q, echo $? returns 0
  • vi then :version prints

    VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Oct  5 2016 16:04:34)
    Included patches: 1-898 Compiled by root@apple.com Normal version
    without GUI.  Features included (+) or not (-):
    +acl             +cindent         -conceal         +digraphs        -farsi           -gettext         +libcall         +mksession       -mouse_netterm   -mzscheme        -profile         +scrollbind      +syntax          +textobjects     +visualextra     -X11
    -arabic          -clientserver    +cryptv          -dnd             +file_in_path    -hangul_input    +linebreak       +modify_fname    -mouse_sgr       +netbeans_intg   +python/dyn      +signs           +tag_binary      +title           +viminfo         -xfontset
    +autocmd         -clipboard       +cscope          -ebcdic          +find_in_path    +iconv           +lispindent      +mouse           -mouse_sysmouse  +path_extra      -python3         +smartindent     +tag_old_static  -toolbar         +vreplace        -xim
    -balloon_eval    +cmdline_compl   +cursorbind      -emacs_tags      +float           +insert_expand   +listcmds        -mouseshape      -mouse_urxvt     -perl            +quickfix        -sniff           -tag_any_white   +user_commands   +wildignore      -xsmp
    -browse          +cmdline_hist    +cursorshape     +eval            +folding         +jumplist        +localmap        -mouse_dec       +mouse_xterm     +persistent_undo +reltime         +startuptime     -tcl             +vertsplit       +wildmenu        -xterm_clipboard
    +builtin_terms   +cmdline_info    +dialog_con      +ex_extra        -footer          -keymap          -lua             -mouse_gpm       +multi_byte      +postscript      -rightleft       +statusline      +terminfo        +virtualedit     +windows         -xterm_save
    +byte_offset     +comments        +diff            +extra_search    +fork()          -langmap         +menu            -mouse_jsbterm   +multi_lang      +printer         +ruby/dyn        -sun_workshop    +termresponse    +visual          +writebackup     -xpm    system vimrc file: "$VIM/vimrc"
         user vimrc file: "$HOME/.vimrc"  2nd user vimrc file: "~/.vim/vimrc"
          user exrc file: "$HOME/.exrc"   fall-back for $VIM: "/usr/share/vim" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H    
    -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: gcc   -L/usr/local/lib -o vim        -lm -lncurses  -liconv
    
  • I had tried iTerm, Terminal, changed shell to bash, zsh, fish. Remove first line, set nocompatible, in .vimrc. But they all returned this result.

如果您需要更多关于我的电脑的配置信息,请留言,我会提供。

谢谢!


更新

因为我在当前shell基础上开了一个新的shell,比如通过命令zshbash。它忽略了我的.zshrc/.bash_profile

因此,$PATH不包括/usr/local/bin。然后,我使用默认的MacOS /usr/bin/vim版本,而不是Homebrew的vim8.0版本,因为which vim指向了默认的MacOS版本。

尝试通过command-T打开新的shell,并确保which vim返回/usr/local/bin/vim,即Homebrew的版本。vi:qecho $?输出了预期的结果0

这解决了退出vi后代码1的问题,这影响了git commit命令等等。但我仍然不明白为什么MacOS的默认版本vi会造成这种意外的行为。


复制粘贴所见的确切错误代码 - Inian
没有错误。在退出vi后,shell是干净的。我通过echo $?手动检查了上一个命令的退出代码,并得到了结果。 - transang
请参见http://unix.stackexchange.com/questions/14497/why-would-vim-return-non-zero-exit-code-if-i-exit-immediately-after-opening。 - chepner
哦,我忘了 ~/.vim 文件夹了。那里有很多插件。仅编辑 ~/.vimrc 是不够的。 - transang
顺便说一句,感谢提供链接。但是仍然不清楚为什么去掉“silent!unmap <C-E>”就可以正常运行。 - transang
显示剩余5条评论
1个回答

1

您不需要将vim设置为“nocompatible”,因为这是默认设置。

我在我的MacOS中删除了此功能,它可以正常工作并具有退出状态0,无论是否写入文件。

注意:在MacOS中,“vi”是指向“Vim”的链接(而“awk”实际上是“gawk”)

结论:您可以使用

silent! unmap <C-E>

在你的.vimrc文件中,Vim可能不会出现错误。

这个问题困扰了我很长时间,但并没有足够的动力去花时间研究为什么会发生。对于一个简洁的解决方案表示赞赏。 - Yarek T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接