Last active
August 29, 2015 13:57
-
-
Save dsedivec/9476955 to your computer and use it in GitHub Desktop.
vimkill command: pgrep + vim + kill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vimkill () { | |
local temp_file | |
temp_file=$(mktemp -t vimkillXXXXXXXXXX) | |
if [ $? -ne 0 ]; then | |
echo "mktemp failed" >&2 | |
return 1 | |
fi | |
if ! pgrep -fl "$@" > "$temp_file"; then | |
rm "$temp_file" | |
echo "no matching processes found" >&2 | |
return 1 | |
fi | |
cat >> "$temp_file" <<-EOF | |
# Delete lines you don't want | |
# Feel free to add additional PIDs, one per line | |
# Lines that don't start with a number will be ignored | |
# Exit non-zero (Vim: :cquit) to abort or just delete all lines | |
EOF | |
local status=1 | |
if "${EDITOR:-vi}" "$temp_file"; then | |
pids=$(awk -v ORS=' ' '/^[0-9]+/{print $1}' "$temp_file") | |
if [ -n "$pids" ]; then | |
echo "Killing $pids" | |
kill "$pids" | |
status=$? | |
else | |
echo "no PIDs to kill" >&2 | |
fi | |
else | |
echo "editor exited non-zero" >&2 | |
fi | |
rm "$temp_file" | |
return $status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment