Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
You should replace the terminal tag with the shell that you use. –  Cristian Ciupitu Jul 31 at 7:40
1  
@illuminÉ no mouse... –  quant Aug 2 at 0:15

8 Answers 8

up vote 27 down vote accepted

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.

share|improve this answer
1  
Mind blown. Thank you. –  quant Jul 31 at 3:56
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 at 4:01
1  
magic-space that is one sweet option! –  fduff Jul 31 at 7:10
1  
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 at 13:33
1  
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 5 at 21:30

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.

share|improve this answer
    
This is a good answer too, thanks :) –  quant Jul 31 at 3:56

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

share|improve this answer
    
For me, Ctrl+O executed a command that I haven't run for months, since it was aliased to something else! Checking my history, it was executed 24577 commands ago… –  Sparhawk Jul 31 at 7:14
    
Ah, for some reason, it looks like ranger has rebound Ctrl+o to ranger-cd <enter>. –  Sparhawk Aug 1 at 16:30

Most shells that have a command line edition 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 edition mode, by adding set -o vi to your shell startup file (e.g. ~/.bashrc.

share|improve this answer
    
I knew about the emacs relation with the terminal... but the vi trick was great!!! –  Dox Aug 6 at 20:06

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
I really like this, also looks like a good response to "authorization denied". "I meant sudo ffs!" –  user1261166 Aug 1 at 11:20

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.