Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

vim

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

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?

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!

like image 963
Brad Parks Avatar asked Sep 26 '17 16:09

Brad Parks


4 Answers

Not aware of any configuration option that does this, but when this happens if you type :q<Enter>, it will quit 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.

like image 94
toth Avatar answered Nov 18 '22 05:11

toth


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

like image 29
Krischa Onarestlessday Avatar answered Nov 18 '22 05:11

Krischa Onarestlessday


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

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

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

Similarly, you could create a script $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/bin first in your PATH, and let that shim do the work without relying on a shell function.

like image 16
Charles Duffy Avatar answered Nov 18 '22 03:11

Charles Duffy


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

like image 11
TheCodeTinkerer Avatar answered Nov 18 '22 05:11

TheCodeTinkerer