-
Notifications
You must be signed in to change notification settings - Fork 129
Preview
You can make full use of fzf's --preview option when using fzf-tab .
If you don't know what it is, please read junegunn/fzf#preview-window first.
An example:
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'exa -1 --color=always $realpath' # remember to use single quote here!!!
fzf-tab defines some variables for better previewing.
This is the string fzf shows to you.
Example: --accessed use the accessed timestamp field, README.md
This is the real string to be insert into your commandline.
For example, if the $desc is --accessed use the accessed timestamp field, the $word is --accessed.
This is the description of the group which the $word belongs to.
For example, --accessed belongs to group named [option], and README.md belongs to [filename].
README.md belongs to [filename] to completing exa, but belongs to [files] when completing ls.
NOTE: To use this variable, please make sure you have set zstyle ':completion:*:descriptions' format '[%d]'.
If you are completing a path and want to access this path when previewing, then you should use $realpath.
For example, if you are completing directories in /usr/, $word will be something like bin, lib,
but $realpath will be /usr/bin, /usr/lib.
Feel free to contribute your preview~

# give a preview of commandline arguments when completing `kill`
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
zstyle ':fzf-tab:complete:(kill|ps):argument-rest' fzf-preview \
[[ $group == "[process ID]" ]] && ps --pid=$word -o cmd --no-headers -w -w'
zstyle ':fzf-tab:complete:(kill|ps):argument-rest' fzf-flags --preview-window=down:3:wrapNOTE: there is a ' symbol above to the left of the [[ $group... that for some reason doesn't render when copying and pasting the above text

zstyle ':fzf-tab:complete:systemctl-*:*' fzf-preview 'SYSTEMD_COLORS=1 systemctl status $word'- directory:
- plain text:
- image:
and more! (xls, xlsx, tar.gz, etc)
zstyle ':fzf-tab:complete:*:*' fzf-preview 'less $realpath'
export LESSOPEN='|~/.lessfilter %s'#! /usr/bin/env sh
# this is a example of .lessfilter, you can change it
mime=$(file -bL --mime-type "$1")
category=${mime%%/*}
kind=${mime##*/}
if [ -d "$1" ]; then
exa --git -hl --color=always --icons "$1"
elif [ "$category" = image ]; then
chafa "$1"
exiftool "$1"
elif [ "$kind" = vnd.openxmlformats-officedocument.spreadsheetml.sheet ] || \
[ "$kind" = vnd.ms-excel ]; then
in2csv "$1" | xsv table | bat -ltsv --color=always
elif [ "$category" = text ]; then
bat --color=always "$1"
else
lesspipe.sh "$1" | bat --color=always
fi
# lesspipe.sh don't use exa, bat and chafa, it use ls and exiftool. so we create a lessfilter.
zstyle ':fzf-tab:complete:(-command-|-parameter-|-brace-parameter-|export|unset|expand):*' \
fzf-preview 'echo ${(P)word}'
# it is an example. you can change it
zstyle ':fzf-tab:complete:git-(add|diff|restore):*' fzf-preview \
'git diff $word | delta'|
zstyle ':fzf-tab:complete:git-log:*' fzf-preview \
'git log --color=always $word'
zstyle ':fzf-tab:complete:git-help:*' fzf-preview \
'git help $word | bat -plman --color=always'
zstyle ':fzf-tab:complete:git-show:*' fzf-preview \
'case "$group" in
"commit tag") git show --color=always $word ;;
*) git show --color=always $word | delta ;;
esac'
zstyle ':fzf-tab:complete:git-checkout:*' fzf-preview \
'case "$group" in
"modified file") git diff $word | delta ;;
"recent commit object name") git show --color=always $word | delta ;;
*) git log --color=always $word ;;
esac'
Requirements:
In section show-file-contents, we have known how to preview a file (directory, other file...)
and we should install https://github.com/petronny/pinyin-completion.
when we press Ctrlx+h, we will see
❯ cd Desktop/dj
tags in context :completion::complete:cd::
local-directories (_cd)
tags in context :completion::user-expand:::
all-expansions expansions original (_user_expand _ftb__main_complete -ftb-complete)
just change :completion::user-expand:* to :fzf-tab:user-expand:*.
`` zstyle ':fzf-tab:user-expand:*' fzf-preview 'less ${(Q)word}'
the `(Q)` unquote the word. if you don't do so, fzf-tab will execute `less '大家\ 0'`not `less '大家 0'`.
### Other tips
if you don't want to use any alias or you want to use the full path,
you will find the fzf-tab completion tag for man cannot work for `\man` and `/usr/bin/man`.
the method is change your zshrc to
zstyle ':fzf-tab:complete:(\|)run-help:' fzf-preview 'run-help $word' zstyle ':fzf-tab:complete:(\|/|)man:*' fzf-preview 'man $word'
now, they can work, because `(\\|*/|)man` can match them.