Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Up Arrow + Enter to run previous command?

Tags:

linux

shell

unix

Sometimes I have to run a command many times in succession, for example to see if a service has started, and it becomes tedious to move my hands away from my normal typing position to press the Up Arrow and Enter keys repeatedly. Is there a way to run the previous command without the Up Arrow and Enter keys, perhaps with an elaborate shell script?

I've tried the following, but it is unsatisfactory because it cannot execute aliases, and it is a little slow.

history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt

Ideally I'd have an alias to this script so I can type in something like "prev" in the command line and hit Enter to run the previous command again.

like image 516
Thomas Avatar asked Sep 17 '10 22:09

Thomas


2 Answers

In bash, you can press ctrlp to go to the previous command -- that's a lot better than having to move to the arrow keys.

See also: https://github.com/fliptheweb/bash-shortcuts-cheat-sheet/

like image 54
Mark Rushakoff Avatar answered Sep 27 '22 15:09

Mark Rushakoff


Use

!!

to run your previous command.

sudo !!

also works , for the record.

like image 40
Greg Avatar answered Sep 27 '22 15:09

Greg