我想验证我的~/.vimrc文件是否正确,基本上只需启动vim并立即运行quit。我几乎可以用vim -c quit实现这一点,只是我必须按enter键。例如,
> cat ~/.vimrc
echoerr 'test'
> vim -c quit
Error detected while processing /home/josh/.vimrc:
line 1:
test
Press ENTER or type command to continue有没有办法做到这一点而不按下enter?我想我可以运行echo | vim -c quit,但我希望有一个更优雅的解决方案。
发布于 2019-01-21 08:02:31
您可以使用try-catch块捕获错误(这些错误被转换为异常),并在任何异常上失败:
$ cat vimrc-fail
echoerr 'test'
$ cat vimrc-pass
echo 'test'
$ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
0
$ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
1来自帮助:
:try :try :endt :endtry E600 E601 E602
:endt[ry] Change the error handling for the commands between
":try" and ":endtry" including everything being
executed across ":source" commands, function calls,
or autocommand invocations.来自:echoerr:
当在try条件中使用时,该消息将作为错误异常引发(请参见试着回波)。
其他说明:
-u NONE阻止获取默认的vimrcs:cq使Vim返回非零退出状态。要获取最后一条错误消息(现在实际上是一个异常),请使用v:exception:
$ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
Vim(echoerr):test
1https://unix.stackexchange.com/questions/495721
复制相似问题