Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Jul 5 '11 at 9:22

This question came from our site for professional and enthusiast programmers.

    
I added an answer (superuser.com/questions/306414/…) some time ago that exactly solves the issue you describe. Did you have a chance to try it? –  jaume Jan 31 '13 at 8:47

6 Answers 6

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 called bind that can be used to bind a key combination (like AltK) to a command (like ssh foo).

Open Terminal and type:

bind '"\ek":"ssh foo\n"'

This will bind the key combination \ek (in Terminal both AltK and EscK) to the command ssh foo. The \n adds a return.

Now press AltK and ssh foo will be executed (modify ssh 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:

cd ~
touch .inputrc

and open it:

open -e ~/.inputrc

You will see TextEdit open. Type:

"\ek":"ssh foo\n"

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: http://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:

enter image description here

share|improve this answer

These solutions are not perfect, but could help. I only have Russian interface, so you'd have to figure specific option names for yourself.

Solution 1.

Terminal - Settings - [Second tab] - Shell - Command to run on launch

Solution 2.

Terminal - Settings - [Second tab] - Keyboard, it will allow you to use special keys to run shell commands (not ctrl-k, though)

enter image description here

enter image description here

share|improve this answer
    
Basically i want it to do "ssh foo" and a carriage return at the keyboard shortcut. Otherwise i could just put up an "alias" in .bashrc and use it. I got excited about the second solution but unfortunately terminal does not save carriage return (in the keyboard settings entry) after writing "ssh foo". –  Injun Joe Jul 5 '11 at 6:57
    
Oh and about the first solution. I used to use it earlier but clicking on shell and then new window was more cumbersome than just starting up a new shell and finding the command from my command history (through ctrl-r). Unless there is a shortcut to start a particular type of terminal, I find it useless. –  Injun Joe Jul 5 '11 at 7:00
    
this is strange, because my terminal actually starts midnight commander (mc) automatically when launched from Dock, for instance. –  etranger Jul 5 '11 at 8:16
    
Oh yes, the first solution would work. The problem is that if I do not want to do click "shell -> new window", I would need to save it in my default shell. But I do not want to start "ssh foo" and exit everytime when I do not want to do it. –  I J Jul 5 '11 at 16:54

Use iTerm2. It has the ability to do exactly what you want: define a profile which runs a certain command when starting the terminal, and assign a shortcut key to that profile - so that pressing the key combination opens a new terminal tab ssh-ed into your server.

share|improve this answer
    
The original asker wants to be able to do this in Terminal, not by using a third party application... –  nhinkle Jul 5 '11 at 19:43

I don't believe there is a way to create a keyboard shortcut like that, however there are a couple of options.

  1. TextExpander allows you to type a small set of letters and have it 'expand' to the whole thing. This could work, not sure if it is supported in Terminal, but it could work.

  2. Since most of the time you run this command in Terminal first, you can go to Terminal > Preferences > Startup and set your 'Shell Opens With' command to be what you want. Then, the few times you don't want to, simply exit out of ssh...a lot easier then typing the command.

share|improve this answer
    
TextExpander works great for terminal, but not xterm, in case anyone wanted to know. –  ggkmath Jul 8 '13 at 16:03

I'm using TextExpander for exact this purpose: Screenshot TextExpander

TextExpander macro:

ssh -l user -p 99999 void.example.com%key:return%
echo "some command"%key:return%

Highly recommended. Unfortunately it's not free, and there other ways for sure, but it works well.

Edit: This ssh connection uses a keyfile, so theres no password required.

Edit2: This looks like another solution to your problem: Terminitor

share|improve this answer
    
TextExpander doesn't work for xterm, in case anyone wanted to know. –  ggkmath Jul 8 '13 at 16:02

You can use Alfred App for using global hotkeys for various things in OS X.

For example: I have defined a global shortcut for opening the Terminal app and ssh'ing to a certain server. That shortcut will even work when the terminal application isn't running yet.

Another advantage of that solution over a simple macro inside terminal is that it automatically ssh's to your remote host also for new Tabs you open.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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