Skip to main content
2 of 5
edited body; edited title
Mayube
  • 11.7k
  • 2
  • 48
  • 93

How do I exit Vim?

Vim is a great text editor for unix systems, but it's notorious for being difficult to exit.

Write a full program that will output :q to exit Vim. It should then read a single line of input, as it will then either be given a bash prompt, in which case the exit was successful, or an error, in which case there are unsaved changes.

The bash prompt looks like this:

E37@vimmachine: /var/override)

While the error looks like this:

E37: No write since last change (add ! to override)

Upon being given the bash prompt, the program's work is done, and it may terminate.

Upon being given the error, your program should randomly (ie each possibility has a non-zero probability of being chosen) output :q!, to exit without saving, or :x, to save and exit.

This is so fewest bytes in each language wins!

Mayube
  • 11.7k
  • 2
  • 48
  • 93