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>'?

3

3 Answers 3

5

You can do:

sudo !!

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

3
  • Perfect, this is exactly what I was looking for you. Thanks.
    – tdm
    Jul 28, 2014 at 16:22
  • Waiting too. Another 7 min. :)
    – tdm
    Jul 28, 2014 at 16:25
  • 1
    This only works in simple cases though, not when the command is a complex one involving redirections, pipes, etc. Jul 28, 2014 at 22:31
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
1
  • 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, 2014 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .