3

I am working on a zsh widget to 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. sk is an alternative for fzf.

This widget works pretty well except multi-line commands.

For example: it shows

enter image description here

like:

enter image description here

So, it replace newline with \n. This is not something i want.

The command looks like the following in .zsh_history file:

: 1657176206:0;function brcd() {\
 cd $(broot --only-folders --hidden --cmd "$1;:pp")\
}\

What I found is !NUM get the history in correct format.

enter image description here

what changes do I need to make on my widget? How can I solve this problem?

3

1 Answer 1

0

OP Here. The solution is:

skim-history() {
  num=$(fc -l 1 -1 | sk --tac --no-sort --exact | rg -o '^[ ]*([0-9]*)[ ]*' -r '$1')

  if [ -n "$num" ]; then
      zle vi-fetch-history -n $num
  fi
}

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .