1

Is there an easy way to re-apply a previous command to a new command line entry?

Say I typed in chmod u+r,g+x file.txt but forgot the sudo. Could I simply type sudo <some easy symbol>'?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
tdm
  • 113

3 Answers3

5

You can do:

sudo !!

Another good one is alt ., to insert the last parameter of the previous command

Halfgaar
  • 453
  • 3
  • 13
4

Press up arrow, press ctrl+a, write sudo, press enter.

1

There are some basic bash shortcuts you should know...

Ctrl + A    Go to the beginning of the line you are currently typing on
Ctrl + E    Go to the end of the line you are currently typing on
Ctrl + L    Clears the Screen.
Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H    Same as backspace
Ctrl + R    Let’s you search through previously used commands
Ctrl + D    Exit the current shell
Ctrl + W    Delete the word before the cursor
Ctrl + K    Clear the line after the cursor
Ctrl + T    Swap the last two characters before the cursor
Esc + T     Swap the last two words before the cursor
Alt + F     Move cursor forward one word on the current line
Alt + B     Move cursor backward one word on the current line
Tab         Auto-complete files and folder names

In your specific case, I also alias h to history|grep.

Such that:

# mco service sendmail status -F operatingsystemmajrelease=6

And I need to prepend something to that...

# h mco

Which shows...

  114  07-28-2014 09:33:25  mco package sendmail install -F operatingsystemmajrelease=6
  115  07-28-2014 09:33:25  mco service sendmail status -F operatingsystemmajrelease=6
  116  07-28-2014 09:33:25  mco package sendmail-cf install -F operatingsystemmajrelease=6

And I want line #116... So I type:

# !115

But if I need something in front of it (e.g. sudo), I'd do...

# sudo !115
ewwhite
  • 1,521
  • As I mentioned in my post, alt . is an important one too. And might as well add alt / too, to circumvent smart tab completion and it the old fashioned way. Or alt *, to expand a *. – Halfgaar Jul 28 '14 at 19:17