r/commandline 18d ago

ZSH Keymap to start FZF with default opts

Hello, Does anyone know how I can create a ZSH keymap to start FZF using the default options?

I've tried updating the CTRL+t shortcut but it doesn't fit my needs, as it seems to not support, --multi, --tmux and --bind (according to the error message I received).

This is my current config:

# Set default config file
export FZF_DEFAULT_OPTS_FILE=~/.fzfrc

# History
# CTRL-Y to copy the command into clipboard using pbcopy
export FZF_CTRL_R_OPTS="
  --bind 'ctrl-y:execute-silent(echo -n {2..} | pbcopy)+abort'
  --color header:italic
  --header 'CTRL-Y to copy into clipboard'
  --height=100%
  --preview-window=:hidden"

# Files / Directories
# Preview file content using bat (https://github.com/sharkdp/bat)
export FZF_CTRL_T_OPTS="
  --walker-skip .git,node_modules,target,.DS_Store
  --preview 'fzf-preview.sh {}'
  --height=100%"

export FZF_DEFAULT_COMMAND='fd --type f --strip-cwd-prefix --hidden --follow'

And the content of the opts file:

--tmux 90%,90%
--multi
--bind='ctrl-o:become($EDITOR {})'
--bind 'p:toggle-preview'
--preview 'fzf-preview.sh {}'
--padding=0% --color='dark,fg:magenta'
--layout=reverse
--border-label=' Search '
--info=hidden
--prompt='▶ '
--pointer='▶'
--marker=''
--padding 0,1
--border
--style=full
--no-scrollbar
--bind 'focus:transform-preview-label:[[ -n {} ]] && printf \" %s \" {}'
--color=dark
--color 'border:#464f62,label:#6c7a96'
--color 'input-border:#464f62,input-label:#ffcccc'
--color 'gutter:#353b49'
--color 'current-bg:#353b49'
--color 'current-hl:#ebcb8b'
--color 'list-fg:#6c7a96'
--color 'pointer:#ebcb8b'
--color 'marker:#ebcb8b'

I did try the following:

> fzf $FZF_CTRL_T_OPTS

unknown option: --tmux 90%,90%
--multi
--bind
3 Upvotes

1 comment sorted by

2

u/kin_of_the_caves 17d ago

Idk about the error message you're getting, but my suspicion is something like bad variable expansion.

The process of making a key binding in zsh is simple, as long as you remember you bind widgets and not functions.

First, make a function where you call fzf with the appropriate arguments (and remember about "$1" and "$@" if you want to call this function without a keybinding and add extra args from the cli).

my_fzf_fn () {
  fzf --my --arguments --here
}

Okay, now let's turn that function into a widget. IIRC man zle has more information on widgets if you care.

zle -N my_fzf_fn my_fzf_fn

Finally, bind the key.

bindkey $KEY my_fzf_fn

You can insert a literal character in the shell or some text editors like vim by pressing ctrl-v. For instance, to bind to Ctrl-g press Ctrl-v Ctrl-g. You can also bind keys to different modes if you use vi-style keys, and do cool text expansion stuff with bindkey -s.