I am on Mac OS X and I usually open PDF files from the terminal (using bash)

open myfile.pdf

Now I'd be able to type only


and have that opened with the command above. Is there any chance to configure bash to run that command for me? I'd be happy if that works only for PDF files, but of course a more general solution is welcome.

Actually it is not purely for saving a few keystrokes. If a solution takes me two hours, I doubt that I will save so much time by having two key presses less the next years. I'd like to learn if this is possible and how.

  • Would it be a bad idea to make some kind of shell-wrapper script for bash? I just scribbled this script down.
    – user13742
    Jun 13 '12 at 21:25
  • @hesse what would you do with the script? How would you call it? I don't see how it solves my problem. Can you elaborate?
    – topskip
    Jun 13 '12 at 21:28

There is a way to add this function to bash.

When you try to "run" file bash will emit command not found error :

$ 1.pdf
bash: 1.pdf: command not found

After that it will also emit ERR "signal". Using buildin trap you can catch this type of signal and define function that will handle error.

On that page you can find script that will handle that error (read also explanation).

Script is well commented so it shouldn't be a problem to modify it to your needs.

After catching ERR it checks error code of last command. If it is error 2, 126 or 127 it will continue. In next step it will check last command and split it into command part and arguments.

First if..fi "handle possible errors involving forgetting a leading slash", so you can remove it.

On the begging of next if statement there is a check if your command is a directory, so it also could be removed. More interesting is second part of that if - script checks filetype and print some suggestions how to open that type of file. Instead of that you can just put something like open $cmd or APPLICATION_TO_OPEN_PDF $cmd (of course you need to add check if file is PDF).

Security note: If you want to use that function you should rather use APPLICATION_TO_OPEN_PDF $cmd than open $cmd. You should also set action for specific filetypes and not use open or exec on all type of files to avoid running some malicious script or something like that.

There is also another way to do this, but I think it could cause some problems, because it is more intrusive.

You could redefine function command_not_found_handle() and put there that checks and opens. That will cause that there will be no error messages, but I'm not sure what it will do with error codes and messages in cases that you don't want to handle.

  • Interesting solution.. Would this require the command to be run, followed by the command not found error message each time?
    – user13742
    Jun 13 '12 at 21:46
  • I'm not sure if it could be done, but I added second solution to my answer. Personally I would go with first solution and error message displayed in terminal before opening program.
    – pbm
    Jun 13 '12 at 22:09
  • Actually the second seems more inspiring to me :) But I will try it out once I am awake (again)
    – topskip
    Jun 13 '12 at 22:14
  • You may want to check command-not-found package from Ubuntu that makes heavy use of this function.
    – pbm
    Jun 13 '12 at 22:16
  • commad_not_fund_handle works fine on my ubuntu machine, but not on my mac (both bash 4). I'll investigate further. But it seems as if it is what I was looking for. Otherwise I'll use your other solution. Thanks!
    – topskip
    Jun 14 '12 at 8:42

In zsh, define a suffix alias in your .zshrc:

alias -s pdf=open

Zsh generally has better interactive features and better programming features than bash. It has been shipped with Mac OS X since at least 10.4.

  • Well, one day I should really think about taking some time off and try it out. Thans for pointing it out!
    – topskip
    Jun 14 '12 at 6:41

I suppose you could create an alias for open name o and then type

 o myfile.pdf

though I dont' know if that saves you so much effort from typing open :-) The filename will have to be specified in any case.

So for the alias:

alias o="open"

should go into your ~/.bashrc file

  • That is what I already have (and use), but I'd really like to type the filename only.
    – topskip
    Jun 13 '12 at 21:07
  • @Patrick Perhaps someone knows a trick to have the command shell filter the input and recognize that someone's given the name of a file and act on it rather than a valid command, but it sounds tricky (I'll be curious and watching to see if anyone comes up with anything) and would add some overhead. I think the difference between file.pdf and o file.pdf given how easy it is to implement could be ok, but that's a decision only you can make of course.
    – Levon
    Jun 13 '12 at 21:09
  • My question is mainly motivated by learning if/how this is possible, not to save two keystrokes.
    – topskip
    Jun 13 '12 at 21:15
  • @Patrick I understand .. I saw your update above. I'll be curious to see what if any solutions emerge for this too.
    – Levon
    Jun 13 '12 at 21:17

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.