Skip to content

Instantly share code, notes, and snippets.

@ziishaned
Created January 27, 2019 11:41
Show Gist options
  • Save ziishaned/18a478b819de0deda62969bd9395640d to your computer and use it in GitHub Desktop.
Save ziishaned/18a478b819de0deda62969bd9395640d to your computer and use it in GitHub Desktop.
Open file links in sublime from iTerm
#!/bin/sh
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|) { 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
@ziishaned
Copy link
Author

ziishaned commented Jan 27, 2019

  1. Open Preferences > Profiles > Default > Advanced
  2. From Semantic History choose Run command...
  3. Write following in input field:
    /your/path/to/iterm_open_with_subl \5 \1 \2
    

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