ziishaned
1/27/2019 - 11:41 AM

Open file links in sublime from iTerm

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