Bash also has a forward history search, which is mapped to CtrlS.
Often, this shortcut will be masked by the stop
flow control key binding for the terminal (check with stty -a
). This will make your terminal stop outputting anything—not quite what you want.
To get forward history search working, you have two options:
Disable the flow control altogether: stty -ixon
Bind flow control to something else, e.g. to CtrlX with stty stop ^X
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
This all depends on the program's implementation, or more specifically on how it parses options. OS X does not use the same implementation of
ls
— it uses the BSD versions thereof, while Ubuntu (and all Linuxes) use the GNU coreutils version.Its behavior is not something you can change through a setting, but you can install the GNU coreutils through Homebrew:
That way, you could do the same thing on OS X. Note that in the default coreutils installation, all GNU utilities will be prefixed with
g
. So, you'd usegls
instead ofls
. You can override that behavior by adding them to your path. To do that, add this to your~/.bash_profile
:That all being said—and because it all depends on the program implementation—you cannot expect this to work everywhere. Some programs are strict about where options appear, and others are not. It's nothing the OS itself enforces.
What you can also do to quickly add another option is to hit ↑, CtrlA, Option→. This will take you to the word after the command name itself, where you can add the option your forgot.
If it doesn't work and prints some weird characters when you press Option→, make sure that these keyboard settings are enabled in Terminal.app's preferences under Settings » Keyboard: