6

So I have an alias for "sublime text" http://www.sublimetext.com/docs/2/osx_command_line.html and very often I need to open a file or a folder right from the terminal, easy for a file:

subl myfile.html 

and for a folder

subl /Users/me 

But how can I use 'pwd' command to open current directory in sublime? Is there a way to pipe it in?

3 Answers 3

11
$(pwd) 

or, you can use backticks, like

`pwd` 

So:

subl `pwd` 

Either way, what happens is the command pwd gets executed, then its return text gets passed as the command-line. A good way to see what's happening, assuming you're using bash, is to issue set -x then run your command. As this example, where subl is aliased to echo shows, the result of pwd is passed on the command-line to the expanded alias (lines beginning with + indicate the actual command being executed):

$ alias subl=echo $ mkdir /tmp/abc $ cd /tmp/abc $ set -x $ subl `pwd` ++ pwd + echo /tmp/abc /tmp/abc $ 

(When you're done, you can stop printing commands by issuing set +x.)

2
  • 1
    The backticks should be avoided, they've been deprecated. stackoverflow.com/questions/4708549/… Commented Sep 17, 2013 at 22:24
  • @slm While backticks shouldn't be used in scripts, they're ok on the command line as long as you're typing simple stuff (no `` or nested command substitution inside). They have the advantage of being easier to type and maintainability is only an issue in scripts. Commented Sep 17, 2013 at 23:11
3

You could use the environmental variable PWD or the .

sub1 $PWD

sub1 .

1

To interpolate the output of a command on the command line of another command, use command substitution.

subl "$(pwd)/somefile.txt" 

Note that you need to put (…) between double quotes. Otherwise, bad things will happen sometimes (when the output of the command contains whitespace or \[*?).

In the special case of pwd, there is a shell variable that contains the same text: instead of $(pwd), you can write $PWD. For the same reasons, this needs to be between double quotes.

If you want subl to invoke the subl program with an absolute path as argument, this has to be done in a function, it can't be done in an alias. Remember that the path may already be an absolute path.

subl () { case "$1" in /*) :;; # already an absolute path, nothing to do *) set "$PWD/$1";; # make an absolute path esac command subl "$1" # invoke the external program subl, even though a function exists } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.