bash history customizing

bash history customizing

Post by Gabki » Sat, 29 May 2004 15:55:29



forgive me if this has been asked before, im a newbie to this
newsgroup, and a google search couldnt find me exactly what im looking
for.

I really like my bash history facility, so much so that i have about
the maximum size set to 50000 and my HISTIGNORE list is rather large.

I found a way to stop history from remembering duplicate commands
(HISTCONTROL=ignoredups), which helps, but I want it to go further.

For one thing, I would like to prevent invalid commands from being
saved in my history. (not terminated commands, if possible, just
invalid ones, i.e. "cd $HOMEE" or "emax afile.txt")
Does anyone know of a good way to do this?

I also want to eliminate all duplicate commands in my history file,
but I want to keep the most recent duplicate only.
In other words if my history contains this...

1 cd $HOME
2 vi somefile.txt
3 cp somefile anotherfile
4 exit
5 cd $HOME
6 vi somefile
7 exit

I would like it to become

1 cp somefile anotherfile
2 cd $HOME
3 vi somefile
4 exit

This way, over time most of my useful, regularly used commands would
rise to the top, while the useless ones would sift to the bottom of
the list. (for eventual manual purging?)

The obvious way is via a sort -u, but this sorts alphabetically which
is totally not what I want.

The only way I can thionk of doing this is via a regularly run script,
is ther a better way of doing it?
Surely im not the only one who has thought of this before...

 
 
 

bash history customizing

Post by Thorsten Kamp » Sat, 29 May 2004 18:22:16


* Gabkin (2004-05-28 08:55 +0100)

Quote:> I also want to eliminate all duplicate commands in my history file,
> but I want to keep the most recent duplicate only.
> In other words if my history contains this...

> 1 cd $HOME
> 2 vi somefile.txt
> 3 cp somefile anotherfile
> 4 exit
> 5 cd $HOME
> 6 vi somefile
> 7 exit

> I would like it to become

> 1 cp somefile anotherfile
> 2 cd $HOME
> 3 vi somefile
> 4 exit

> This way, over time most of my useful, regularly used commands would
> rise to the top, while the useless ones would sift to the bottom of
> the list. (for eventual manual purging?)

> The obvious way is via a sort -u, but this sorts alphabetically which
> is totally not what I want.

> The only way I can thionk of doing this is via a regularly run script,
> is ther a better way of doing it?

uniq or zsh

 
 
 

bash history customizing

Post by Gabki » Sat, 29 May 2004 21:12:10


On Fri, 28 May 2004 11:22:16 +0200, Thorsten Kampe


>* Gabkin (2004-05-28 08:55 +0100)
>> I also want to eliminate all duplicate commands in my history file,
>> but I want to keep the most recent duplicate only.
>> In other words if my history contains this...

>> 1 cd $HOME
>> 2 vi somefile.txt
>> 3 cp somefile anotherfile
>> 4 exit
>> 5 cd $HOME
>> 6 vi somefile
>> 7 exit

>> I would like it to become

>> 1 cp somefile anotherfile
>> 2 cd $HOME
>> 3 vi somefile
>> 4 exit

>> This way, over time most of my useful, regularly used commands would
>> rise to the top, while the useless ones would sift to the bottom of
>> the list. (for eventual manual purging?)

>> The obvious way is via a sort -u, but this sorts alphabetically which
>> is totally not what I want.
>uniq or zsh

uniq cant do anything that a sort -u cant do, unless there is some
kind of uniq option to get rid of duplicates but keep the _last_
duplicate, not the first.
Also, I dont want to sort the history alphabetically, Im trying to
sort it by common usage.

As for zsh, I'm a bash man! (but does zsh have an evolutionary history
facility like this?)

 
 
 

bash history customizing

Post by Ed Morto » Sat, 29 May 2004 21:41:37


<snip>

Quote:> I also want to eliminate all duplicate commands in my history file,
> but I want to keep the most recent duplicate only.
> In other words if my history contains this...

> 1 cd $HOME
> 2 vi somefile.txt
> 3 cp somefile anotherfile
> 4 exit
> 5 cd $HOME
> 6 vi somefile
> 7 exit

> I would like it to become

> 1 cp somefile anotherfile
> 2 cd $HOME
> 3 vi somefile
> 4 exit

Unless you get a better suggestion that doesn't involve a script, this
will do it:

tac file | awk '$1 in cmds{next}{cmds[$1]="";print}' | tac

Regards,

        Ed.

 
 
 

bash history customizing

Post by Stephane CHAZELA » Sat, 29 May 2004 22:55:36


2004-05-28, 14:12(+02), Gabkin:
[...]

Quote:> As for zsh, I'm a bash man! (but does zsh have an evolutionary history
> facility like this?)

Yes, and much more. Some customizations possible for history:

APPEND_HISTORY <D>
     If this is set, zsh sessions will append their history list to the
     history file, rather than overwrite it. Thus, multiple parallel
     zsh sessions will all have their history lists added to the
     history file, in the order they are killed.

EXTENDED_HISTORY <C>
     Save each command's beginning timestamp (in seconds since the
     epoch) and the duration (in seconds) to the history file.  The
     format of this prefixed data is:

     `:<BEGINNING TIME>:<ELAPSED SECONDS>:<COMMAND>'.

HIST_ALLOW_CLOBBER
     Add `|' to output redirections in the history.  This allows history
     references to clobber files even when CLOBBER is unset.

HIST_BEEP <D>
     Beep when an attempt is made to access a history entry which isn't
     there.

HIST_EXPIRE_DUPS_FIRST
     If the internal history needs to be trimmed to add the current
     command line, setting this option will cause the oldest history
     event that has a duplicate to be lost before losing a unique event
     from the list.  You should be sure to set the value of HISTSIZE to
     a larger number than SAVEHIST in order to give you some room for
     the duplicated events, otherwise this option will behave just like
     HIST_IGNORE_ALL_DUPS once the history fills up with unique events.

HIST_FIND_NO_DUPS
     When searching for history entries in the line editor, do not
     display duplicates of a line previously found, even if the
     duplicates are not contiguous.

HIST_IGNORE_ALL_DUPS
     If a new command line being added to the history list duplicates an
     older one, the older command is removed from the list (even if it
     is not the previous event).

HIST_IGNORE_DUPS (-h)
     Do not enter command lines into the history list if they are
     duplicates of the previous event.

HIST_IGNORE_SPACE (-g)
     Remove command lines from the history list when the first
     character on the line is a space, or when one of the expanded
     aliases contains a leading space.  Note that the command lingers
     in the internal history until the next command is entered before
     it vanishes, allowing you to briefly reuse or edit the line.  If
     you want to make it vanish right away without entering another
     command, type a space and press return.

HIST_NO_FUNCTIONS
     Remove function definitions from the history list.  Note that the
     function lingers in the internal history until the next command is
     entered before it vanishes, allowing you to briefly reuse or edit
     the definition.

HIST_NO_STORE
     Remove the history (fc -l) command from the history list when
     invoked.  Note that the command lingers in the internal history
     until the next command is entered before it vanishes, allowing you
     to briefly reuse or edit the line.

HIST_REDUCE_BLANKS
     Remove superfluous blanks from each command line being added to
     the history list.

HIST_SAVE_NO_DUPS
     When writing out the history file, older commands that duplicate
     newer ones are omitted.

HIST_VERIFY
     Whenever the user enters a line with history expansion, don't
     execute the line directly; instead, perform history expansion and
     reload the line into the editing buffer.

INC_APPEND_HISTORY
     This options works like APPEND_HISTORY except that new history
     lines are added to the $HISTFILE incrementally (as soon as they are
     entered), rather than waiting until the shell is killed.  The file
     is periodically trimmed to the number of lines specified by
     $SAVEHIST, but can exceed this value between t*s.

SHARE_HISTORY <K>
     This option both imports new commands from the history file, and
     also causes your typed commands to be appended to the history file
     (the latter is like specifying INC_APPEND_HISTORY).  The history
     lines are also output with timestamps ala EXTENDED_HISTORY (which
     makes it easier to find the spot where we left off reading the
     file after it gets re-written).

     By default, history movement commands visit the imported lines as
     well as the local lines, but you can toggle this on and off with
     the set-local-history zle binding.  It is also possible to create
     a zle widget that will make some commands ignore imported
     commands, and some include them.

     If you find that you want more control over when commands get
     imported, you may wish to turn SHARE_HISTORY off,
     INC_APPEND_HISTORY on, and then manually import commands whenever
     you need them using `fc -RI'.

I have:
HISTFILE=~/.zsh-history.$ZSH_VERSION
HISTSIZE=500
SAVEHIST=50000
setopt HIST_IGNORE_ALL_DUPS HIST_REDUCE_BLANKS \
  HIST_SAVE_NO_DUPS HIST_VERIFY INC_APPEND_HISTORY NO_BANG_HIST

--
Stephane

 
 
 

bash history customizing

Post by Thorsten Kamp » Sat, 29 May 2004 22:25:21


* Gabkin (2004-05-28 14:12 +0100)

> On Fri, 28 May 2004 11:22:16 +0200, Thorsten Kampe

>>* Gabkin (2004-05-28 08:55 +0100)
>>> I also want to eliminate all duplicate commands in my history file,
>>> but I want to keep the most recent duplicate only.
>>> In other words if my history contains this...

>>> 1 cd $HOME
>>> 2 vi somefile.txt
>>> 3 cp somefile anotherfile
>>> 4 exit
>>> 5 cd $HOME
>>> 6 vi somefile
>>> 7 exit

>>> I would like it to become

>>> 1 cp somefile anotherfile
>>> 2 cd $HOME
>>> 3 vi somefile
>>> 4 exit

>>> This way, over time most of my useful, regularly used commands would
>>> rise to the top, while the useless ones would sift to the bottom of
>>> the list. (for eventual manual purging?)

>>> The obvious way is via a sort -u, but this sorts alphabetically which
>>> is totally not what I want.

>>uniq or zsh

> uniq cant do anything that a sort -u cant do,

Think again: uniq has the ability /not to sort/ and "sort" hasn't got
this ability.

Quote:> unless there is some kind of uniq option to get rid of duplicates
> but keep the _last_ duplicate, not the first.

tac | uniq | tac

Quote:> Also, I dont want to sort the history alphabetically, Im trying to
> sort it by common usage.

> As for zsh, I'm a bash man!

So what? If you want basic functionality you use a basic tool suited
for that (bash). If you want extended functionality you use a tool
appropriate for that (zsh)

Thorsten

 
 
 

bash history customizing

Post by Ed Morto » Sat, 29 May 2004 22:33:15



> * Gabkin (2004-05-28 14:12 +0100)
<snip>
>>uniq cant do anything that a sort -u cant do,

> Think again: uniq has the ability /not to sort/ and "sort" hasn't got
> this ability.

I guess that's one way to look at it, but uniq relies on the file
already having been sorted.

Quote:>>unless there is some kind of uniq option to get rid of duplicates
>>but keep the _last_ duplicate, not the first.

> tac | uniq | tac

Wouldn't work since the input file isn't sorted.

        Ed.

 
 
 

bash history customizing

Post by Thorsten Kamp » Sat, 29 May 2004 23:03:13


* Ed Morton (2004-05-28 15:33 +0100)


>> * Gabkin (2004-05-28 14:12 +0100)
> <snip>
>>>uniq cant do anything that a sort -u cant do,

>> Think again: uniq has the ability /not to sort/ and "sort" hasn't got
>> this ability.

> I guess that's one way to look at it, but uniq relies on the file
> already having been sorted.

>>>unless there is some kind of uniq option to get rid of duplicates
>>>but keep the _last_ duplicate, not the first.

>> tac | uniq | tac

> Wouldn't work since the input file isn't sorted.

Okay, thanks, my mistake.

Thorsten

 
 
 

1. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(

Regards,

Neil.

--


------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. context switch and the FP registers

3. bash history -s appends to the previous history

4. How can I setup my sound card ffor slackware

5. Customize history shell

6. Where is the C Compiler on newly installed AIX 4.3.2 ?

7. Customizing Completion in Bash

8. 2.2.5 kernel panic after adding RAM

9. Customized installation with customized comps file...

10. searching over the bash history

11. bash history substitution

12. partial history completion in bash

13. bash history