mirror of
https://github.com/hakluke/how-to-exit-vim.git
synced 2025-05-03 20:26:21 +02:00
214 lines
5.1 KiB
Markdown
214 lines
5.1 KiB
Markdown
# How to exit vim
|
|
Below are some simple methods for exiting vim.
|
|
|
|
## The simple way
|
|
Credit: @tomnomnom
|
|
|
|
```
|
|
:!ps axuw | grep vim | grep -v grep | awk '{print $2}' | xargs kill -9
|
|
```
|
|
|
|
## The ps-less process tree way
|
|
Credit: @kpumuk
|
|
|
|
```
|
|
:!grep -P "PPid:\t(\d+)" /proc/$$/status | cut -f2 | xargs kill -9
|
|
```
|
|
|
|
## The ps-less way
|
|
Credit: @tomnomnom
|
|
|
|
```
|
|
:!kill -9 $(find /proc -name "cmdline" 2>/dev/null | while read procfile; do if grep -Pa '^vim\x00' "$procfile" &>/dev/null; then echo $procfile; fi; done | awk -F'/' '{print $3}' | sort -u)
|
|
```
|
|
|
|
## The ps-less way using status files
|
|
Credit: @hakluke
|
|
|
|
```
|
|
:!find /proc -name status | while read file; do echo "$file: "; cat $file | grep vim; done | grep -B1 vim | grep -v Name | while read line; do sed 's/^\/proc\///g' | sed 's/\/.*//g'; done | xargs kill -9
|
|
```
|
|
|
|
## The pythonic way
|
|
Credit: @hakluke
|
|
|
|
```
|
|
:py3 import os,signal;from subprocess import check_output;os.kill(int(check_output(["pidof","vim"]).decode
|
|
('utf-8')),signal.SIGTERM)
|
|
```
|
|
|
|
## The Colon-less way
|
|
Credit: @w181496
|
|
|
|
In insert mode:
|
|
```
|
|
<C-R>=system("ps axuw | grep vim | grep -v grep | awk '{print $2}' | xargs kill -9")
|
|
```
|
|
|
|
## The remote way
|
|
Credit: @eur0pa
|
|
|
|
In `vi`:
|
|
```
|
|
:%!( key="kill-vi-$RANDOM"; nc -l 8888 | if grep $key; then pgrep '^vi$' | xargs kill; fi; ) &
|
|
```
|
|
|
|
Remotely:
|
|
```
|
|
$ while true; do curl http://vi-host:8888/kill-vi-$RANDOM; done
|
|
```
|
|
`vi` will eventually exit
|
|
|
|
Locally (the cheaty, lazy way, why even bother):
|
|
```
|
|
$ curl "http://localhost:8888/$(ps aux | grep -E -o 'kill-vi-[0-9]+')"
|
|
```
|
|
|
|
## The hardware way
|
|
Credit: @Jorengarenar
|
|
|
|
_**Pull the plug out**_
|
|
|
|
## The timeout way
|
|
|
|
Credit: @aarongorka
|
|
|
|
Before running vim, make sure to set a timeout:
|
|
```
|
|
$ timeout 600 vim
|
|
```
|
|
Never forget to set a timeout again:
|
|
```
|
|
$ alias vim='timeout 600 vim'
|
|
```
|
|
Make sure to save regularly.
|
|
|
|
## The Russian Roulette timeout way
|
|
|
|
When you want to spice things up a bit:
|
|
```
|
|
$ timeout $RANDOM vim
|
|
```
|
|
|
|
## The physics way
|
|
Credit: @eyemyth
|
|
|
|
Accumulate a sufficient amount of entropy.
|
|
|
|
|
|
## The reboot way
|
|
Credit: @tctovsli
|
|
In `vi`:
|
|
```
|
|
:!sudo reboot
|
|
```
|
|
|
|
## The using vim against itself way (executing the buffer)
|
|
Open Vim to empty buffer and type:
|
|
```
|
|
i:qa!<esc>Y:@"<cr>
|
|
```
|
|
|
|
## The AppleScript way
|
|
Credit: @dbalatero
|
|
In Mac terminal `vi`:
|
|
|
|
Replace "iTerm" with your terminal application of choice:
|
|
|
|
```
|
|
:let script="activate application \"iTerm\"\ntell application \"System Events\"\n keystroke \":\"\n keystroke \"q\"\n keystroke \"a\"\n keystroke \"!\"\n key code 36\nend tell" | call writefile(split(script, "\n", 1), '/tmp/exit-vim.scpt', 'b') | !osascript /tmp/exit-vim.scpt
|
|
```
|
|
|
|
## The Mac Activity Monitor way
|
|
Credit: @dbalatero
|
|
|
|
```
|
|
let script="activate application \"Activity Monitor\"\ntell application \"System Events\"\n\tkeystroke \"f\" using {option down, command down}\n\tkeystroke \"vim\"\n\n\ttell process \"Activity Monitor\"\n\t\ttell outline 1 of scroll area 1 of window 1\n\t\t\tselect row 1\n\n\t\t\tkeystroke \"q\" using {option down, command down}\n\t\t\tkey code 36\n\t\tend tell\n\tend tell\nend tell\n" | call writefile(split(script, "\n", 1), '/tmp/exit-vim.scpt', 'b') | !osascript /tmp/exit-vim.scpt
|
|
```
|
|
|
|
## The Passive Way
|
|
|
|
_**Walk away.**_
|
|
|
|
## The Passive-Agressive Way
|
|
|
|
```
|
|
!bash -c "💣(){ 💣|💣& };💣"
|
|
```
|
|
|
|
*...then walk away.* (n.b. That's a [fork bomb](https://en.wikipedia.org/wiki/Fork_bomb#Bash), please don't try at home.)
|
|
|
|
## The Microsoft Way
|
|
Credit: @cheezmeister
|
|
|
|
```
|
|
!powershell.exe /c "get-process gvim | stop-process"
|
|
```
|
|
|
|
## The C way
|
|
Credit: @dbalatero
|
|
|
|
```
|
|
:let script=['#define _POSIX_SOURCE', '#include <signal.h>', '', "int main() {", " kill(" . getpid() . ", SIGKILL);", ' return 0;', '}'] | call writefile(script, '/tmp/exit_vim.c', 'b') | execute "!gcc /tmp/exit_vim.c -o /tmp/exit_vim" | execute "! /tmp/exit_vim"
|
|
```
|
|
|
|
## The Emacs way
|
|
Credit: @dbalatero
|
|
|
|
```
|
|
:let command='emacs --batch --eval=''(shell-command "kill -9 ' . getpid() . '")'' --kill' | execute "!" . command
|
|
```
|
|
|
|
## The Vim way
|
|
Credit: @david50407
|
|
|
|
```
|
|
:let command='vim ''+\\!kill -9 ' . getpid() . ''' +qall -es' | execute "!" . command
|
|
```
|
|
|
|
## The Client-Server way
|
|
Credit: @tartansandal
|
|
|
|
If `+clientserver` is enabled -- typically the case for the GUI -- you can simply
|
|
|
|
```
|
|
:!gvim --remote-send ':q\!<CR>'
|
|
```
|
|
|
|
## The Yolo Way
|
|
Credit: @ryanc
|
|
|
|
Don't run this, it could break your computer.
|
|
|
|
```
|
|
:!echo b | sudo tee -a /proc/sysrq-trigger
|
|
```
|
|
|
|
## The Abstinence Method
|
|
Credit: @ryanc
|
|
|
|
```
|
|
$ alias vim=/bin/true
|
|
```
|
|
|
|
## The shortest way
|
|
Credit: @MasterDevX
|
|
|
|
```
|
|
:!x=$(echo "c"); x=$x$(echo "G"); x=$x$(echo "t"); x=$x$(echo "p"); x=$x$(echo "b"); x=$x$(echo "G"); x=$x$(echo "w"); x=$x$(echo "g"); x=$x$(echo "L"); x=$x$(echo "V"); x=$x$(echo "N"); x=$x$(echo "U"); x=$x$(echo "T"); x=$x$(echo "1"); x=$x$(echo "A"); x=$x$(echo "g"); x=$x$(echo "d"); x=$x$(echo "m"); x=$x$(echo "l"); x=$x$(echo "t"); x=$x$(echo "C"); x=$x$(echo "g"); x=$x$(echo "="); x=$x$(echo "="); $(echo $x | base64 --decode)
|
|
```
|
|
|
|
## The Docker way
|
|
Credit: @tartansandal
|
|
|
|
If you run Vim in a docker container like:
|
|
|
|
```
|
|
docker run --rm -it --name my-vim -v `pwd`:/root thinkca/vim
|
|
```
|
|
|
|
then you would normally exit vim by stopping the associated container:
|
|
|
|
```
|
|
docker stop my-vim
|
|
```
|