how-to-exit-vim icon indicating copy to clipboard operation
how-to-exit-vim copied to clipboard

Simpler simple way

Open mef opened this issue 5 years ago • 11 comments

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 avatar Jan 08 '20 09:01 mef

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

mef avatar Jan 08 '20 09:01 mef

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

jthack avatar Jan 08 '20 11:01 jthack

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 avatar Jan 08 '20 13:01 vrza

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 avatar Jan 08 '20 13:01 vrza

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

vrza avatar Jan 08 '20 14:01 vrza

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

jdebp avatar Jan 08 '20 15:01 jdebp

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

vrza avatar Jan 08 '20 16:01 vrza

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

mupi2k avatar Jan 09 '20 18:01 mupi2k

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 avatar Feb 04 '20 16:02 dkalaluhi

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

vrza avatar Feb 17 '20 13:02 vrza

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 .

dkalaluhi avatar Feb 17 '20 14:02 dkalaluhi