how-to-exit-vim
how-to-exit-vim copied to clipboard
Simpler simple way
In the current simple way, two grep commands are piped one after the other. The first looks up process containing vim, the second excludes the line corresponding to grep vim
.
The following does the same using just one grep
:
:!ps axuw | grep [v]im | awk '{print $2}' | xargs kill -9
Would you consider updating README accordingly?
P.S. Someone on hackerNews posted an even shorter syntax, c.f. this comment.
@mef I think “simple” was sarcastic. The man who wrote the simple way is a vim expert and Linux lover.
Still, this is an anti-pattern very commonly seen in shell scripts and snippets, and it's worth pointing it out, for educational purposes. There are 3 problems with it:
- Most ps flags are unnecessary. If
vim
is run in a terminal,ps
will run in the same terminal, so nops
flags are needed. If not, (e.g.gvim
), onlyx
is necessary, to capture processes without a tty. - The
[v]im
idiomatic trick works around the need to filter outgrep
from the process list. -
grep | awk
is a general (and common) anti-pattern, asawk
program syntax (pattern { action }
) has built in pattern matching.
Putting it all together, the POSIX idiomatic way would be:
:!ps x | awk '/[v]im/ {print $1}' | xargs kill -9
Alternatively, grep | cut
can be used instead of awk
, with GNU tools this could reduce memory usage by ~2 megabytes (with 1 additional process, and similar execution time):
:!ps x | grep [v]im | cut -c-7 | xargs kill -9
The two ps-less ways are also overly complicated, spawning too many processes and using too many tools (find(1)
, awk(1)
, sed(1)
, sort(1)
)... This can (on Linux) be as simple and fast as:
:!grep -l vim$ /proc/*/comm | cut -d/ -f3 | xargs kill -9
Don't overlook the anti-pattern of using SIGKILL
rather than SIGHUP
. The instructions contain more anti-patterns than you think.
@jdebp It seems that gvim
traps and ignores SIGHUP
. SIGTERM
would be a more elegant solution, allowing vim
to clean up on exit.
:!grep -l vim$ /proc/*/comm | cut -d/ -f3 | xargs kill -9
you should submit a "memory efficient" way. Also make the suggestion to use SIGTERM (15) instead of SIGKILL (9) :smile_cat:
Honestly I think the ... |grep [v]im | ...
is an extremely important point to make. piping to grep twice can cause a serious burden on CPU threads on modern x86_64 Architectures. Please consider tagging this issue as "critical"
@dkalaluhi That's a straw man argument, as no one is trying to make a point of performance in this thread. The point we are trying to make is that clear, concise and idiomatic code is a good thing. Messy, long-winded code causes a cognitive burden on developers maintaining it and decreases development velocity.
Sarcasm, ya know?
On Mon, Feb 17, 2020 at 8:49 AM Vladimir Vrzić [email protected] wrote:
@dkalaluhi https://github.com/dkalaluhi That's a straw man argument, as no one is trying to make a point of performance in this thread. The point we are trying to make is that clear, concise and idiomatic code is a good thing. Messy, long-winded code causes a cognitive burden on developers maintaining it and decreases development velocity.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hakluke/how-to-exit-vim/issues/64?email_source=notifications&email_token=AB3PROCASGJ5US5PEXFCZGTRDKIVHA5CNFSM4KEFBH3KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL6PECI#issuecomment-587002377, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB3PROCIKE63JIP72HN2CXLRDKIVHANCNFSM4KEFBH3A .