Skip to content

Instantly share code, notes, and snippets.

@bassmanitram
Created April 21, 2022 08:51
Show Gist options
  • Save bassmanitram/fda20a289c7eaa12160cf69912286fd8 to your computer and use it in GitHub Desktop.
Save bassmanitram/fda20a289c7eaa12160cf69912286fd8 to your computer and use it in GitHub Desktop.
A script to raise a GVIM window if the file is already being edited
#
# Only use for simple executions of GVIM
#
if [[ "$1" == -f ]]; then
file=$2
else
file=$1
fi
if [[ -e "$file" ]]; then
#
# The file exists so let's see if we are already editing
#
# List its long-form process details
#
psline=( $(ps -C gvim -o pid= -o args= | grep "gvim.* $file" 2>/dev/null) )
#
# If it exists, the first token will be the pid
#
GVIMPID=${psline[0]}
if [[ "$GVIMPID" ]]; then
#
# It's already active - raise it
#
#
# Find the window ID
#
WINID=( $(wmctrl -lp | awk '{print $3 " " $1}' | grep "^$GVIMPID" | awk '{print $2}') )
#
# If only one window ID was found, then raise it and exit
#
if [[ "${WINID[0]}" && -z "${WINID[1]}" ]]; then
wmctrl -ia ${WINID[0]}
[[ $? -eq 0 ]] && exit 0
fi
fi
fi
#
# In all other circumstances, execute real gvim
#
exec /usr/bin/gvim "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment