Translate

Saturday, August 26, 2023

Fuzzy Finder FZF with Syntax Highlight Preview

FZF is nice cli program that allow quick file navigation that find files as you type.
BAT is nice replacement for cat with highlight features

sudo apt install fzf
sudo apt install bat

Using fzf together with batcat to source code files and preview it with syntax highlight

fzf --preview 'batcat --style=numbers --color=always --line-range :500 {}'

.bashrc

source /usr/share/doc/fzf/examples/key-bindings.bash

if type rg &> /dev/null; then
  export FZF_DEFAULT_COMMAND='rg --files'
  export FZF_DEFAULT_OPTS='-m --height 50% --border'
fi

alias findz="fzf --preview 'batcat --style=numbers --color=always --line-range :500 {}'"

_fzf_comprun() {
  local command=$1
  shift

  case "$command" in
    cd)           fzf --preview 'tree -C {} | head -200'   "$@" ;;
    export|unset) fzf --preview "eval 'echo \$'{}"         "$@" ;;
    ssh)          fzf --preview 'dig {}'                   "$@" ;;
    *)            fzf --preview 'batcat -n --color=always {}' "$@" ;;
  esac
}

export FZF_CTRL_R_OPTS="
  --preview 'echo {}' --preview-window up:3:hidden:wrap
  --bind 'ctrl-/:toggle-preview'
  --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'
  --color header:italic
  --header 'Press CTRL-Y to copy command into clipboard'"

export FZF_ALT_C_OPTS="--preview 'tree -C {}'"

Usage:

vim $(fzf)

Ctrl+R   # open command history
Alt+C   # show directories and cd to it
Vim **<tab>  #open file list and open with vim
cd **<tab> # open directory list and cd to it

(fzf.vim) .vimrc

command! -bang -nargs=* Rg call fzf#vim#grep("rg --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1, {'options': '--delimiter : --nth 4..'}, <bang>0)

set grepprg=rg\ --vimgrep\ --smart-case\ --follow
command! -bang -nargs=? -complete=dir Files
    \ call fzf#vim#files(<q-args>, {'options': ['--layout=reverse', '--info=inline', '--preview', '~/.vim/bundle/fzf.vim/bin/preview.sh {}']}, <bang>0)

"Open Files in the bottom

"let g:fzf_layout = { 'up':  '40%'}
"let g:fzf_layout = { 'down':  '40%'}

No comments:

Blog Archive