[Question]
  [
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 will be this:
```
E37@vimmachine: /var/override)
```
While the error will be this:
```
E37: No write since last change (add ! to override)
```
Upon being given the bash prompt, the program's work is done, and it should not give any more output (except for whitespace).
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 [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so fewest bytes **in each language** wins!
  
[Answer]
# [Python 2](https://docs.python.org/2/), 66 bytes
Saved 3 bytes thanks to @Mr.Xcoder and 4 thanks to @Mayube!
```
from random import*
if'('in input(':q'):print choice([':x',':q!'])
```
[Try it online!](https://tio.run/##HYoxDsIwDEV3TuFOSREsMCBFYmTlAoBQlLjUErWDGygcnDmETu8//Zc@uRfeFOL0zLAH9dN13qVTGapyrKAhieblgjpjDTHMhTXucWbTuqTEGUIvFNCejHubVb0ac2lLOWx3Do4Ck1JGGIkDwt2P/9zzDcH6GKGBLCAvVKWI7ZdlHXzo8Qc "Python 2 – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/en/), 35 bytes
```
p:q
p %i(q! x)[rand 2]if/\(/=~gets
```
[Try it online!](https://tio.run/##KypNqvz/v8CqkKtAQTVTo1BRoUIzuigxL0XBKDYzTT9GQ9@2Lj21pPj/f1djcysFv3yF8qLMklSF4sy85FSFnMTiEoXkjMS89FQFjcSUFAVFhZJ8hfyy1KKizJRUza95@brJickZqQA)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 19 bytes
```
„:q,'Nåi„:x…:q!‚.R,
```
[Try it online!](https://tio.run/##MzBNTDJM/f//UcM8q0Iddb/DSzNBzIpHDcusChUfNczSC9L5/9/V2NxKwS9fobwosyRVoTgzLzlVISexuEQhOSMxLz1VQSMxJUVBUaEkXyG/LLWoKDMlVfNrXr5ucmJyRioA "05AB1E – Try It Online")
```
„:q,                # Print the string ":q"
    'Nåi            # If N is in the input
        „:x         #   Push the string ":x"
           …:q!     #   Push the string ":q!"
               ‚    #   Wrap them into an array
                .R, #   Randomly print one of them
```
[Answer]
# Vimscript, 45 41 bytes
```
ec"q"|exe"/("|ec["q!","x"][getpid()%2]
```
Shaved off 4 bytes by using `getpid()` to get a number. (Can anyone think of a shorter way to get a number that won't be the same everywhere, all the time?)
Assuming +3 bytes for invoking Vim with `-c` to run the above script and `-` to read input from stdin:
```
vim -c 'ec"q"|exe"/("|ec["q!","x"][reltime()[1]%2]' -
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~76~~ ~~75~~ ~~72~~ 69 bytes
**This answer has been [out-golfed](https://codegolf.stackexchange.com/a/127479).**
*-1 byte thanks to Rod. -3 bytes thanks to Mayube. -3 bytes thanks to Artyer.*
```
from random import*
print'::qx!'[random()>.5:('N'in input(':q'))*5:2]
```
[Try it online!](https://tio.run/##Jck7DsIwDADQnVO4k5NKMBRVSBnYuvYCiCFKAo1E7dQ1v4MzBySmN7zy1ompq/UiPIN4ij/yXFi03RTJpOjc8mrw9D9jj7veGRwxE2QqdzXoFrS27V13rhWH/cHByPCUrAnWTCHBza8KYfJ0TWB8jNCAMvAjieSYLH6It8GHKX0B "Python 2 – Try It Online")
[Answer]
## JavaScript, ~~52~~ 46 bytes
*Saved 6 bytes thanks to @ETHProductions*
```
prompt(':q')[30]&&alert(new Date%2?':q!':':x')
```
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 140 139 138 bytes
```
using static System.Console;class P{static void Main(){Write(":q");if(ReadLine()[3]==':')Write(new System.Random().Next()>9?":x":":q!");}}
```
[Try it online!](https://tio.run/##NcyxCsIwEIDhV4lZehns4mRKdXBVkTo4iENITzloL9iLWil99lhQ5//n8zL3ocOUHkJ8UxJdJK@Ob4nY5pvAEhosfONE1GH41WegWu0cMZjh1FFE0PauTUFXqNDVW2IEc15cyjKzmfkejK@/WjmuQwsm32MfwayWa217bSdjNiHjmNIH "C# (.NET Core) – Try It Online")
* 1 byte saved thanks to LiefdeWen!
* 1 byte saved thanks to Mord Zuber!
[Answer]
# [><>](https://esolangs.org/wiki/Fish), ~~32~~ ~~30~~ 29 bytes
* Had a redundant `!o`.
* -1 byte thanks to Aaron.
```
iii0["@!qx:q:"ooi{=?;o!ox~oo;
```
[Try it online!](https://tio.run/##HcLNCkBAFAbQV/nGirWFGomNrXeYzMUtzW1@QimvPqRzFo5bzsWg/KW9LkT4@d1d34r6XI9Im/NYNxqT4AycCJHdTNhNTJg341ZCaayFQhLIQSGwpeoF "><> – Try It Online")
## Explanation (old)
```
"@!qx:q:"ooi~i~i~i{=?;o!ox~oo;
"@!qx:q:"                        Push @!qx:q: on the stack (in reverse)
         oo                      Print :q [stack: @!qx:]
           i~i~i~                Remove first 3 input characters
                 i               Load @ or : from the input (= i)
                  {              Shift stack to the left [stack: !qx:i@]
                   =?            If 4th character was @
                     ;              Exit
                                 Else
                      o             Print :
                       !o           Jump to
                         x          Random direction (left or right because
                                    up and down retrigger x)
                                       Left:
                     ;o!o                 Print x and exit.
                                       Right:
                          ~oo;            Remove x, print q! and exit.
```
(will update soon)
[Answer]
# [Actually](https://github.com/Mego/Seriously), 27 bytes
```
":q"ü[":x",":q!"]`⌂`J'!,cIƒ
```
[Try it online!](https://tio.run/##S0wuKU3Myan8/1/JqlDp8J5oJasKJR0gW1EpNuFRT1OCl7qikquxuZWCX75CeVFmSapCcWZecqpCTmJxiUJyRmJeeqqCRmJKioKiQkm@Qn5ZalFRZkqqplKy57FJ//9/zcvXTU5MzkgFAA "Actually – Try It Online") (Note: As TIO doesn't play nice with stdin, I've replaced the `,` (read from stdin) with the literal string, which you can change to test it. Also, you'll need to disable output cache in order to get a different response with the random output.)
Explanation:
```
":q"ü[":x",":q!"]`⌂`J'!,cIƒ
":q"ü                       - Push the literal :q and print
     [":x",":q!"]           - Push the list of literals :x and :q!
                 `⌂`J       - Push function literals ⌂ (terminate) and J (choose random element from list)
                     '!     - Push the literal !
                       ,    - Read from stdin
                        c   - Pop top two values a,b and return "a".count(b)
                         I  - Pop top three values a,b,c and return b if a is truthy and c if a is falsy
                          ƒ - Pop a function from the stack and call it
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 27 bytes
```
':q'⋄('!'∊⍞)/':q!' ':x'⊃⍨?2
```
[Try it online!](https://tio.run/##dY09bsJAEIV7n2JcDRTI/BRI25DGRSREJMQFRt4RrGTvkmVlCC1RQCBHaVImNeeaO1AbB6J0eeXT975Hy7yjXyh381reP6djefvoglRf4MlqV5gtw@zxKZLDPt2YUKN6Rjm9tjBGORyl@m4nTRUjoNqgHHdSnUf9uqFruAV7qwAUAhfLoDC6lz@mKB0MH0pTFJQtjGUFSUk@cSV7bzS3f0nsW/3vXMHEwdqbwLAyNmPIqTnLFmTnDC3SGmIIDv6cF@s6WXPHVw "APL (Dyalog Unicode) – Try It Online")
`':q'` print this
`⋄` then
`?2` random integer among the first two
`⊃⍨` use that to select from
`':q!' ':x'` this list of two strings
`(`…`)/` replicate that with, i.e. make *n* copies of each element in that, where *n* is
 `'!'∊⍞` is exclamation point a member of the text input? (Boolean, i.e. 0 or 1)
[Answer]
## Batch, 98 bytes
```
@echo :q
@set/ps=
@if %s:~3,1%==@ exit/b
@set/an=%random%%%2
@if %n%==0 (echo :x)else echo :q!
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 33 bytes (32 code, +1 for -n flag)
```
p:q;$_[?!]? p([:q!,:x][$$%2]): p
```
I'm not too sure on the `$$%2` snippet. I'm using it to generate a random bit to index my array by taking the current ruby process's pid modulo 2, and it'll be fairly unpredictable. `.sample` adds an extra byte.
[Try it online!](https://tio.run/##KypNqvz/v8Cq0FolPtpeMdZeoUAj2qpQUceqIlavODG3ICdV00qh4P9/xX/5BSWZ@XnF/3XzAA "Ruby – Try It Online")
[Answer]
# JavaScript, ~~76~~ ~~71~~ 50 bytes
```
prompt(":q")[3]<"@"?alert(new Date%2?":q!":":x"):0
```
Saved a bunch of bytes thanks to Artyer and Neil, as well as condensing my ternaries and moving first alert to prompt (duh).
[Answer]
# [Python 2](https://docs.python.org/2/), 64 bytes
```
import time
if'w'in input(':q'):print'::qx!'[time.time()%2>1::2]
```
[Try it online!](https://tio.run/##HYpBCsIwFAX3nuJ1IUk3gnEhfNBdt15AREIT7QebpOnX6Okj7WYYmEk/GWIwlUN6C07IttxXrzymmAXCo9/wQxXFAWvRiibVUsocRBFN30Zdl2u3QLdbc94TmVut3eFIuESUzOIxc@g9XnYW9IMNTw9tnUMDiYgfnzM73/4B "Python 2 – Try It Online")
Since this is just an agglomeration of [both](https://codegolf.stackexchange.com/a/127479/48878) of [these](https://codegolf.stackexchange.com/a/127480/48878) Python answers, I've marked this as community wiki.
[Answer]
# [Python 2](https://docs.python.org/2/), 47 bytes
```
print'::qx!'[id(0)%3<2:('N'in input(':q'))*5:2]
```
[**Try it online**](https://tio.run/##BcHNCsIwDADgV8kOklYQpEOE4HXXvYB4KG2wAUm3Lv49uOf6fcvXStXQ@9JEDYnWz4BXye7od@MlkMMZRUF0eZpDWtH7/YnCrXecxjPBXOHdxBg20cTwiJtBKlHvDC7mDANYhfri1iSzx5/WQ4qp8B8)
**Explanation:**
The source of randomness is `id(0)%3<2`. The function `id` returns the memory location of the parameter. Passing anything will give a non-deterministic memory address. The result will always be even, but it will only sometimes be divisible by 3. The results modulo 3 vary. So, this can be a source of randomness. Using `<2` means that roughly 2/3 of the time, the resulting boolean is `True`.
The rest of the answer is inspired by [this one](https://codegolf.stackexchange.com/a/127480/34718).
[Answer]
## [Keg](https://esolangs.org/wiki/Keg), ~~14~~ 12 bytes
```
\:~2%[q\!|x]
```
This pushes a random number and outputs corresondingly to the random number.
[Answer]
# TCL, 95 bytes
```
puts :q;puts [if [string match *!* [gets stdin]] {puts [lindex ":q! :x" [expr round(rand())]]}]
```
A simple and rather long solution in tcl...
[Answer]
**Assembly 86Bytes** Linux System Calls
```
global _start
_start:
mov edx,4
mov ecx,g 
mov ebx,1
mov eax,4
int 0x80
g db ':q!',10
```
[Answer]
# LOGO, ~~37~~ 36 bytes
```
pr[:q]if 4<count rl[pr pick[:q! :x]]
```
Try it online at [Anarchy Golf Performance checker](http://golf.shinh.org/check.rb).
# Explanation:
```
pr[:q]if 4<count rl[pr pick[:q! :x]]   Main program.
pr                                     Print
  [:q]                                 the list `[:q]` without delimiter `[]`
      if           [              ]    If...
         4<                            4 is less than...
           count                       the number of word in the...
                 rl                    ReadLine (from stdin),
                    pr                 print
                       pick            a random word picked from the list...
                           [:q! :x]    [:q! :x]
```
(because `E37@vimmachine: /var/override)` has 2 words, while `E37: No write since last change (add ! to override)` has 10 words)
[Answer]
# Perl 5 (with -E flag), 35 bytes
```
say":q";<>=~l&&say qw(:q! :x)[$$%2]
```
Run this with `perl -E 'say":q";<>=~l&&say qw(:q! :x)[$$%2]'`.
[Try it online!](https://tio.run/##K0gtyjH9X1qcqlBmqmdoYP2/OLFSyapQydrGzrYuR00NyFUoLNewKlRUsKrQjFZRUTWK/f/f1djcSsEvX6G8KLMkVaE4My85VSEnsbhEITkjMS89VUEjMSVFQVGhJF8hvyy1qCgzJVUTAA "Perl 5 – Try It Online")
Ungolfed:
```
say(":q");
if(<> =~ /l/){ #read a line and check if it contains 'l'
    @a=(":q!",":x");
    say($a[$$ % 2]); #print one element from array based on PID
}
```
]