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

I know I can use Up to iterate through previous commands. Running the last command simply involves Up + Enter. However, I was thinking of buying the Happy Hacking Keyboard as I spend a lot of time in vim.

This keyboard has no arrow keys, and the only way I know how to get this kind of behaviour is by pressing Ctrl + R and beginning to repeat my previous command.

Is there an easy way to emulate Up + Enter in an UNIX terminal without the arrow keys?

Asked By: quant


Sure! Since you’re used to vi keybindings, why not configure your shell to respond to them? For bash, put this in your ~/.inputrc:

set editing-mode vi

Running instances of bash will not re-read the file, so log out and back in.

zsh will even detect this for you: if none of your startup scripts force the editing mode one way or the other using bindkey and if your $EDITOR environment variable is detected to contain vi, then it will automatically enable vi keybindings. If you need to force it, put this in your ~/.zshrc:

bindkey -v

Thereafter, use ESC as usual to enter command line and k and j to move up and down.

ALSO: The default shell bindings in most shells are the emacs bindings, so actually Crtl-P and Ctrl-N should already work without you having to change anything.

Answered By: Celada

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


Then Enter.

Or alternatively:


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.

Answered By: Sparhawk

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

Answered By: daisy

With any POSIX shell implementing the User Portability option (includes ksh, zsh, bash, yash), you can also use the fc command:

fc -e : -1

See an example:

$ echo "hello"

$ fc -e : -1
echo "hello"

More info in Execute a range of commands from history‘s answer by Jonathan Leffler.

Answered By: fedorqui

With csh or any shell implementing csh-like history substitution (tcsh, bash, zsh), you can also use the !<beginning of command> to call the last command beginning with <beginning of command>.

for example if you ran

$ tail file.txt
$ less otherfile.txt
$ !ta

!ta would execute tail file.txt

Answered By: Dan

I find I need to redo commands as super user often, so

sudo !!

redoes the previous command as if I had remembered to type sudo in the first place.

Answered By: Jared Chmielecki

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.)

For all you Mac iterm2 users:

You can bind +R to 0x0C 0x10 0x0d. This will clear the terminal and run the last command.

iterm 2 key preferences

Answered By: Brian Wigginton

When I am using a good xterm emulator, e.g. putty, my favorite way to proceed is to select the command including line feed, copy it to clipboard and then use right click on the mouse to paste it. This repeats the command in a single click.
This also works fine for a sequence of commands separated by semi-colons.

Answered By: Bernard Perrin

you can use !n to execute nth command in terminal.
where ‘n’ is line no of history command.

Answered By: Ravi Sevta

tl;dr !! just appends the last command to your current command. <backtick>!!<backtick> executes the last command and appends the output to your current command.

There are actually 2 variants:

  1. The last command itself
  2. The result of the last command

Let me show you 2 examples:

Example 1

$ mkdir /test
mkdir: /test: Permission denied

$ sudo !!
sudo mkdir /test

As you see above, I’m executing a command and essentially just appending the last command.

Another variation

Example 2

$ find ~/Documents "test.txt"

$ vi `!!`
# Opens test.txt. But if you do
$ vi !!
vi find ~/Documents -name "test.txt"
VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 29 2018 06:55:58)
Unknown option argument: "-name"
More info with: "vim -h"
Answered By: Pradyumna Shembekar

The ksh shell has a default alias r that repeats the most recent command. It is bound to fc -s:

alias r='fc -s'

Interestingly, the bash documentation for the fc builtin (help fc, and also in the manual itself) even mentions this, but it’s not a default alias in that shell:

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.

In bash, this would also work even if you have turned history expansions off with set +H.

In the zsh shell, there is a r builtin that is documented to be the same as fc -e -.

The fc utility is a POSIX standard utility.

Answered By: Kusalananda
  • Type History and note the number in front of the command that you to
    execute and use !number

  • You can also use !-1 (1 being the last command you execute, replace
    it with the count number that you get when you do it from below
    keeping the last executed as 1 )

  • !-2 (second last and so on)
Answered By: Lovish

There are several layers in which you can ask for the last command.

Line editor

The line editor (where you type your commands) is provided directly by ksh, by the library readline in bash, and by the library zle in zsh. Other shells, like dash may not have an editing library to understand this keys (unless compiled with the --with-libedit option enabled) .

This work in ksh, bash, zsh:

CtrlP # Emacs mode (previous)
CtrlN # Emacs mode (next)
ESCk # vi mode ESC to go to command mode and k for up
ESCj # vi mode ESC to go to command mode and j for down


The history system provided by many shells allow the expansion of some shortcuts:

!!    # bash and zsh, may be provided by `alias !!='fc -e -'` in ksh.
!-1   # bash and zsh only


There are some commands which are able to retrieve history commands.

Like the quite verbose command (only to print):

history 2 | head -n-1

or the older and therefore usually more portable fc command (directly re-executes):

fc -e - -1

Or maybe the simpler fc -s -1, but zsh doesn’t accept such option for fc.

If you want to only print the command, use: fc -nl -1 -1.

To edit, set FCEDIT to a text editor (ex, ed, emacs, vi, etc.) and use fc directly: fc -1.

Answered By: user232326

The happy hacking keyboard does have arrow keys, just not dedicated arrow keys. They can be used with:

  • Fn and [ for Up
  • Fn and ; for Left
  • Fn and ' for Right, and
  • Fn and / for Down.

This is indicated with the front-printed legends on these respective keys as seen on this blog.

Answered By: Swirle13

One of my favorites shortcuts is:
hold Ctrl and P+J
It’s like up and enter but in a smoothie way

Here you can find more shortcuts

Answered By: Gustavo Adolfo Mejía