Bash:当“Vim:警告:不输出到终端”时杀死 Vim

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46432027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:28:54  来源:igfitidea点击:

Bash: Kill Vim when "Vim: Warning: Output not to a terminal"

bashvim

提问by Brad Parks

Every once in awhile I make a mistake at the command line, and use vim in a subshell.

每隔一段时间我就会在命令行上犯一个错误,并在子 shell 中使用 vim。

This of course locks up that terminal window, and outputs a bunch of errors, with the main error being

这当然会锁定那个终端窗口,并输出一堆错误,主要错误是

Vim: Warning: Output not to a terminal

Is there a way for me to configure vim so it automatically dies when this happens, and doesn't hang my terminal?

有没有办法让我配置 vim 以便在发生这种情况时它会自动终止,并且不会挂起我的终端?

I know I could probably figure out the process id of this vim instance, and kill it, but I would like a better solution if possible, as I tend to run lots of different vim instances in different tmux panes/windows. Thanks!

我知道我可能会找出这个 vim 实例的进程 ID,并杀死它,但如果可能的话,我想要一个更好的解决方案,因为我倾向于在不同的 tmux 窗格/窗口中运行许多不同的 vim 实例。谢谢!

采纳答案by Charles Duffy

You can prevent it from starting in the first place easily enough. Consider putting the following function definition in your .bashrc:

你可以很容易地防止它从一开始就开始。考虑将以下函数定义放入您的.bashrc:

vim() {
  [ -t 1 ] || { echo "Not starting vim without stdout to TTY!" >&2; return 1; }
  command vim "$@"
}

The commandbuiltin prevents recursing, by ensuring that it invokes an external command (rather than just calling the function again).

所述command内置防止递归,通过确保它调用一个外部命令(而不是仅仅再次调用功能)。

Similarly, you could create a script $HOME/bin/vim:

同样,您可以创建一个脚本$HOME/bin/vim

#!/bin/sh
if [ -t 1 ]; then
  exec /usr/bin/vim "$@"
else
  echo "Not starting vim without stdout to TTY!" >&2
  exit 1
fi

...put $HOME/binfirst in your PATH, and let that shim do the work without relying on a shell function.

...$HOME/bin首先放入您的PATH, 并让该 shim 完成工作而不依赖于 shell 函数。

回答by toth

Not aware of any configuration option that does this, but when this happens if you type :q<Enter>, it will quit vim.

不知道有任何配置选项可以执行此操作,但是当您键入时发生这种情况时:q<Enter>,它将退出 vim。

Also, while Ctrl-C will not work, Ctrl-Z will put vim in the background and then you can kill it with kill %1.

此外,虽然 Ctrl-C 不起作用,但 Ctrl-Z 会将 vim 置于后台,然后您可以使用kill %1.

回答by Krischa Onarestlessday

Or the vim short cut
ZQ
Capital letters. You can use it in normal mode :-)

或 vim 捷径
ZQ
大写字母。您可以在正常模式下使用它 :-)

回答by TheCodeTinkerer

You can just type this and hit enter, even though it is not showing up in the stdout it is still being input:

你可以输入这个并按回车键,即使它没有出现在标准输出中,它仍然被输入:

  1. type the below :q!

  2. Execute keyboard key Return/Enter

  1. 输入以下内容:q!

  2. 执行键盘键 Return/Enter