-1

I have this alias to kill all neovim instance:

alias killnvim="ps -ef | grep nvim | awk '{print $2}' | xargs kill &> /dev/null"

Each time I run it it always return this message:

[1]    20958 done        ps -ef |
       20959 done        grep --color=auto nvim |
       20960 done        awk '{print }' |
       20961 terminated  xargs kill &> /dev/null

What should I do to suppress that message? I want my command to output nothing.

I'm using zsh if that matter.

Thanks.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
rahmat
  • 1,727
  • 16
  • 35

2 Answers2

0

It's a quoting problem. You need the {print $2} to be in single quotes so that the invocation of the alias doesn't replace the $2 with a value, and for the whole alias to be in single quotes to stop $2 being replaced at definition time.

zsh does allow two single quotes to be replaced by one quote in a single-quoted string, but you need to set an option to allow that.

More portable is to use the following:

alias killnvim='ps -ef | grep nvim | awk '"'"'{print $2}'"'"' | xargs kill &> /dev/null'

You could also escape any sensitive characters, but the above seems to work.

simon3270
  • 722
  • 1
  • 5
  • 8
0

Unless you have a good reason to use an alias, prefer a function:

killnvim () {
  ps -ef | grep nvim | awk '{print $2}' | xargs kill &> /dev/null
}

(That said, there are better alternatives to rolling your own pipeline to kill a process by name.)

chepner
  • 497,756
  • 71
  • 530
  • 681