zsh:funcs: Rename pgrep to psgrep
Instead of replacing pgrep, have this function under a slightly different name. Also add support for multiple arguments as the function does not aim to be called under the same name now.
This commit is contained in:
@@ -482,31 +482,21 @@ _page_readme_chpwd_handler() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Improved pgrep
|
|
||||||
# I sometimes find `pgrep` not matching the processes I am searching for, but
|
# I sometimes find `pgrep` not matching the processes I am searching for, but
|
||||||
# `ps aux | grep ...` did not disappoint yet.
|
# `ps aux | grep ...` did not disappoint yet.
|
||||||
pgrep() {
|
psgrep() {
|
||||||
# Fall back to real `pgrep` if options are specified. I have no interest in
|
|
||||||
# emulating `pgrep` features with `ps` and other tools. This function is
|
|
||||||
# only meant for quick searches
|
|
||||||
if (( $# > 1 )); then
|
|
||||||
command pgrep "$@"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
# - Set EXTENDED_GLOB for the `b` globbing flag.
|
# - Set EXTENDED_GLOB for the `b` globbing flag.
|
||||||
# - Set UNSET so that no arguments can be specified leading to `grep ""`
|
emulate -L zsh -o extendedglob
|
||||||
# matching everything, as `ps aux` without pipe would be my desired
|
|
||||||
# behavior.
|
|
||||||
emulate -L zsh -o extendedglob -o unset
|
|
||||||
|
|
||||||
|
for arg; do
|
||||||
# Substitute the captured first character with itself surrounded by
|
# Substitute the captured first character with itself surrounded by
|
||||||
# brackets. The `(#b)` turns on backreferences, storing the match in the
|
# brackets. The `(#b)` turns on backreferences, storing the match in the
|
||||||
# array $match (in this case with only one element).
|
# array $match (in this case with only one element).
|
||||||
# So for example: "pattern" -> "[p]attern"
|
# So for example: "pattern" -> "[p]attern"
|
||||||
# This has the effect that the `grep` does not grep itself in the processes
|
# This has the effect that the `grep` does not grep itself in the processes
|
||||||
# list.
|
# list.
|
||||||
ps aux | grep "${1/(#b)(?)/[$match]}"
|
ps aux | grep "${arg/(#b)(?)/[$match]}"
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use shellcheck.net if shellcheck is not installed.
|
# Use shellcheck.net if shellcheck is not installed.
|
||||||
|
|||||||
Reference in New Issue
Block a user