4

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?

6
$(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.)

| improve this answer | |
  • 1
    The backticks should be avoided, they've been deprecated. stackoverflow.com/questions/4708549/… – slm Sep 17 '13 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. – Gilles 'SO- stop being evil' Sep 17 '13 at 23:11
3

You could use the environmental variable PWD or the .

sub1 $PWD

sub1 .

| improve this answer | |
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
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.