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:
Simply use UP and CTRL-A.
Both !sudo
and sudo !!
function easy.
With a difference:
sudo !!
will execute the last command taken from the history with sudo
in the beginning.
!sudo
will execute the last command in the history that started with sudo
.
Usually CTRL-A brings you at the beginning of the line in a linux terminal and CTRL-E to the end.
If you want you can continue to use your strategy. You have only to discover the shortcut of your system. You can find a list of shortcuts in graphic and text environment e.g. here.
Only a warning: sudo !!
and !sudo
function really fast and you have no visual control of the line you are running as superuser. This can result harmful sometimes. Specially if you incur in a missprint.
Let's we do a dramatic example:
cd $HOME/Directory_To_Delete # You go to delete what is inside a directory
rm -rf . ; cd .. # You do it and go up of one
apt-get update # Note the space in the beginning of the line
sudo !! # Gone!! Now you realize you never really wanted
Too late you just erased your home directory and all the subfolders !!.
When you write a command with a space as a first character it's possible that it will not finish in the history. This depend from the value of the variable $HISTCONTROL. If the list of its values includes ignorespace (or ignoreboth) the lines which begin with a space character are not saved in the history list. So with sudo !! you will execute the last command before.
You can imagine similar situation with !sudo
, it will execute the last command starting with sudo
recorded in the history, but if the last that you remember was starting with a space... now you know you will use another.
Last note: less is dramatic your error more it can be devious because it can pass without that you notice... and you will risk to be called to pay all the interests of a bill that you cannot remember.
Update (Bash is great):
with recent versions of Bash it is possible to fix this dangerous behaviour with the following command that I suggest to put in ~/.bashrc
file:
shopt -s histverify
From man bash
you can read
If the histverify shell option is enabled and readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the readline editing buffer for further modification.
So you can read, and modify when needed, the command that you will execute.
Since it is not the default setting in many distribution we still have to remember it when we use other accounts.
Best Answer
Assuming you're in bash, use Ctrl + U.