Skip to content

Commit

Permalink
[bash] Fire command directly with CTRL-X in CTRL-R
Browse files Browse the repository at this point in the history
Related junegunn#477
  • Loading branch information
4z3 committed Feb 5, 2019
1 parent 315e568 commit 9676876
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions shell/key-bindings.bash
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,49 @@ __fzf_cd__() {
}

__fzf_history__() (
local line
shopt -u nocaseglob nocasematch
line=$(
edit_key=${FZF_CTRL_R_EDIT_KEY:-enter}
exec_key=${FZF_CTRL_R_EXEC_KEY:-ctrl-x}
if selected=$(
HISTTIMEFORMAT= history |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac --sync -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m" $(__fzfcmd) |
command grep '^ *[0-9]') &&
if [[ $- =~ H ]]; then
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
else
sed 's/^ *\([0-9]*\)\** *//' <<< "$line"
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tac --sync -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS --expect=$edit_key,$exec_key +m" $(__fzfcmd) |
command grep "^\\($exec_key$\\|$edit_key$\\| *[0-9]\\)")
then
key=${selected%%$'\n'*}
line=${selected#*$'\n'}

result=$(
if [[ $- =~ H ]]; then
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
else
sed 's/^ *\([0-9]*\)\** *//' <<< "$line"
fi
)

if [[ "$key" = "$edit_key" ]]; then
result=$result$__fzf_edit_suffix__
fi

echo "$result"
fi
)

__fzf_edit_suffix__=#FZFEDIT

__fzf_rebind_ctrl_x_ctrl_p__() {
if test "${READLINE_LINE: -${#__fzf_edit_suffix__}}" = "$__fzf_edit_suffix__"; then
echo derp1 >&2
bind '"\C-x\C-p": ""'
READLINE_LINE=${READLINE_LINE:0:-${#__fzf_edit_suffix__}}
else
echo derp2 >&2
bind '"\C-x\C-p": accept-line'
fi
}

bind '"\C-x\C-p": ""'
bind -x '"\C-x\C-o": __fzf_rebind_ctrl_x_ctrl_p__'

if [[ ! -o vi ]]; then
# Required to refresh the prompt after fzf
bind '"\er": redraw-current-line'
Expand All @@ -80,7 +110,7 @@ if [[ ! -o vi ]]; then
fi

# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^"'
bind '"\C-r": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^\C-x\C-o\C-x\C-p"'

# ALT-C - cd into the selected directory
bind '"\ec": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"'
Expand Down Expand Up @@ -110,7 +140,7 @@ else
bind -m vi-command '"\C-t": "i\C-t"'

# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"'
bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a\C-x\C-o\C-x\C-p"'
bind -m vi-command '"\C-r": "i\C-r"'

# ALT-C - cd into the selected directory
Expand Down

0 comments on commit 9676876

Please sign in to comment.