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 frequently search for changes with history | grep 'string'

I get a list of commands in my history, along with the history line number), e.g.

history | grep 'git'

  755  git status
 1535  git push origin master
 1570  git merge origin/one-146
 1667  git reset --hard origin/master

I can now recall and execute a command in one go with !nnn, for example:

!755
git status
# On branch master
nothing to commit, working directory clean

My question is: How can I recall a numbered history command and stay on the commands line for editing and not execute it right away 'as is', the way that ! does, so that I can change a couple of things peform presing return?

share|improve this question

3 Answers 3

You can search back through the history using Ctrl+R. If the history entry is long use the mouse (not the keyboard, that stops the search) to copy and paste part of the command to edit back in.

As @rijsg commented, you can then use the (left and right) arrows or equivalent keys to stop the search and start editing.

share|improve this answer
2  
Any arrow key or equivalent command (Ctrl-a for example) will stop the search leaving the current history entry in the command prompt, without executing it. This may or may not be enough, depending on what @Michael wants. –  njsg Dec 8 '13 at 19:03
    
@rijsg Thanks for the expansion, you are right. –  Zelda Dec 8 '13 at 19:29

If you set the histverify option, e.g.

shopt -s histverify

then all history substitutions are brought up for editing instead of being executed immediately. You would then need to press Enter twice instead of once after typing !755 to execute the command.

You can push arbitrary text onto the history list with history -s. Combine this with fc -nl to list a specific history entry.

history -s "$(fc -nl 755 755)"

then press Up to recall what is now the latest history entry.

share|improve this answer
    
I tried set histverify, set histverify on, set histverify true, set histverify 1, set histverify 0 but none of them produced the behavior described !755 still gets done immediately. –  Michael Durrant Dec 10 '13 at 13:45
    
@MichaelDurrant Why did you try these seemingly random commands? None of them set an option. I linked to the documentation of the shopt builtin in my post: shopt -s histverify. –  Gilles Dec 10 '13 at 14:09
    
ow. good thing I didn't downvote. easy there. Anyway because it still wasn't clear to me what to do. Docs are good but a direct answer also helps :) Links can go dead. I think having $ shopt -s histverify in the answer would help so I'll add it. –  Michael Durrant Dec 10 '13 at 14:14
up vote 0 down vote accepted

I've since adopted another approach to this - using ![line-number]:p

This prints the statement and adds it to history but doesn't actually execute it. I then do up arrow and change it as desired.

I combine this with my hg alias (alias hg='history | grep ') to recall history commands based on some text.

Example:

$ hg checkout

17140   git checkout README.rdoc
17143   git checkout master
17201   git checkout README.rdoc
17204   git checkout master
17923   git checkout .bashrc
18151   git checkout v311

I use this in addition to ctrl-r (reverse history search) because sometimes I prefer to see an immediate list of all the possibilities for a given string rather than just the output on 1 line that ctrl-r shows. After hg [string] I would then do ![line-number]search_string as in the hg checkout shown above.

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.