zsh-users
 help / color / mirror / code / Atom feed
* How to parse zsh history
@ 2022-07-07  5:56 Ahmad Ismail
  2022-07-07  6:40 ` Ahmad Ismail
       [not found] ` <1728479877.503410.1657201438041@mail.virginmedia.com>
  0 siblings, 2 replies; 6+ messages in thread
From: Ahmad Ismail @ 2022-07-07  5:56 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 2129 bytes --]

My history file looks like:

....
: 1656846180:0;mint-vm
: 1657092275:4;g d fpull
: 1657092331:0;stow-dotfiles
: 1657092447:0;fd --hidden --ignore-file .gitignore
--base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
--type l --type f | sd ^. $HOME | xargs -I{} rm {}\

: 1657092530:0;fd --hidden
--base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
--type l --type f | sd ^. $HOME | xargs -I{} rm {}\
stow --target="$HOME" --dir="$HOME/.dotfiles" --no-folding --restow
.common-dotfiles-buggy-nemo-bookmarks\

: 1657092737:0;for i in "${array[@]}"\
do\
 \
 DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
 FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
 \
 wget $DOWNLOAD_URL\
 \
\
done
: 1657092752:15;array=(
https://cinnamon-spices.linuxmint.com/applets/view/83\
 https://cinnamon-spices.linuxmint.com/applets/view/106\
 https://cinnamon-spices.linuxmint.com/applets/view/222\
 https://cinnamon-spices.linuxmint.com/applets/view/238\
 https://cinnamon-spices.linuxmint.com/applets/view/284\
https://cinnamon-spices.linuxmint.com/applets/view/303 )\
\
for i in "${array[@]}"\
do\
 \
 DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
 FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
 \
 wget $DOWNLOAD_URL\
 \
\
done
....

I am working on a widget for fuzzy search history.

....
skim-history() {
  origquery=${BUFFER}
  output=$(history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort
--exact)

  if [ $? -eq 0 ]; then
    BUFFER=$output
  else
    BUFFER=$origquery
  fi

  CURSOR=$#BUFFER
}

zle     -N   skim-history
bindkey '^R' skim-history
....

In `history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact`,
sd is used instead of sed, which just removes numbers from the beginning.
However, it messes things up if the command is multi-line.

What I found is, the only way zsh history parsing works correctly is if I
use something like !NUM, to get the history. How can I edit my zle
extension to do that?

Another option is (if previous fails), do not save multi line commands in
history. How can I do that?

[-- Attachment #2: Type: text/html, Size: 3173 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to parse zsh history
  2022-07-07  5:56 How to parse zsh history Ahmad Ismail
@ 2022-07-07  6:40 ` Ahmad Ismail
  2022-07-07 10:59   ` Ahmad Ismail
       [not found] ` <1728479877.503410.1657201438041@mail.virginmedia.com>
  1 sibling, 1 reply; 6+ messages in thread
From: Ahmad Ismail @ 2022-07-07  6:40 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 3191 bytes --]

The way fzf do it is

....
fzf-history-widget() {
  local selected num
  setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2>
/dev/null
  selected=( $(fc -rl 1 | perl -ne 'print if
!$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
    FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS
-n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore
$FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
  local ret=$?
  if [ -n "$selected" ]; then
    num=$selected[1]
    if [ -n "$num" ]; then
      zle vi-fetch-history -n $num
    fi
  fi
  zle reset-prompt
  return $ret
}
zle     -N            fzf-history-widget
bindkey -M emacs '^R' fzf-history-widget
bindkey -M vicmd '^R' fzf-history-widget
bindkey -M viins '^R' fzf-history-widget
....

but I want to do it with skim instead of fzf.

On Thu, Jul 7, 2022 at 11:56 AM Ahmad Ismail <ismail783@gmail.com> wrote:

> My history file looks like:
>
> ....
> : 1656846180:0;mint-vm
> : 1657092275:4;g d fpull
> : 1657092331:0;stow-dotfiles
> : 1657092447:0;fd --hidden --ignore-file .gitignore
> --base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
> --type l --type f | sd ^. $HOME | xargs -I{} rm {}\
>
> : 1657092530:0;fd --hidden
> --base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
> --type l --type f | sd ^. $HOME | xargs -I{} rm {}\
> stow --target="$HOME" --dir="$HOME/.dotfiles" --no-folding --restow
> .common-dotfiles-buggy-nemo-bookmarks\
>
> : 1657092737:0;for i in "${array[@]}"\
> do\
>  \
>  DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
>  FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
>  \
>  wget $DOWNLOAD_URL\
>  \
> \
> done
> : 1657092752:15;array=(
> https://cinnamon-spices.linuxmint.com/applets/view/83\
>  https://cinnamon-spices.linuxmint.com/applets/view/106\
>  https://cinnamon-spices.linuxmint.com/applets/view/222\
>  https://cinnamon-spices.linuxmint.com/applets/view/238\
>  https://cinnamon-spices.linuxmint.com/applets/view/284\
> https://cinnamon-spices.linuxmint.com/applets/view/303 )\
> \
> for i in "${array[@]}"\
> do\
>  \
>  DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
>  FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
>  \
>  wget $DOWNLOAD_URL\
>  \
> \
> done
> ....
>
> I am working on a widget for fuzzy search history.
>
> ....
> skim-history() {
>   origquery=${BUFFER}
>   output=$(history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort
> --exact)
>
>   if [ $? -eq 0 ]; then
>     BUFFER=$output
>   else
>     BUFFER=$origquery
>   fi
>
>   CURSOR=$#BUFFER
> }
>
> zle     -N   skim-history
> bindkey '^R' skim-history
> ....
>
> In `history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact`,
> sd is used instead of sed, which just removes numbers from the beginning.
> However, it messes things up if the command is multi-line.
>
> What I found is, the only way zsh history parsing works correctly is if I
> use something like !NUM, to get the history. How can I edit my zle
> extension to do that?
>
> Another option is (if previous fails), do not save multi line commands in
> history. How can I do that?
>
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 4693 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to parse zsh history
  2022-07-07  6:40 ` Ahmad Ismail
@ 2022-07-07 10:59   ` Ahmad Ismail
  0 siblings, 0 replies; 6+ messages in thread
From: Ahmad Ismail @ 2022-07-07 10:59 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 3935 bytes --]

My current solution is:
....
skim-history() {
  origquery=${BUFFER}
  # output=$(history 1 -1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort
--exact)
  # output=$(fc -ln 1 -1 | sk --tac --no-sort --exact)
  num=$(fc -l 1 -1 | sk --tac --no-sort --exact | rg -o '^[ ]*([0-9]*)[ ]*'
-r '$1')

 if [ -n "$num" ]; then
    BUFFER=$(fc -ln $num $num)
  else
    BUFFER=$origquery
  fi

  # CURSOR=$#BUFFER
  # zle redisplay
}

zle     -N   skim-history
bindkey '^R' skim-history
....

Please let me know how can i make it better.

On Thu, Jul 7, 2022 at 12:40 PM Ahmad Ismail <ismail783@gmail.com> wrote:

> The way fzf do it is
>
> ....
> fzf-history-widget() {
>   local selected num
>   setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2>
> /dev/null
>   selected=( $(fc -rl 1 | perl -ne 'print if
> !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
>     FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS
> -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore
> $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
>   local ret=$?
>   if [ -n "$selected" ]; then
>     num=$selected[1]
>     if [ -n "$num" ]; then
>       zle vi-fetch-history -n $num
>     fi
>   fi
>   zle reset-prompt
>   return $ret
> }
> zle     -N            fzf-history-widget
> bindkey -M emacs '^R' fzf-history-widget
> bindkey -M vicmd '^R' fzf-history-widget
> bindkey -M viins '^R' fzf-history-widget
> ....
>
> but I want to do it with skim instead of fzf.
>
> On Thu, Jul 7, 2022 at 11:56 AM Ahmad Ismail <ismail783@gmail.com> wrote:
>
>> My history file looks like:
>>
>> ....
>> : 1656846180:0;mint-vm
>> : 1657092275:4;g d fpull
>> : 1657092331:0;stow-dotfiles
>> : 1657092447:0;fd --hidden --ignore-file .gitignore
>> --base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
>> --type l --type f | sd ^. $HOME | xargs -I{} rm {}\
>>
>> : 1657092530:0;fd --hidden
>> --base-directory="$HOME/.dotfiles/.common-dotfiles-buggy-nemo-bookmarks"
>> --type l --type f | sd ^. $HOME | xargs -I{} rm {}\
>> stow --target="$HOME" --dir="$HOME/.dotfiles" --no-folding --restow
>> .common-dotfiles-buggy-nemo-bookmarks\
>>
>> : 1657092737:0;for i in "${array[@]}"\
>> do\
>>  \
>>  DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
>>  FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
>>  \
>>  wget $DOWNLOAD_URL\
>>  \
>> \
>> done
>> : 1657092752:15;array=(
>> https://cinnamon-spices.linuxmint.com/applets/view/83\
>>  https://cinnamon-spices.linuxmint.com/applets/view/106\
>>  https://cinnamon-spices.linuxmint.com/applets/view/222\
>>  https://cinnamon-spices.linuxmint.com/applets/view/238\
>>  https://cinnamon-spices.linuxmint.com/applets/view/284\
>> https://cinnamon-spices.linuxmint.com/applets/view/303 )\
>> \
>> for i in "${array[@]}"\
>> do\
>>  \
>>  DOWNLOAD_URL=$(curl -Lfs ${i} | grep -m1 -Po '(?<=href=")[^"]*zip')\
>>  FILE_NAME=$(echo $DOWNLOAD_URL | grep -Eo "([^\/]+$)")\
>>  \
>>  wget $DOWNLOAD_URL\
>>  \
>> \
>> done
>> ....
>>
>> I am working on a widget for fuzzy search history.
>>
>> ....
>> skim-history() {
>>   origquery=${BUFFER}
>>   output=$(history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort
>> --exact)
>>
>>   if [ $? -eq 0 ]; then
>>     BUFFER=$output
>>   else
>>     BUFFER=$origquery
>>   fi
>>
>>   CURSOR=$#BUFFER
>> }
>>
>> zle     -N   skim-history
>> bindkey '^R' skim-history
>> ....
>>
>> In `history -1 1 | sd '^[ ]*[0-9]*[ ]*' '' | sk --tac --no-sort --exact`,
>> sd is used instead of sed, which just removes numbers from the beginning.
>> However, it messes things up if the command is multi-line.
>>
>> What I found is, the only way zsh history parsing works correctly is if I
>> use something like !NUM, to get the history. How can I edit my zle
>> extension to do that?
>>
>> Another option is (if previous fails), do not save multi line commands in
>> history. How can I do that?
>>
>>
>>
>>
>>
>>

[-- Attachment #2: Type: text/html, Size: 5771 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to parse zsh history
       [not found] ` <1728479877.503410.1657201438041@mail.virginmedia.com>
@ 2022-07-07 13:46   ` Peter Stephenson
  2022-07-07 18:16     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2022-07-07 13:46 UTC (permalink / raw)
  To: zsh-user

Sorry, *still* don't know how to reply to everybody...

On 07/07/2022 06:56 Ahmad Ismail <ismail783@gmail.com> wrote:
> I am working on a widget for fuzzy search history.

I guess you want some variation on

print -l ${history[(R)*(#a2)stuffhere*]}

--- to be clear, this is outputting all history lines that are in shell
memory that contain a string "stuffhere" with up to two approximations.

I should have a play with that and let us know where you get stuck.

$history is in the standard zsh/parameter module, so since you almost
certainly have completion loaded it should be available already.

If you need to load from files before search, fc has numerous options
for that, including one to load to a local context so you don't pollute
the global history.

pws


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to parse zsh history
  2022-07-07 13:46   ` Peter Stephenson
@ 2022-07-07 18:16     ` Bart Schaefer
  2022-07-18 12:48       ` Ahmad Ismail
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2022-07-07 18:16 UTC (permalink / raw)
  To: zsh-user

On Thu, Jul 7, 2022 at 6:46 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> If you need to load from files before search, fc has numerous options
> for that, including one to load to a local context so you don't pollute
> the global history.

In particular
fc -p -a /dev/null $(wc -l < $filename) 0
fc -R $filename

will load the contents of $filename into $history for the scope of the
containing function, and then discard it when the function returns.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to parse zsh history
  2022-07-07 18:16     ` Bart Schaefer
@ 2022-07-18 12:48       ` Ahmad Ismail
  0 siblings, 0 replies; 6+ messages in thread
From: Ahmad Ismail @ 2022-07-18 12:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-user

[-- Attachment #1: Type: text/plain, Size: 603 bytes --]

*Thank you very much.*


On Fri, Jul 8, 2022 at 12:17 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Thu, Jul 7, 2022 at 6:46 AM Peter Stephenson
> <p.w.stephenson@ntlworld.com> wrote:
> >
> > If you need to load from files before search, fc has numerous options
> > for that, including one to load to a local context so you don't pollute
> > the global history.
>
> In particular
> fc -p -a /dev/null $(wc -l < $filename) 0
> fc -R $filename
>
> will load the contents of $filename into $history for the scope of the
> containing function, and then discard it when the function returns.
>
>

[-- Attachment #2: Type: text/html, Size: 1465 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-07-18 12:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-07  5:56 How to parse zsh history Ahmad Ismail
2022-07-07  6:40 ` Ahmad Ismail
2022-07-07 10:59   ` Ahmad Ismail
     [not found] ` <1728479877.503410.1657201438041@mail.virginmedia.com>
2022-07-07 13:46   ` Peter Stephenson
2022-07-07 18:16     ` Bart Schaefer
2022-07-18 12:48       ` Ahmad Ismail

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).