Is there a way to add a keyboard shortcut for a terminal command in OSX. Basically most of the times i open the terminal app in MAC in order to ssh into a certain server foo. What I want to do is add a keyboard shortcut (say ^k) so that on a terminal when I do that, it runs "ssh foo" in the terminal.
Thanks
PS: I think if there is something for the xterm in linux then it should work for the terminal too. So this might not be an OSX specific question.
PS2: I want the shortcut to do carriage return with the "ssh foo". If its just "ssh foo", then I can write an alias in .bashrc. My goal is to minimize the number of keystrokes I've to do at the end of the day.
Best Answer
You are right, this is not an OS X specific question. The answer is found in
bash
, the standard shell on OS X (also included in most if not all Linux distros).bash
provides a built-in command calledbind
that can be used to bind a key combination (like AltK) to a command (likessh foo
).Open Terminal and type:
This will bind the key combination
\ek
(in Terminal both AltK and EscK) to the commandssh foo
. The\n
adds a return.Now press AltK and
ssh foo
will be executed (modifyssh foo
to your needs).If you want this key combination to be automatically loaded when starting a Terminal create a file called
.inputrc
in your home folder:and open it:
You will see TextEdit open. Type:
and save it (or use your favorite editor). That's it!
There's much more to it than what I've explained here. I recommend that you have a look to this question: https://stackoverflow.com/questions/4200800/in-bash-how-do-i-bind-a-function-key-to-a-command and check (the rather dry) section READLINE in
man bash
. Notice that the escape sequence\M-
, which in other operating systems is mapped to Alt, doesn't work in Terminal. You need to use\e
to bind Alt.Since escape sequences are terminal emulation dependent I'd like to share the setup I used to test the above command: