fzf.vim icon indicating copy to clipboard operation
fzf.vim copied to clipboard

How to highlight the search word in preview window

Open sushovannits opened this issue 4 years ago • 3 comments

  • [x] I have read through https://github.com/junegunn/fzf.vim/blob/master/README.md
  • [x] I have read through https://github.com/junegunn/fzf/blob/master/README-VIM.md
  • [x] I have read through the manual page of fzf (man fzf)
  • [x] I have searched through the existing issues

I am using this configuration https://github.com/junegunn/fzf.vim#example-advanced-ripgrep-integration and wanted to know is there a way to highlight the PATTERN in the preview window by default, Makes it way easier to spot.

sushovannits avatar Mar 28 '20 04:03 sushovannits

  • [x] I have read through https://github.com/junegunn/fzf.vim/blob/master/README.md
  • [x] I have read through https://github.com/junegunn/fzf/blob/master/README-VIM.md
  • [x] I have read through the manual page of fzf (man fzf)
  • [x] I have searched through the existing issues

I am using this configuration https://github.com/junegunn/fzf.vim#example-advanced-ripgrep-integration and wanted to know is there a way to highlight the PATTERN in the preview window by default, Makes it way easier to spot.

Any luck on finding a way to do this? It would be fixed if only the search terms could be put into a var like it was done with "{}".

machocam avatar Oct 30 '20 19:10 machocam

I have the same question.

cooljser avatar Dec 30 '21 06:12 cooljser

I was looking for a solution for this for quite some time, and now have found a solution for fzf in terminal that requires slight modification of fzf source to work even better with ripgrep. I considered posting on junegunn/fzf, but i think it matches this issue quite well, as an integrated solution in fzf would also be available in fzf.vim.

Consider this function to search for a file from terminal: $ fzf_rg_edit_preview <first word> Highlighting the <first-word> in the preview is already possible as is, by piping bat output into ripgrep again, searching for $1.

# Fuzzy find a file that contains supplied term and then edit
fzf_rg_edit_preview(){
    if [[ $# == 0 ]]; then
        echo 'Error: search term was not provided.'
        return
    fi
    local match=$(
      rg --column --color=always --line-number --no-heading --smart-case "${*:-}" |
        fzf --ansi \
            --color "fg:15,bg:-1,hl:1,fg+:-1,bg+:-1,hl+:1,info:-1,prompt:-1,pointer:12,marker:4,spinner:11,header:-1" \
            --delimiter : \
            --preview "bat --theme=base16-256 --color=always {1} --highlight-line {2} 
                | rg --no-heading --colors 'match:bg:yellow' 
                     --ignore-case --color=always --context 1000 '$1'" \
            --preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
      )
    local file=$(echo "$match" | cut -d':' -f1)
    if [[ -n $file ]]; then
        $EDITOR "$file" +$(echo "$match" | cut -d':' -f2)
    fi
}

However, highlighting also the interactively typed words currently seems not possible, as the query term {q} has to be parsed within the preview command. I added two cases ({|q} and {q|}) to the replacePlaceholder function, that inject '|' between words such that it can be used as input for ripgrep. This is available as a fork. Please note, that on (non-default) Ubuntu 20.04 i was only able to compile 0.29.0, not 0.30.0, so the branch is based on that.

Now, the preview command can be written as

--preview "bat --theme=base16-256 --color=always {1} --highlight-line {2} 
                | rg --no-heading --colors 'match:bg:yellow' 
                     --ignore-case --color=always --context 1000 '$1{|q}'" \

Doing $ fzf_rg_edit_preview placeholder in the fzf source gives: 2022-04-05 22-58-53

I hope that this might serve as a starting point for an official solution or at least help others in the meantime.

I noticed some issues with parsing the query, e.g. when entering '{' characters, but atm i don't know enough about it to provide a meaningful fix for that. Also, there is some conflict of the rg command in the --preview with the --preview-window, where if the --context is too in comparison of the file length, the previewd lines will not match. If someone knows a solution for that, please let me know.

I think it might also be possible to use --preview='eval <command string>' with the <command string> including parsed query input, but i am not sure how to do that in a safe way.

iaamp avatar Apr 05 '22 22:04 iaamp