[bash] Start C-r search with current command line (#1886)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Restore the original line when search is aborted. Add --query "$READLINE_LINE" and fall back to the current behavior pre Bash 4.
authored by Jack Bates · committed by GitHub · parent 7bf1f2c · Browse files · 3 files·+64 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
Diff stats
3 files+64 added−54 removed
- tests:+25 added−24 removed
- other:+39 added−30 removed
Provenance
- Commit
7c447bb- Tree
ed6dbfc- Author
Jack Bates <jack@nottheoilrig.com>- Committer
GitHub <noreply@github.com>- Parent
7bf1f2c
3 files changed+64 added−54 removed
modified:shell/key-bindings.bash+38 added-29 removed
49 unmodified lines5051525354555654555657585960616258596061626364656667686970716768697071727374757677787980817273747576777879808186878882838485909192868788899091929394959697989910010110210310410510610749 unmodified lines -o -type d -print 2> /dev/null | cut -b3-"}" dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd %q' "$dir"}
__fzf_history__() ( local line line=$(__fzf_history__() { local output output=$( builtin fc -lnr -2147483648 | perl -p -l0 -e 'BEGIN { getc; $/ = "\n\t" } s/^[ *]//; $_ = '"$1"' - $. . "\t$_"' | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --read0" $(__fzfcmd) ) echo "${line#*$'\t'}") last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -p -l0 -e 'BEGIN { getc; $/ = "\n\t"; $HISTCMD = $ENV{last_hist} + 1 } s/^[ *]//; $_ = $HISTCMD - $. . "\t$_"' | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --query $(printf %q "$READLINE_LINE") --read0" $(__fzfcmd) ) || return READLINE_LINE=${output#*$'\t'} if [ -z "$READLINE_POINT" ]; then echo "$READLINE_LINE" else READLINE_POINT=0x7fffffff fi}
# Required to refresh the prompt after fzfbind -m emacs-standard '"\er": redraw-current-line'
# CTRL-T - Paste the selected file path into the command lineif [ $BASH_VERSINFO -gt 3 ]; then bind -m emacs-standard -x '"\C-t": "fzf-file-widget"'elif __fzf_use_tmux__; then bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select_tmux__`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'else bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'fi
# CTRL-R - Paste the selected command from history into the command linebind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u__fzf_history__ $HISTCMD\e\C-e`"\C-a"`\C-e\e\C-e\er"'
# ALT-C - cd into the selected directorybind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'
bind -m vi-command '"\C-z": emacs-editing-mode'bind -m vi-insert '"\C-z": emacs-editing-mode'bind -m emacs-standard '"\C-z": vi-editing-mode'
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then # CTRL-T - Paste the selected file path into the command line if __fzf_use_tmux__; then bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select_tmux__`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"' else bind -m emacs-standard '"\C-t": " \C-b\C-k \C-u`__fzf_select__`\e\C-e\er\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'# CTRL-T - Paste the selected file path into the command linebind -m vi-command '"\C-t": "\C-z\C-t\C-z"'bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"' fi bind -m vi-command '"\C-t": "\C-z\C-t\C-z"' bind -m vi-insert '"\C-t": "\C-z\C-t\C-z"'
# CTRL-R - Paste the selected command from history into the command linebind -m vi-command '"\C-r": "\C-z\C-r\C-z"'bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"' # CTRL-R - Paste the selected command from history into the command line bind -m emacs-standard '"\C-r": "\C-e \C-u\C-y\ey\C-u"$(__fzf_history__)"\e\C-e\er"' bind -m vi-command '"\C-r": "\C-z\C-r\C-z"' bind -m vi-insert '"\C-r": "\C-z\C-r\C-z"'else # CTRL-T - Paste the selected file path into the command line bind -m emacs-standard -x '"\C-t": fzf-file-widget' bind -m vi-command -x '"\C-t": fzf-file-widget' bind -m vi-insert -x '"\C-t": fzf-file-widget'
# CTRL-R - Paste the selected command from history into the command line bind -m emacs-standard -x '"\C-r": __fzf_history__' bind -m vi-command -x '"\C-r": __fzf_history__' bind -m vi-insert -x '"\C-r": __fzf_history__'fi
# ALT-C - cd into the selected directorybind -m emacs-standard '"\ec": " \C-b\C-k \C-u`__fzf_cd__`\e\C-e\er\C-m\C-y\C-h\e \C-y\ey\C-x\C-x\C-d"'bind -m vi-command '"\ec": "\C-z\ec\C-z"'bind -m vi-insert '"\ec": "\C-z\ec\C-z"'
fimodified:test/test_go.rb+25 added-24 removed
82 unmodified lines83 end84 end85 86 class Tmux87- TEMPNAME = '/tmp/fzf-test.txt'88-87 attr_reader :win88 89 def initialize(shell = :bash)90 @win = go("new-window -d -P -F '#I' '#{Shell.send(shell)}'").first35 unmodified lines126 system('tmux', 'setb', str, ';', 'pasteb', '-t', win, ';', 'send-keys', '-t', win, 'Enter')127 end128 129 def capture(pane = 0)132- File.unlink TEMPNAME while File.exist? TEMPNAME133- wait do134- go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME} 2> /dev/null")135- $CHILD_STATUS.exitstatus.zero?136- end137- File.read(TEMPNAME).split($INPUT_RECORD_SEPARATOR).reverse.drop_while(&:empty?).reverse130+ go("capture-pane -p -t #{win}.#{pane}")131 end132 133 def until(refresh = false, pane = 0)134 lines = nil1595 unmodified lines1730 end1731 1732 module TestShell1733 def setup1741- super1734+ @tmux = Tmux.new shell1735+ tmux.prepare1736 end1737 1738 def teardown1739 @tmux.kill108 unmodified lines1848 tmux.send_keys :Enter1849 tmux.until { |lines| lines[-1] == '3rd' }1850 end1851 1852+ def test_ctrl_r_abort1853+ skip "doesn't restore the original line when search is aborted pre Bash 4" if shell == :bash && /(?<= version )\d+/.match(`#{Shell.bash} --version`).to_s.to_i < 41854+ tmux.send_keys 'foo'1855+ tmux.until { |lines| lines[-1].start_with? 'foo' }1856+ tmux.send_keys 'C-r'1857+ tmux.until { |lines| lines[-1].start_with? '>' }1858+ tmux.send_keys 'C-g'1859+ tmux.until { |lines| lines[-1].start_with? 'foo' }1860+ end1861+1862 def retries(times = 3)1863 (times - 1).times do1864 begin1865 return yield181 unmodified lines2047 class TestBash < TestBase2048 include TestShell2049 include CompletionTest2050 2051+ def shell2052+ :bash2053+ end2054+2055 def new_shell2056 tmux.prepare2057 tmux.send_keys "FZF_TMUX=1 #{Shell.bash}", :Enter2058 tmux.prepare2059 end2060 2053- def setup2054- super2055- @tmux = Tmux.new :bash2056- end2057-2061 def test_dynamic_completion_loader2062 tmux.paste 'touch /tmp/foo; _fzf_completion_loader=1'2063 tmux.paste '_completion_loader() { complete -o default fake; }'2064 tmux.paste 'complete -F _fzf_path_completion -o default -o bashdefault fake'14 unmodified lines2079 class TestZsh < TestBase2080 include TestShell2081 include CompletionTest2082 2083+ def shell2084+ :zsh2085+ end2086+2087 def new_shell2088 tmux.send_keys "FZF_TMUX=1 #{Shell.zsh}", :Enter2089 tmux.prepare2090 end2084-2085- def setup2086- super2087- @tmux = Tmux.new :zsh2088- end2091 end2092 2093 class TestFish < TestBase2094 include TestShell2095 2096+ def shell2097+ :fish2098+ end2099+2100 def new_shell2101 tmux.send_keys 'env FZF_TMUX=1 fish', :Enter2102 tmux.send_keys 'function fish_prompt; end; clear', :Enter2103 tmux.until(&:empty?)3 unmodified lines2107 tmux.prepare2108 tmux.send_keys "set -g #{name} '#{val}'", :Enter2109 tmux.prepare2110 end2105-2106- def setup2107- super2108- @tmux = Tmux.new :fish2109- end2111 end2112 2113 __END__2114 # Setup fzf15 unmodified linesmodified:.travis.yml+1 added-1 removed
17 unmodified lines18192021222217 unmodified lines - make test # LC_ALL=C to avoid escape codes in # printf %q $'\355\205\214\354\212\244\355\212\270' on macOS. Bash on # macOS is built without HANDLE_MULTIBYTE? - make install && ./install --all && LC_ALL=C tmux new-session -d && ruby test/test_go.rb - make install && ./install --all && LC_ALL=C tmux new-session -d && ruby test/test_go.rb --verbose