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:

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?


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.


$ 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.

| improve this answer | |

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.

| improve this answer | |

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.

| 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

If you configure your .bashrc file to use the vi editor at the command line it will make command line edits so much less complex.

1st, once "set -o vi" is enabled, you can simply hit "<esc> k", then keep hitting "k" to go up the history file as it were, if you go past an entry, use "j" to go down.

NOTE: when using vi, there are two modes: command and edit to enter the command mode hit the ESCAPE KEY ONCE, then a,A,i,I, etc. to insert.

Simple cursor pad in vi: left=h, down=j, up=k, right=l http://linuxmeister.net/vi/vi-Summary.jpg

I've added all the entries needed to make this work at: http://linuxmeister.net/Notes/bashrc-simple.html

So, based on the very helpful information above, I was able to use "history" and RECALL a line from "history", WITHOUT executing it. (THANK YOU FOR THAT POST!)

if shopt is listed and histverify is "off" a ! will EXECUTE immediately,

if you've added to your .bashrc, or typed: shopt -s histverify, then a ! allows an edit, for example:

    1  more .bashrc
    2  history
    3  shopt
    4  . ./.bashrc
    5  shopt | grep hist
    6  history
    7  alias
    8  history
    9  shopt
   10  history

--> !5

when I hit !5 the following command appears, but does NOT execute

### if histverify is "on".

shopt | grep hist
cmdhist         on
histappend      on
histreedit      off
histverify      on
lithist         off

Remember that Linus and Richard created Linux and GNU tools to take advantage of UNIX. The vi editor (created by Bill Joy) is one of the best tools to master as it is on EVERY distribution of UNIX and Linux.

All of the other command line editors need to be loaded and require additional software. Using a GUI in Linux (other than Firefox, Thunderbird, and OpenOffice) is like towing your Diesel 4x4 with a 1968 VW Beetle... it'll work, but it sure doesn't make any sense.

Remember what Kernigan said about a GUI, "what you see is all you get".

| improve this answer | |
echo !number

then hit up-arrow, remove the word echo and edit the rest; press enter to execute.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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