1
1
mirror of https://github.com/LnL7/nix-darwin.git synced 2024-09-19 16:47:29 +03:00

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

@ -43,7 +43,7 @@ let
./modules/programs/nix-script.nix
./modules/programs/tmux.nix
./modules/programs/vim.nix
./modules/programs/zsh.nix
./modules/programs/zsh
];
};

View File

@ -51,7 +51,7 @@
services.nix-daemon.enable = true;
services.nix-daemon.tempDir = "/nix/tmp";
nix.distributedBuilds = true;
# nix.distributedBuilds = true;
nix.extraOptions = "pre-build-hook = ";
nix.binaryCachePublicKeys = [ "cache.daiderd.com-1:R8KOWZ8lDaLojqD+v9dzXAqGn29gEzPTTbr/GIpCTrI=" ];
@ -152,6 +152,7 @@
programs.zsh.enable = true;
programs.zsh.enableBashCompletion = true;
programs.zsh.enableFzfHistory = true;
programs.zsh.variables.cfg = "$HOME/.nixpkgs/darwin-config.nix";
programs.zsh.variables.darwin = "$HOME/.nix-defexpr/darwin";
@ -184,35 +185,6 @@
bindkey '^[[A' up-line-or-beginning-search
zle -N down-line-or-beginning-search
zle -N up-line-or-beginning-search
__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 --toggle-sort=ctrl-r $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
'';
environment.variables.HOMEBREW_CASK_OPTS = "--appdir=/Applications/cask";

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