Skip to content

Instantly share code, notes, and snippets.

@ivan
Created January 11, 2017 05:38
Show Gist options
  • Save ivan/0bdd4c5d37dc74f90089050136a9f36c to your computer and use it in GitHub Desktop.
Save ivan/0bdd4c5d37dc74f90089050136a9f36c to your computer and use it in GitHub Desktop.
How to open files in the correct Sublime Text window instead of the most-recently-used Sublime Text window
#!/bin/bash
# Do not -o pipefail because xdotool returns exit code 1 if no match
set -eu
# For debugging:
# set -o verbose
# When called from the command line, Sublime Text opens files in the most-recently-used window,
# and provides no option for controlling this behavior. Work around this by searching for the
# window we *don't* want the file opened in, finding the other sublime_text window, then raising
# ("activating") it before opening the file.
UNWANTED_WID=$(xdotool search --all --desktop 0 --class --name '^(sublime_text|~/wiki/scratchpad\.md .*- Sublime Text)$' | head -n 1)
if [[ ! -z "${UNWANTED_WID:=}" ]]; then
WANTED_WID=$(xdotool search --all --desktop 0 --class '^sublime_text$' | fgrep -v "$UNWANTED_WID" | head -n 1)
xdotool windowactivate "$WANTED_WID"
# Wait for window to actually activate before opening the file
sleep 0.06
fi
/opt/sublime_text/sublime_text "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment