summaryrefslogtreecommitdiff
path: root/shell/key-bindings.bash
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-03-13 17:44:44 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-03-13 17:44:44 +0900
commit5eef0acea1a479e5c1e2e88990b4e60915ba1bb5 (patch)
tree1c5ae8f1cdf7f6cf850ae296e12b10c1bef9de2e /shell/key-bindings.bash
parentdd6138a6559363871d88c594ab9d170e36959129 (diff)
parent3935aa84d8cc66c0371926c61afe89832f00df10 (diff)
downloadfzf-5eef0acea1a479e5c1e2e88990b4e60915ba1bb5.tar.gz
Merge pull request #145 from junegunn/refactor-shell-ext
Refactor shell extensions
Diffstat (limited to 'shell/key-bindings.bash')
-rw-r--r--shell/key-bindings.bash75
1 files changed, 75 insertions, 0 deletions
diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash
new file mode 100644
index 00000000..b7c79463
--- /dev/null
+++ b/shell/key-bindings.bash
@@ -0,0 +1,75 @@
+# Key bindings
+# ------------
+__fsel() {
+ command find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
+ -o -type f -print \
+ -o -type d -print \
+ -o -type l -print 2> /dev/null | sed 1d | cut -b3- | fzf -m | while read item; do
+ printf '%q ' "$item"
+ done
+ echo
+}
+
+if [[ $- =~ i ]]; then
+
+__fsel_tmux() {
+ local height
+ height=${FZF_TMUX_HEIGHT:-40%}
+ if [[ $height =~ %$ ]]; then
+ height="-p ${height%\%}"
+ else
+ height="-l $height"
+ fi
+ tmux split-window $height "bash -c 'source ~/.fzf.bash; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
+}
+
+__fcd() {
+ local dir
+ dir=$(command find -L ${1:-.} \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
+ -o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m) && printf 'cd %q' "$dir"
+}
+
+__use_tmux=0
+[ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ] && __use_tmux=1
+
+if [ -z "$(set -o | \grep '^vi.*on')" ]; then
+ # Required to refresh the prompt after fzf
+ bind '"\er": redraw-current-line'
+
+ # CTRL-T - Paste the selected file path into the command line
+ if [ $__use_tmux -eq 1 ]; then
+ bind '"\C-t": " \C-u \C-a\C-k$(__fsel_tmux)\e\C-e\C-y\C-a\C-d\C-y\ey\C-h"'
+ else
+ bind '"\C-t": " \C-u \C-a\C-k$(__fsel)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er \C-h"'
+ fi
+
+ # CTRL-R - Paste the selected command from history into the command line
+ bind '"\C-r": " \C-e\C-u$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
+
+ # ALT-C - cd into the selected directory
+ bind '"\ec": " \C-e\C-u$(__fcd)\e\C-e\er\C-m"'
+else
+ bind '"\C-x\C-e": shell-expand-line'
+ bind '"\C-x\C-r": redraw-current-line'
+
+ # CTRL-T - Paste the selected file path into the command line
+ # - FIXME: Selected items are attached to the end regardless of cursor position
+ if [ $__use_tmux -eq 1 ]; then
+ bind '"\C-t": "\e$a \eddi$(__fsel_tmux)\C-x\C-e\e0P$xa"'
+ else
+ bind '"\C-t": "\e$a \eddi$(__fsel)\C-x\C-e\e0Px$a \C-x\C-r\exa "'
+ fi
+ bind -m vi-command '"\C-t": "i\C-t"'
+
+ # CTRL-R - Paste the selected command from history into the command line
+ bind '"\C-r": "\eddi$(HISTTIMEFORMAT= history | fzf +s --tac +m -n2..,.. | sed \"s/ *[0-9]* *//\")\C-x\C-e\e$a\C-x\C-r"'
+ bind -m vi-command '"\C-r": "i\C-r"'
+
+ # ALT-C - cd into the selected directory
+ bind '"\ec": "\eddi$(__fcd)\C-x\C-e\C-x\C-r\C-m"'
+ bind -m vi-command '"\ec": "i\ec"'
+fi
+
+unset __use_tmux
+
+fi