I can't remember the trick where I could get the last command without running it:

let's say I want to be be able to access the command !1255 when pressing the up arrow key and modify the command. So what's the trick to call the command, make it be shown up in the command line but not executed and afterwards accessible via the arrow key up?

I tried with putting an echo, but then I have an echo before the command, I don't remember how to do it correctly.

  • 4
    if i understand your request properly, perhaps just try a suffix of ":p" as in "!1255:p" Oct 9, 2015 at 20:32
  • @Theophrastus yes, exactly, I had forgotten this :p part and couldn't seem to get the right keywords in google to find it... Thanks guys.
    – Arturas M
    Oct 9, 2015 at 20:58

5 Answers 5


Will do this

! is history recall
1255 is the line number
:p prints but does not execute

Then you can use up-arrow to get ther previous (unexecuted) command back and you can change it as you need.

I often combine this with hg ("History Grep") - my favorite alias.

$ alias hg  # Maybe use hgr instead if you are a Mercurial CLI user.
alias hg='history | tail -200 | grep -i'

This searches for text on a recent history line, regardless of case and is used this way:

When I want to search for recent vi commands to edit a certain file and then I want to re-use one of them to edit the same file but with a different file extension.

$ hg variables
 6153  vi Variables/user-extensions.js 
 6176  vi Variables/user-extensions.js 
 6178  vi Variables/user-extensions.js 
 6190  vi Variables/user-extensions.js 
 6230  hg variables
$ # Notice the difference in case with V and v is ignored
$ !6190:p
vi Variables/user-extensions.js 
$ ["up-arrow"]
$ vi Variables/user-extensions.[now change .js to .html]

I also define hga ("History Grep All") to search my entire history:

$ alias hga
alias hga='history | grep -i'

but I don't use it much because my history is (intentionally) very large and I get too much output that later affects scrolling back thru pages in my terminal.


Hitting Ctrl + R ( Which is the emacs' reverse-incremental-search key binding as @gnp motioned in the comments ) in the terminal allows you to search in the histrory for a specific command. The command will be shown and can be edited before executing. You will see something like this :

enter image description here


  • 1
    Thanks! Not really the direct answer to my question, but I didn't know this and I find it really useful. Thank you very much!
    – Arturas M
    Oct 9, 2015 at 20:59
  • 1
    Also noteworthy,Ctrl+R is emacs' reverse-incremental-search. Many emacs keybindings work accross a lot of GNU tools: Ctrl+A, Ctrl+E, Ctrl+K, Ctrl+Y and others.
    – GnP
    Oct 16, 2015 at 22:42
  • @gnp Thanks for the information, I didn't know that. Oct 17, 2015 at 9:26
  • Took me a long time to comfortable with ctrl-r after learning two keys things. 1. ignore the output as you are typing. 2. Be sure you know enough to type enough uniquely for the command you want Feb 25, 2016 at 0:29

In bash Ctl+alt+e does shell expansion, thus typing !1255 and then pressing this combination will substitute current line with the contents of history entry 1255

  • didn't do that for me on Ubuntu Oct 9, 2015 at 20:51
  • @MichaelDurrant, I just tried in on 14.04 (kubuntu) and it works for me. It is documented in man bash as shell-expand-line. Maybe there is a shortcut set for gnome-terminal.
    – adonis
    Oct 9, 2015 at 20:55
  • @adonis I never knew this one, thank you very much, indeed it does what I was requesting too. And Indeed I can find more uses from this function. Awesome! Thanks!
    – Arturas M
    Oct 9, 2015 at 21:02

the fc command does what you want

fc -l <number> will list command history around that number

fc <number> will open that command line entry in vim (or whatever your default editor is I suppose), so you can edit it, and upon :wq it will be available to you with the up arrow.


You want to use the fc command while specifying list instead of execute (-l)

fc -ln -1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .