Sublime Forum

Command line opening should use project

#1

I tend to navigate to a project folder on the command line and then open the current folder with

subl .

If there is a project file in the folder, it is ignored. It would be nice if ST2 would use the project file and open the project without having to use the --project switch.

http://sublimetext.userecho.com/topic/105061-/

0 Likes

Opening files from command line confustion
#2

+1 for this request

I’ve been using this shell scripts in meantime:

#!/bin/sh

if test $1; then
  # passing args -> open given file
  sublime $@
else
	# no arguments
	PRJ=`find . -iname *.sublime-project`
	if test $PRJ; then
		# project exists in current folder, open it
		sublime --project $PRJ
	else
		# no project, open current folder
		sublime .
	fi
fi
0 Likes

#3

facepalm

Writing a wrapper script didn’t even occur to me.

sigh

0 Likes

#4

Using @vojtajina’s script as a starting point, I tweaked it until I came up with something that works well for me. I thought I’d share it just in case someone else might find it useful.

#! /bin/bash

if  -z $@ ]; then
	# no arguments, let's just open sublime
	sublime
elif  $1 != '.' ]; then
	# passing args and $1 is not a .
	sublime $@
else
	# look for a project file ... redirect stderr to /dev/null so
	# we won't see the error message if the file does not exist.
	# changed this from `find . -iname *.sublime-project` because
	# that does a recursive find which is problematic when wanting
	# to open something like a home directory (recursively looks
	# for all sublime projects ... not good). this also assumes
	# there will at most one project file in a directory. not sure
	# why someone might have more, but if they do, they should
	# specify on the command line which one they want to open

	PROJECT=`ls *.sublime-project 2>/dev/null`

	# added quotes around $PROJECT to account for spaces and such
	# in the file name
	if  -e "$PROJECT" ]; then
		# project exists in current folder, open it
		sublime . --project "$PROJECT"
	else
		# no project, open current folder
		sublime .
	fi
fi

Two things:

  1. This script works on Mac OS X 10.7.4. I make not guarantees it will work unmodified anywhere else.
  2. This script depends on a symlink in your path as such:
sublime -> /Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl
0 Likes