Sign up ×
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 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.

share|improve this question
3  
if i understand your request properly, perhaps just try a suffix of ":p" as in "!1255:p" – Theophrastus yesterday
    
@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 yesterday

5 Answers 5

up vote 13 down vote accepted
!1255:p

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.

share|improve this answer
1  
Note that hg is also Mercurial's CLI. – usandfriends 10 hours ago
    
Good note. Added to answer. – Michael Durrant 7 hours ago

Hitting Ctrl + R 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

Source

share|improve this answer
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 yesterday

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.

share|improve this answer

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

share|improve this answer
    
didn't do that for me on Ubuntu – Michael Durrant yesterday
    
@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 yesterday
    
@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 yesterday

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

fc -ln -1
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.