An alternative to the up arrow + Enter to run the previous command?

Sometimes I have to run a command many times in a row, for example, to see if the service is running, and it becomes tedious to move my hands away from the usual input position, to repeatedly press the Up and Enter keys. Is there a way to run the previous command without the Up and Enter keys, perhaps using a complex shell script?

I tried the following, but this is unsatisfactory because it cannot perform aliases and it is a bit slower.

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 will have an alias for this script, so I can type something like "prev" on the command line and press Enter to run the previous command again.

+5
source share
7 answers

In bash:

$ help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat = rep] [command]
    Display or execute commands from the history list.

    fc is used to list or edit and re-execute commands from the history list.
    FIRST and LAST can be numbers specifying the range, or FIRST can be a
    string, which means the most recent command beginning with that
    string

    Options:
      -e ENAME select which editor to use. Default is FCEDIT, then EDITOR,
            then vi
      -l list lines instead of editing
      -n omit line numbers when listing
      -r reverse the order of the lines (newest listed first)

    With the `fc -s [pat = rep ...] [command] 'format, COMMAND is
    re-executed after the substitution OLD = NEW is performed.

    A useful alias to use with this is r='fc -s', so that typing `r cc'
    runs the last command beginning with `cc' and typing `r' re-executes
    the last command.

    Exit Status:
    Returns success or status of executed command; non-zero if an error occurs.

r; .

+3

!!

.

sudo !!

.

+7

, , watch ? watch stdout, .

watch command

+4

bash ( cntl R) - ​​ .

. bash , .

+3

emacs vi?

set -o vi 
set -o emacs

emacs vi keybindings. emacs vi bash. , . , vi , esc, . emacs ( ) ctrl + p, ctrl + j .

!! - .

+2

, , , F3 , , .

My keyboard makes it easier to access function keys, but I no longer work on the command line on UNIX, so I couldn’t say for sure if this is possible.

+1
source

All Articles