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.
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: 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:
enter image description here
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
-
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– Injun Joe2011年07月05日 06:57:48 +00:00Commented Jul 5, 2011 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– Injun Joe2011年07月05日 07:00:43 +00:00Commented Jul 5, 2011 at 7:00
-
this is strange, because my terminal actually starts midnight commander (mc) automatically when launched from Dock, for instance.etranger– etranger2011年07月05日 08:16:15 +00:00Commented Jul 5, 2011 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– I J2011年07月05日 16:54:09 +00:00Commented Jul 5, 2011 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.
-
The original asker wants to be able to do this in Terminal, not by using a third party application...nhinkle– nhinkle2011年07月05日 19:43:58 +00:00Commented Jul 5, 2011 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.
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.
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.
-
TextExpander works great for terminal, but not xterm, in case anyone wanted to know.ggkmath– ggkmath2013年07月08日 16:03:06 +00:00Commented Jul 8, 2013 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
-
TextExpander doesn't work for xterm, in case anyone wanted to know.ggkmath– ggkmath2013年07月08日 16:02:13 +00:00Commented Jul 8, 2013 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.
You must log in to answer this question.
Explore related questions
See similar questions with these tags.