add fzf history keybindings for zsh

This commit is contained in:
Daiderd Jordan 2017-01-30 23:04:29 +01:00
parent 0c9710c375
commit 8cdebe9fa9
No known key found for this signature in database
GPG key ID: D02435D05B810C96
4 changed files with 45 additions and 52 deletions

View file

@ -26,9 +26,7 @@ in
programs.zsh.enable = mkOption {
type = types.bool;
default = false;
description = ''
Whether to configure zsh as an interactive shell.
'';
description = "Whether to configure zsh as an interactive shell.";
};
programs.zsh.variables = mkOption {
@ -47,49 +45,43 @@ in
programs.zsh.shellInit = mkOption {
type = types.lines;
default = "";
description = ''
Shell script code called during zsh shell initialisation.
'';
description = "Shell script code called during zsh shell initialisation.";
};
programs.zsh.loginShellInit = mkOption {
type = types.lines;
default = "";
description = ''
Shell script code called during zsh login shell initialisation.
'';
description = "Shell script code called during zsh login shell initialisation.";
};
programs.zsh.interactiveShellInit = mkOption {
type = types.lines;
default = "";
description = ''
Shell script code called during interactive zsh shell initialisation.
'';
description = "Shell script code called during interactive zsh shell initialisation.";
};
programs.zsh.promptInit = mkOption {
type = types.lines;
default = "autoload -U promptinit && promptinit && prompt walters";
description = ''
Shell script code used to initialise the zsh prompt.
'';
description = "Shell script code used to initialise the zsh prompt.";
};
programs.zsh.enableCompletion = mkOption {
type = types.bool;
default = true;
description = ''
Enable zsh completion for all interactive zsh shells.
'';
description = "Enable zsh completion for all interactive zsh shells.";
};
programs.zsh.enableBashCompletion = mkOption {
type = types.bool;
default = true;
description = ''
Enable bash completion for all interactive zsh shells.
'';
description = "Enable bash completion for all interactive zsh shells.";
};
programs.zsh.enableFzfHistory = mkOption {
type = types.bool;
default = true;
description = "Enable fzf keybinding for Ctrl-r history search.";
};
};
@ -144,6 +136,7 @@ in
# This file is read for interactive shells.
bindkey -e
${optionalString cfg.enableFzfHistory "source ${./fzf-history.zsh}"}
# Only execute this file once per shell.
if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi

View file

@ -0,0 +1,28 @@
__fzf_use_tmux__() {
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
}
__fzfcmd() {
__fzf_use_tmux__ &&
echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf"
}
# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected num
setopt localoptions noglobsubst pipefail 2> /dev/null
selected=( $(fc -l 1 |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS +s --tac -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --query=${(q)LBUFFER} +m" $(__fzfcmd)) )
local ret=$?
if [ -n "$selected" ]; then
num=$selected[1]
if [ -n "$num" ]; then
zle vi-fetch-history -n $num
fi
fi
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget