bug-bash
[Top][All Lists]
Advanced

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

Re: feature requests - bash history


From: What, me urgent?
Subject: Re: feature requests - bash history
Date: Wed, 3 Aug 2011 14:24:56 -0700 (PDT)

As a follow-up to my post of a few weeks ago (excerpted below), here are two
bash procedures I wrote for my bashrc to demonstrate what I mean. Even if
the idea doesn't rise to a level to be integrated as bash features, maybe
some  readers of this list or other bash users will find these useful.

Cheers.

CONTENTS
=========
hrm - a bash function to remove multiple lines from the bash history
hgrep - display multiple search solutions from within the current shell
context's history


function hrm {
# remove a range of lines from the history
todolist=()

   function usage_message {
      echo -e "\nhrm - remove multiple entries from history\nusage: hrm {
n-m | n,m | n }\n   n-m    delete from entry n until entry m\n   n,m   
starting from entry n, delete m entries\n   n    delete individual entry n"
   }

   function error_message {
      echo "error: a parameter is non-numeric or out of bounds: "$myparm
      usage_message
   }

if [[ -z "$@" ]] || [[ "$1" =~ ^-[h?]$ ]] || [[ "$1" == "--help" ]]; then
   usage_message
   return
fi

for myparm in "$@" ;do

if [[ "$myparm" =~ ^[[:digit:]]*-[[:digit:]]*$ ]]; then
   n=${myparm%%-*}
   m=${myparm##*-}
   if [[ $n == 0 ]] || [[ $m == 0 ]] || \
      [[ $n -ge $HISTCMD ]] || [[ $m -ge $HISTCMD ]] ; then
      error_message
      return
   else
      if [[ $n == $m ]] ; then
         echo "warning: start and end numbers identical: "$myparm
         read -p "continue anyway? (y/N)"
         [ "$REPLY" != "y" ] && return
      elif [[ $n -gt $m ]] ; then
         echo "warning: start "$n", greater than end, "$m
         read -p "continue anyway? (y/N)"
         [ "$REPLY" != "y" ] && return
         z=$m; m=$n; n=$z
      fi
      while [[ $n -le $m ]]; do
         todolist=("address@hidden" $m)
         let m--
      done
   fi

elif [[ "$myparm" =~ ^[[:digit:]]*,[[:digit:]]*$ ]]; then
   n=${myparm%%,*}
   z=${myparm##*,}
   let m=$n+$z-1
   if [[ $n == 0 ]] || [[ $z == 0 ]] || \
      [[ $n -ge $HISTCMD ]] || [[ $m -ge $HISTCMD ]] ; then
      error_message
   else
      while [[ $n -le $m ]]; do
         todolist=("address@hidden" $m)
         let m--
      done
   fi

else
   if ! [[ "$myparm" =~ ^[[:digit:]]*$ ]] || \
      [[ $myparm == 0 ]] ||                  \
      [[ $myparm -ge $HISTCMD ]]             ; then
     error_message
     return
   fi
   todolist=("address@hidden" $myparm)
fi

done

for myparm in $(oldIFS="$IFS"; IFS=$'\n'; \
                sort -g -r -u <<< "${todolist[*]}"; \
                IFS="$oldIFS"); do
   history -d $myparm
done
history -w
}



function hgrep {
if [[ -z "$@" ]] || [[ "$1" =~ ^-[h?]$ ]] || [[ "$1" == "--help" ]]; then
   echo -e "hgrep - grep for bash's history buffer\nUsage: hgrep [grep
options] [grep pattern]"
   return
fi
history | grep -v -e hgrep -e history | grep $*
echo "To edit a line, enter  !nn <alt> ^"
}





> 1] display multiple solutions for a bash history search - Instead of a
> user having to repeat the crtl-r/ctrl-s sequence, and have each solution
> overwrite the previous one, allow the user to prefix ctrl-r/ctrl-s with an
> integer for the maximum number of entries to display. Maybe prefix each
> solution with it's line number in the history, so that the desired
> solution could be invoked using !nn
> 
> 2] Add a feature for history -d to delete more than one line of the
> history. Maybe history -d n,m to delete lines n through m, or history -d
> n:m to delete m lines beginning with line number n.
> 
> 3] Add a feature for history -d to delete multiple lines based upon a 
> regular expression.
> EXAMPLES:
> history -d n ^man* - delete the n most recent history lines that begin
> with "man"
> history -d n:m ^man* - delete the first m lines that begin with "man",
> starting at line n.
> history -d n,m ^man* - delete all occurrences of ^man* between lines n and
> m.
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/feature-requests---bash-history-tp32008330p32190052.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]