How do I repeat the last command without using the arrow keys?

With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh):

!!

Then Enter.


Or alternatively:

!-1

Then Enter.


Or Ctrl+P, Enter


Magic space

Also, note that !! and !-1 will not auto-expand for you, until you execute them (when it might be too late).

If using bash, you can put bind Space:magic-space into ~/.bashrc, then pressing Space after the command will auto-expand them inline, allowing you to inspect them before execution. This is particularly useful for history expansion from a command run a while ago, e.g. !echo will pull the last command run starting with echo. With magic space, you get to preview the command before it's run.

That's the equivalent of doing bindkey ' ' magic-space in tcsh or zsh.


Most shells that have a command line editing feature support Emacs key bindings. (a tiny subset)

Up     Ctrl+P
Down   Ctrl+N
Left   Ctrl+B
Right  Ctrl+F
Home   Ctrl+A
End    Ctrl+E
Delete Ctrl+D

Alternatively, you could set up your shell to use vi command editing mode, by adding set -o vi to your shell startup file (e.g., ~/.bashrc).  Then, for example, you can

  • Use EsckEnter to re-execute the previous command (same as !!).  The minus key - also works as a "move up" command.
  • Use EsckkkkkkkkkkkkEnter or Esc12kEnter to re-execute the 12th previous command (same as !-12).
  • Use Esc and a motion command (i.e., k, suitably repeated), and then edit the bash command line you find there.  Remember, you will still be in vi command mode, so you will need to use a vi editing command (e.g., I, A, C, R, or one of their lowercase counterparts) to actually add to the command from history.
    So, for example, EsckisudoSpaceEnter is equivalent to sudo !!.
  • For advanced users: you can even copy (yank) text from one line and paste (put) it on another, so you can accomplish results comparable to !-2:- !$.  (Unfortunately, it does not seem to support named buffers.)

My favorite one is CTRL + P then CTRL + O

This works by default, no extra configuration needed. ^P will let you switch to the last command and ^O will let you execute current line

Note that CTRL + O can be used for as many times as you want