Add custom commands to the Mac OS X Terminal

Published on Thursday, May 10th, 2007

When I use my Mac and type away in the Terminal, I’ve felt the need to add custom commands; e.g. launch an application with that folder/file as a parameter.

The way to accomplish this is to create a hidden file in your user folder (i.e. Users/robertnyman) named .profile (you use the dot before the name to create a hidden file in Mac OS X). In that file, you just simply add your commands. Here’s an example where I’ve added three commands to launch Preview, Photoshop and Firefox respectively with the current folder/a file as the input parameter:


prev(){
    open -a /Applications/Preview.app/ "$1"
}

ps(){
    open -a /Applications/Adobe\ Photoshop\ CS/Adobe\ Photoshop\ CS.app/ "$1"
}

ff(){
    open -a /Applications/Firefox.app/ "$1"
}

One of the power features with the code above is that when the prev command is called for a folder, it will open all images in that folder and its sub-folders in the same Preview window. So, happy customizing of commands! :-)

Bonus code: case-insensitive auto-completion

When you press the Tab key in Terminal, you auto-complete the current word you’re typing (according to available commands/files). However, this is case-sensitive, which is a bit of an annoyance since many default folders in Mac OS X are named with upper-case starting letters, such as Desktop, Pictures, Movies etc. But, come to the rescue, a simple command to run in the Terminal to make it case-insensitive:

echo "set completion-ignore-case On" >> ~/.inputrc

Tip: remember to close the Terminal window and restart it before you try it.

15 comments

  • Fredrik Frodlund
    May 10th, 2007 at 12:52

    No need to restart the terminal. Just do a
    source ~/.inputrc
    to apply changes made in the file. ;)

  • Guilherme Zuhlke O’Connor
    May 10th, 2007 at 14:39

    Great hint. Loved it!

    You can do even better an put a function more or less like this

    runapp() {
    $app=`find /Applications/ -name “*.app” | grep $1`;
    shift;
    open -a “$app/” “$2″;
    }

    and you can invoke any application you know part of the name with a command like

    $ runapp photoshop .

    Just need some clash detection, case tha part of the name you typed appears more than once, like

    $ runapp adobe .

    (Sorry if my code is poor or doesn’t work rightaway, I’m at work where I only have windoze at hand and I can’t test it.)

  • Harmen Janssen
    May 10th, 2007 at 17:57

    That sounds very interesting, Robert!
    I should really take some time to get familiar with the Terminal/UNIX. It looks like it’s worth it :)

  • Robert Nyman - author
    May 10th, 2007 at 20:35

    Fredrik,

    Ah, thanks! :-)

    Guilherme,

    Well, yes, but that’s too much typing for my taste. :-)

    Harmen,

    It’s definitely worth it! :-)

  • Brian Schlining
    May 10th, 2007 at 20:36

    Just FYI, you can run open -a Firefox in a terminal; you don’t need to specify a full path or the .app extension.

  • Robert Nyman - author
    May 10th, 2007 at 20:40

    Brian,

    Yes, absolutely, that goes for most apps. But what you miss out on then is the ability to pass a file as a parameter (unless you write a longer statement).

  • Aldrik
    May 12th, 2007 at 0:39

    In linux this is done by setting up aliases and google says it hasn’t changed for OSX… Terminal Alias Shortcuts.

  • Robert Nyman - author
    May 12th, 2007 at 1:03

    Aldrik,

    As I understand it, it’s about the same approach, and that you need to add that as well to a .profile or .bash_profile file.

  • Aldrik
    May 12th, 2007 at 3:00

    Robert,
    Indeed, it’s just a bit cleaner. Plus you can add arguments to existing commands.

  • Alexandre Bonatto
    November 29th, 2007 at 19:34

    Thank you!

    I am still learning to use the amazing and powerful TERMINAL :-)

    Alexandre

  • Robert Nyman - author
    November 30th, 2007 at 8:37

    Alexandre,

    Me too, me too. :-)

  • kamasheto
    December 22nd, 2008 at 5:13

    Thanks for this. Much appreciated.

    I’m a new Mac-user and am still getting the hang of it. I’ve been using both Linux and Windows and I am really liking the combination of both in Mac.

    Say, wouldn’t you know of a way to merge directories under Mac?

  • Robert Nyman - author
    December 22nd, 2008 at 20:43

    kamasheto,

    Thanks! Off the top of my head, I don’t know how to do that.

  • Colin
    January 9th, 2009 at 1:18

    sorry to sound like a n00b, but how do you add a command? Is it just a .txt file or is there something else that you have to do. Either way great article

  • Robert Nyman - author
    January 9th, 2009 at 13:31

    Colin,

    A hidden file that doesn’t really have any type, i.e. just a name that starts with a dot (.). You can trea a hidden gile in the Terminal directly by writing touch .myfile, and then open it up with the editor you prefer.

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview

Top results