[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash history



Arnt Karlsen wrote:
> Bob wrote in message 
> > Arnt Karlsen wrote:
> > > ..from grep --help |less: ;o)
> > >  -i, --ignore-case         ignore case distinctions
> > 
> > That is a grep option.  It doesn't apply to bash's history search.
> 
> ..correct, that's _why_ we use such grep pipes instead. ;o)

Uhm...  You can't use grep to substitute for bash's Control-r
function.  You have completely misunderstood the question!  Let me add
some more lengthy explanation to clarify the operation.

       reverse-search-history (C-r)
              Search backward starting at the current line and moving
              `up' through the history as necessary.  This is an
              incremental search.

That is the emacs mode key which is almost the same as ESC-/ in vi
editing mode.  It searches through your command history and recalls
matching commands to the command line.  You can then edit the command
and press Enter which then executes the command line.  This is the
WYSIWYG command line editing replacement for the old csh's ! history
substitution mechanism.[1]

Let's contrive an example.  Normally when using bash you type in
several commands.  Let's say you are editing a file with ed and then
compiling the file and then linking the file.

  $ ed main.c
  $ cc -g -o a.out main.c

Then you go to do it again.  You know that ed was previously in your
history.  So you type in "C-r ed" in order to recall the command
line.  On your terminal command line after typing that in you see:

  (reverse-I-search)`ed': ed main.c

Since that is the command you want to run again you press Enter and
the command is invoked.  You edit the file.  Then wanting to compile
it you type in "C-r cc" and see:

  (reverse-I-search)`cc': cc -g -o a.out main.c

Again since that is the command you want to invoke you press Enter and
execute that command line.  If it wasn't main.c that I wanted to
compile but lib.c instead and I had compiled it previously then I
would press C-r again in order to find the previous line matching
"cc".  If I find there are lines matching cc but matching elsewhere on
the line then I type in a space.  It is an incremental search and will
match incrementally as many characters as I type in and show me the
results immediately.  I can press DEL (the Backspace key) to back up
and type in different characters.  If there aren't any exactly
matching commands you may simply edit the command that you see and
change it into the command that you want.

Obviously that is a contrived example to illustrate the use of C-r
from the command line.  I would normally use C-o a lot in those types
of sequences.  I would use ESC-. a lot (ESC-_ for the vi crowd).  I
would use Makefiles.  I would use emacs flymake mode.  There are a
lot of things that would be done to optimize this routine so let's not
go there please.

Using grep on bash's history is not a replacement for C-r.

Bob

[1] In csh days one would say "!ed" and "!cc" and search the history
and execute the matched command.  Or more complicated "!42" or even
editing the line with "!42:s/main.c/lib.c/" or even crazy commands
like "ed !408:2:s/.c/.s/" to edit the second argument from line 408.
Sometimes those didn't work out so well and unexpected commands were
executed by mistake.  But in those days of paper printing terminals
the previous commands were printed on paper and once printed were
always visible and not changed.  It was easy to look back on the roll
that came off and pick out a command and run it again.  And you always
printed the command number in the prompt to make it easier.  CRTs made
this easier with WYSIWYG editing.  Those extreme contortions were no
longer necessary.

Command line editing with C-r and then editing your command line
really made things easier because you saw immediately what you were
going to invoke.

Attachment: signature.asc
Description: Digital signature


Reply to: