37

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!

2
  • 2
    Hmm. I don't mean to object overmuch, but asking how to make something automatic and then accepting a purely manual answer seems a bit... surprising. Commented Sep 11, 2018 at 15:29
  • good point - i forgot about that, and just thought the "no code" solution was probably easier for most people - but you're right for sure, and I'm switching back to your answer ;-) Thanks! Commented Sep 11, 2018 at 15:45

5 Answers 5

48

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I like the script approach, but appreciate this answer as well, as I think this would work on other systems - though I feel I've tried this before in some lock up scenarios and it didn't help. I did just try it by doing something like so: vim $(vim /tmp/it.json) which happens to me sometimes when I recall the last command to edit a file and don't do an expansion first.
@BradParks, for that case vim $(vim x) you can exit by typing :q<Enter> twice.
In my case it was <ESC> and then force quite with :q<Enter>
33

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

2 Comments

+1 Both Ctrl-Z and Ctrl-C didn't work for me, it returned me a message job can't be suspended without being able to do anything. This ZQ worked, thanks!
ZQ saved the day for me
19

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.

3 Comments

How does it work? Which program would be calling the vim function?
@user2023370, "which program"? The user's interactive shell. (If that shell isn't bash, one would want to put it in a different file as appropriate).
@user2023370 The program who made the bug... When the bug is located between the keyboard and the chair. ;-)
12

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

Comments

1

If any of the above doesn't work then try using the following: press esc once and then use either

:qa

or

SHIFT ZZ

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.