157

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?

Sparhawk
  • 19,941
quant
  • 4,161

16 Answers16

217

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.

Sparhawk
  • 19,941
  • 1
    @Arman Glad to help. There's a whole lot of history expansion commands; the link lists some of them. I've edited in some information about magic space that might help with usage of some of these. – Sparhawk Jul 31 '14 at 04:01
  • 5
    magic-space that is one sweet option! – fduff Jul 31 '14 at 07:10
  • Or if you're using zsh, "! ! " – phemmer Jul 31 '14 at 13:11
  • 3
    There's also the histverify option to shopt which will cause readline to perform the history expansion but not execute the command on the first press of the enter key. Which will let you evaluate the result and decide if that's what you want to run. – Etan Reisner Jul 31 '14 at 13:33
  • 2
    Never understood why !! is used so much more than CTRL-P. CTRL-P lets you see the command before executing it -- and you can even make modifications to it. I always feel like I'm rolling the dice with !!. – abonet Aug 05 '14 at 21:30
  • @abonet Most of the time I use the up arrow and Ctrl+r to navigate history. However, !! is easy for me to remember, since it fits within the canon as per the link (i.e. !-2, !ls, !$, !-2:2, etc.) Also, regarding previewing the command, did you read the second half of my answer? – Sparhawk Aug 05 '14 at 23:32
  • 2
    @abonet Another use that I just found for !! is using its expansion in-place. For example sudo !!. – Sparhawk Aug 07 '14 at 02:40
  • How can you escape it? (i.e., make it so that typing !! does not select the last command?) – David Rhoden Dec 12 '15 at 20:19
  • @DavidRhoden \!\! or \!!; or '!!' or '!'!; or "!"!. You can test with (e.g.) echo \!!. – Sparhawk Dec 12 '15 at 21:06
  • @abonet's suggestion of using ctrl-p is the best answer here for live cli use - it avoids the actual history expansion feature, which is necessary for some, and can be used in place (@Sparhawk's comment suggests this isn't the case, but 'sudo ctrl-p' will produce the same result as 'sudo !!', except subsequent presses of ctrl-p will loop through the command history, making it even more effective). – Shawn Erquhart Dec 17 '15 at 14:52
  • @ShawnErquhart I'm not sure what you mean by "the actual history expansion feature". However, I still can't make ctrl+p work in-place. e.g. echo foo<enter>sudo <ctrl>+<p> will produce echo foo at the prompt. – Sparhawk Dec 17 '15 at 21:09
  • @Sparhawk history expansion is a feature that can be turned on/off (in the cli via set +H, for example). There are good reasons to do this, as some consider history expansion potentially dangerous. However, even with expansion off, you can still use the emacs bindings, such as ctrl+p. Not sure why it isn't working in place for you - I use bash via msysgit on Win10, works in place for me. – Shawn Erquhart Dec 18 '15 at 01:17
  • Ack. I could have sworn it was working in place, but it looks like I was mistaken on that bit, it does replace the entire line. – Shawn Erquhart Dec 18 '15 at 01:54
  • @ShawnErquhart Ah, okay, your comments on history expansion make sense. One wouldn't want unplanned things expanding! magic-space will mitigate it somewhat, but not in all cases. – Sparhawk Dec 18 '15 at 03:48
75

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

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

daisy
  • 54,555
17

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.

Celada
  • 44,132
12

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"
hello

$ fc -e : -1
echo "hello"
hello

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

fedorqui
  • 7,861
  • 7
  • 36
  • 74
7

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.

polym
  • 10,852
6

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

HalosGhost
  • 4,790
5

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.

Kusalananda
  • 333,661
3

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

Dan
  • 131
1

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

Ravi Sevta
  • 427
  • 1
  • 5
  • 10
1

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
Password:

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

Another variation

Example 2

$ find ~/Documents "test.txt"
/Users/<user>/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"
1

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:

Ctrl-P # Emacs mode (previous)
Ctrl-N # Emacs mode (next)
ESC-k # vi mode ESC to go to command mode and k for up
ESC-j # vi mode ESC to go to command mode and j for down

history

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

commands

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.

1

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.

AdminBee
  • 22,803
Swirle13
  • 11
  • 2
0

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.

0
  • 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)
Lovish
  • 11
0

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 https://cheatography.com/gamejia90/cheat-sheets/basic-bash/