Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Forked from sos4nt/iterm_open_with
Last active June 26, 2021 01:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save trinitronx/f59a8308d42d71fdba41 to your computer and use it in GitHub Desktop.
Save trinitronx/f59a8308d42d71fdba41 to your computer and use it in GitHub Desktop.
Semantic history command for iTerm2 and Sublime Text 3. Allows iTerm integration of Command+Click to open a file in default app (if non-text), or Sublime Text with optional line number and column. Detects relative paths based on PWD.
#!/bin/sh
# iterm_open_with - open a URL, file from CWD, full path, or path with linenumber in default app or Sublime Text if text file
# For usage with iTerm2:
# In iTerm's Preferences > Profiles > Default > Advanced > Semantic History,
# choose "Run command..." and enter "/your/path/to/iterm_open_with \5 \1 \2".
# Usage: iterm_open_with $(pwd) filename [linenumber]
# $(pwd) = current working directory (either use `pwd` or $PWD)
# filename = filename to open
# lineno = line number
pwd=$1
file=$2
regex='https?://([a-z0-9A-Z]+(:[a-zA-Z0-9]+)?@)?[-a-z0-9A-Z\-]+(\.[-a-z0-9A-Z\-]+)*((:[0-9]+)?)(/[a-zA-Z0-9;:/\.\-_+%~?&@=#\(\)]*)?'
perl -e "if ( \"$file\" =~ m|$regex|i) { exit 0 } else { exit 1 }"
if [ $? -ne 0 ]; then
# if it's not a url, try splitting by ':'
arr=($(echo $2 | tr ':' "\n"))
file=${arr[0]}
lineno=${arr[1]:-$3}
colno=${arr[2]:-${3##*:}}
[ -e "$file" ] || file=${pwd}/${file}
fi
file "$file" | grep -q "text"
if [ $? -ne 0 ]; then
/usr/bin/open $file
else
/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ${file}${lineno:+:${lineno}}${colno:+:${colno}}
fi
@trinitronx
Copy link
Author

Semantic history command for iTerm2 and Sublime Text 3. In iTerm's Preferences > Profiles > Default > Advanced > Semantic History, choose "Run command..." and enter "/your/path/to/iterm_open_with \5 \1 \2".

@peterjaap
Copy link

Thanks, I adjusted it a bit for PhpStorm; https://gist.github.com/peterjaap/87355e0bed3a28cdf56035fe513fa4b5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment