Skip to content

Instantly share code, notes, and snippets.

@bcarlin
Last active July 12, 2019 17:23
Show Gist options
  • Save bcarlin/5318397 to your computer and use it in GitHub Desktop.
Save bcarlin/5318397 to your computer and use it in GitHub Desktop.
Alias for sublime text to automatically open a project when calling "subl" if a .sublime-project file is found in the current directory.This alias declaration to put in a bashrc file.
# [...]
function project_aware_subl {
project_file=$(ls *.sublime-project 2>/dev/null | head -n 1)
command subl ${*:-${project_file:-.}}
}
alias subl="project_aware_subl"
# [...]
@trisweb
Copy link

trisweb commented Jul 12, 2019

This is such a great concept! I ran into the same exact problem -- I have a project config I always want to use, since it has folder excludes and everything, and it's in the same folder as my actual project; but I always forget to open it because I just use subl . habitually.

This almost worked for me. I ran into a weird recursion if I sourced the profile more than once (since subl is already a function defined as itself). Using the command command bypasses functions for that and resolved. Here's what I ended up with, also expanding the logic to be more readable for myself, if it helps anyone who lands on this in the future.

# SublimeText auto-open project in current directory if it exists and argument is . or blank.
function project_aware_subl {
  project_file=$(ls *.sublime-project 2>/dev/null | head -n 1)
  if [[ ( ! -z "$project_file" ) && ( "${*}" == "." || -z "${*}") ]]
  then
    command subl "$project_file"
  else
    command subl ${*}
  fi
}
alias subl="project_aware_subl"

@bcarlin
Copy link
Author

bcarlin commented Jul 12, 2019

Glad it helped you!

I never ran into this issue (also, I'm not using {ba,z}sh for several years now) and I didn't know about command.
I've updated the gist. Thank you!

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