hakluke / how-to-exit-vim

Below are some simple methods for exiting vim.
https://hakluke.com
7.02k stars 315 forks source link

Simpler simple way #64

Open mef opened 4 years ago

mef commented 4 years ago

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?

mef commented 4 years ago

P.S. Someone on hackerNews posted an even shorter syntax, c.f. this comment.

jthack commented 4 years ago

@mef I think “simple” was sarcastic. The man who wrote the simple way is a vim expert and Linux lover.

vrza commented 4 years ago

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:

  1. Most ps flags are unnecessary. If vim is run in a terminal, ps will run in the same terminal, so no ps flags are needed. If not, (e.g. gvim), only x is necessary, to capture processes without a tty.
  2. The [v]im idiomatic trick works around the need to filter out grep from the process list.
  3. grep | awk is a general (and common) anti-pattern, as awk 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
vrza commented 4 years ago

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
vrza commented 4 years ago

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
jdebp commented 4 years ago

Don't overlook the anti-pattern of using SIGKILL rather than SIGHUP. The instructions contain more anti-patterns than you think.

vrza commented 4 years ago

@jdebp It seems that gvim traps and ignores SIGHUP. SIGTERM would be a more elegant solution, allowing vim to clean up on exit.

mupi2k commented 4 years ago

:!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:

dkalaluhi commented 4 years ago

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"

vrza commented 4 years ago

@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.

dkalaluhi commented 4 years ago

Sarcasm, ya know?

On Mon, Feb 17, 2020 at 8:49 AM Vladimir Vrzić notifications@github.com 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 .