Skip to content

Instantly share code, notes, and snippets.

@dsedivec
Last active August 29, 2015 13:57
Show Gist options
  • Save dsedivec/9476955 to your computer and use it in GitHub Desktop.
Save dsedivec/9476955 to your computer and use it in GitHub Desktop.
vimkill command: pgrep + vim + kill
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