Terminal: prefix previous 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.


Here are a few common keyboard combinations that many Mac and Linux programs use (not sure which are followed by most Windows apps, though):

  • CTRL-A - Bring the cursor to the beginning of the current line
  • CTRL-E - Bring the cursor to the end of the current line
  • CTRL-Left - Move the cursor back one word
  • CTRL-Right - Move the cursor forward one word

Use

sudo !!

The !! will be replaced by the complete previous command.