Merge pull request #136 from junegunn/fzf-tmux
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add fzf-tmux script
authored by Junegunn Choi · committed by Junegunn Choi · parents 5f3326a, 87c71a3 · Browse files · 4 files·+144 added−134 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+144 added−134 removed
- tests:+2 added−2 removed
- other:+142 added−132 removed
Provenance
- Commit
6728870- Tree
990b0b6- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent 1
5f3326a- Parent 2
87c71a3
4 files changed+144 added−134 removed
added:bin/fzf-tmux+70 added
1+#!/usr/bin/env bash2+# fzf-tmux: starts fzf in a tmux pane3+# usage: fzf-tmux [-h HEIGHT[%]] [-w WIDTH[%]] [--] [FZF OPTIONS]4+5+args=()6+opt=""7+while [ $# -gt 0 ]; do8+ arg="$1"9+ case "$arg" in10+ -w*|-h*)11+ if [ ${#arg} -gt 2 ]; then12+ size="${arg:2}"13+ else14+ shift15+ size="$1"16+ fi17+ [[ "$arg" =~ ^-w ]] && opt="-h"18+ [[ "$size" =~ %$ ]] && opt="$opt -p ${size:0:-1}" ||19+ opt="$opt -l $size"20+ ;;21+ --)22+ # "--" can be used to separate fzf-tmux options from fzf options to23+ # avoid conflicts24+ break25+ ;;26+ *)27+ args+=("$1")28+ ;;29+ esac30+ shift31+done32+33+if [ -z "$TMUX_PANE" ]; then34+ fzf "${args[@]}"35+ exit $?36+fi37+38+set -e39+40+# Build arguments to fzf41+[ ${#args[@]} -gt 0 ] && fzf_args=$(printf '\\"%s\\" ' "${args[@]}"; echo '')42+43+# Clean up named pipes on exit44+id=$RANDOM45+fifo1=/tmp/fzf-fifo1-$id46+fifo2=/tmp/fzf-fifo2-$id47+fifo3=/tmp/fzf-fifo3-$id48+cleanup() {49+ rm -f $fifo1 $fifo2 $fifo350+}51+trap cleanup EXIT SIGINT SIGTERM52+53+fail() {54+ >&2 echo "$1"55+ exit 156+}57+fzf=$(which fzf 2> /dev/null) || fail "fzf executable not found"58+59+mkfifo $fifo260+mkfifo $fifo361+if [ -t 0 ]; then62+ tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3'"'63+else64+ mkfifo $fifo165+ tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3'"'66+ cat <&0 > $fifo1 &67+fi68+cat $fifo269+[ "$(cat $fifo3)" = '0' ]70+modified:plugin/fzf.vim+16 added-9 removed
31 unmodified lines32 set cpo&vim33 34 function! s:fzf_exec()35 if !exists('s:exec')36- call system('type fzf')37- if v:shell_error38- let s:exec = executable(s:fzf_go) ?39- \ s:fzf_go : (executable(s:fzf_rb) ? s:fzf_rb : '')36+ if executable(s:fzf_go)37+ let s:exec = s:fzf_go38 else39+ let path = split(system('which fzf 2> /dev/null'), '\n')40+ if !v:shell_error && !empty(path)41+ let s:exec = path[0]42+ elseif executable(s:fzf_rb)43+ let s:exec = s:fzf_rb44+ else45+ call system('type fzf')46+ if v:shell_error47+ throw 'fzf executable not found'48+ else41- let s:exec = 'fzf'49+ let s:exec = 'fzf'50+ endif51+ endif52 endif43- return s:fzf_exec()44- elseif empty(s:exec)45- unlet s:exec46- throw 'fzf executable not found'53+ return s:exec54 else55 return s:exec56 endif57 endfunction201 unmodified linesmodified:test/test_go.rb+2 added-2 removed
136 unmodified lines137 self.until { |lines| lines.empty? }138 end139 private140 def defaults opts141- { timeout: 5, pane: 0 }.merge(opts)141+ { timeout: 10, pane: 0 }.merge(opts)142 end143 144 def wait opts = {}145 timeout, pane = defaults(opts).values_at(:timeout, :pane)62 unmodified lines208 end209 210 def test_vanilla211 tmux.send_keys "seq 1 100000 | #{fzf}", :Enter212- tmux.until(timeout: 10) { |lines|212+ tmux.until(timeout: 20) { |lines|213 lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }214 lines = tmux.capture215 assert_equal ' 2', lines[-4]216 assert_equal '> 1', lines[-3]361 unmodified linesmodified:README.md+56 added-123 removed
12 unmodified lines13 14 fzf project consists of the followings:15 16 - `fzf` executable17+- `fzf-tmux` script for launching fzf in a tmux pane18 - Shell extensions19 - Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)19- - Fuzzy auto-completion (bash)20+ - Fuzzy auto-completion (bash only)21 22 You can [download fzf executable][bin] alone, but it's recommended that you23 install the extra stuff using the attached install script.24 25 [bin]: https://github.com/junegunn/fzf-bin/releases26 26-### Using git (recommended)27+#### Using git (recommended)28 29 Clone this repository and run30 [install](https://github.com/junegunn/fzf/blob/master/install) script.31 1 unmodified line33 git clone https://github.com/junegunn/fzf.git ~/.fzf34 ~/.fzf/install35 ```36 36-### Using curl37+#### Using curl38 39 In case you don't have git installed:40 41 ```sh2 unmodified lines44 tar xz --strip-components 1 -C ~/.fzf45 ~/.fzf/install46 ```47 47-### Using Homebrew48+#### Using Homebrew49 50 On OS X, you can use [Homebrew](http://brew.sh/) to install fzf.51 52 ```sh2 unmodified lines55 # Install shell extensions - this should be done whenever fzf is updated56 /usr/local/Cellar/fzf/$(fzf --version)/install57 ```58 58-### Install as Vim plugin59+#### Install as Vim plugin60 61 Once you have cloned the repository, add the following line to your .vimrc.62 63 ```vim64 set rtp+=~/.fzf65 ```66 66-Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf:67+Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf68+(recommended):69 70 ```vim71 Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' }72 ```73 74 Usage75 -----76 75-```76-usage: fzf [options]77-78- Search mode79- -x, --extended Extended-search mode80- -e, --extended-exact Extended-search mode (exact match)81- -i Case-insensitive match (default: smart-case match)82- +i Case-sensitive match83- -n, --nth=N[,..] Comma-separated list of field index expressions84- for limiting search scope. Each can be a non-zero85- integer or a range expression ([BEGIN]..[END])86- --with-nth=N[,..] Transform the item using index expressions for search87- -d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)88-89- Search result90- +s, --no-sort Do not sort the result91- --tac Reverse the order of the input92- (e.g. 'history | fzf --tac --no-sort')93-94- Interface95- -m, --multi Enable multi-select with tab/shift-tab96- --no-mouse Disable mouse97- +c, --no-color Disable colors98- +2, --no-256 Disable 256-color99- --black Use black background100- --reverse Reverse orientation101- --prompt=STR Input prompt (default: '> ')102-103- Scripting104- -q, --query=STR Start the finder with the given query105- -1, --select-1 Automatically select the only match106- -0, --exit-0 Exit immediately when there's no match107- -f, --filter=STR Filter mode. Do not start interactive finder.108- --print-query Print query as the first line109- --sync Synchronous search for multi-staged filtering110- (e.g. 'fzf --multi | fzf --sync')111-112- Environment variables113- FZF_DEFAULT_COMMAND Default command to use when input is tty114- FZF_DEFAULT_OPTS Defaults options. (e.g. '-x -m')115-```116-77 fzf will launch curses-based finder, read the list from STDIN, and write the78 selected item to STDOUT.79 80 ```sh7 unmodified lines88 ```sh89 vim $(fzf)90 ```91 132-### Keys92+#### Using the finder93 134-Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press135-enter key to select the item. CTRL-C, CTRL-G, or ESC will terminate the finder.94+- `CTRL-J` / `CTRL-K` (or `CTRL-N` / `CTRL-P)` to move cursor up and down95+- `Enter` key to select the item, `CTRL-C` / `CTRL-G` / `ESC` to exit96+- On multi-select mode (`-m`), `TAB` and `Shift-TAB` to mark multiple items97+- Emacs style key bindings98+- Mouse: scroll, click, double-click; shift-click and shift-scroll on99+ multi-select mode100 137-The following readline key bindings should also work as expected.101+#### Extended-search mode102 139-- CTRL-A / CTRL-E140-- CTRL-B / CTRL-F141-- CTRL-H / CTRL-D142-- CTRL-W / CTRL-U / CTRL-Y143-- ALT-B / ALT-F144-145-If you enable multi-select mode with `-m` option, you can select multiple items146-with TAB or Shift-TAB key.147-148-You can also use mouse. Double-click on an item to select it or shift-click (or149-ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and150-down.151-152-### Extended-search mode153-103 With `-x` or `--extended` option, fzf will start in "extended-search mode".104 105 In this mode, you can specify multiple patterns delimited by spaces,106 such as: `^music .mp3$ sbtrkt !rmx`9 unmodified lines116 117 If you don't need fuzzy matching and do not wish to "quote" every word, start118 fzf with `-e` or `--extended-exact` option.119 171-Useful examples172----------------120+Examples121+--------122 174-```sh175-# fe [FUZZY PATTERN] - Open the selected file with the default editor176-# - Bypass fuzzy finder if there's only one match (--select-1)177-# - Exit if there's no match (--exit-0)178-fe() {179- local file180- file=$(fzf --query="$1" --select-1 --exit-0)181- [ -n "$file" ] && ${EDITOR:-vim} "$file"182-}123+Many useful examples can be found on [the wiki124+page](https://github.com/junegunn/fzf/wiki/examples). Feel free to add your125+own as well.126 184-# fd - cd to selected directory185-fd() {186- local dir187- dir=$(find ${1:-*} -path '*/\.*' -prune \188- -o -type d -print 2> /dev/null | fzf +m) &&189- cd "$dir"190-}191-192-# fh - repeat history193-fh() {194- eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//')195-}196-197-# fkill - kill process198-fkill() {199- ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}200-}201-```202-203-For more examples, see [the wiki204-page](https://github.com/junegunn/fzf/wiki/examples).205-127 Key bindings for command line128 -----------------------------129 130 The install script will setup the following key bindings for bash, zsh, and14 unmodified lines145 If you want to customize the key bindings, consider editing the146 installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and147 `~/.config/fish/functions/fzf_key_bindings.fish`.148 228-Auto-completion229----------------149+`fzf-tmux` script150+-----------------151 231-Disclaimer: *Auto-completion feature is currently experimental, it can change232-over time*152+[fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.153 234-### bash154+```sh155+# usage: fzf-tmux [-h HEIGHT[%]] [-w WIDTH[%]] [--] [FZF OPTIONS]156 157+# select git branches in horizontal split (15 lines)158+git branch | fzf-tmux -h 15159+160+# select multiple words in vertical split (20% of screen width)161+cat /usr/share/dict/words | fzf-tmux -w 20% --multi162+```163+164+It will still work even when you're not on tmux, silently ignoring `-h` and165+`-w` options, so you can invariably use `fzf-tmux` in your scripts.166+167+Fuzzy completion for bash168+-------------------------169+170 #### Files and directories171 172 Fuzzy completion for files and directories can be triggered if the word before173 the cursor ends with the trigger sequence which is by default `**`.59 unmodified lines233 # Options to fzf command234 export FZF_COMPLETION_OPTS='+c -x'235 ```236 303-### zsh304-305-TODO :smiley:306-307-(Pull requests are appreciated.)308-237 Usage as Vim plugin238 -------------------239 240 (Note: To use fzf in GVim, an external terminal emulator is required.)241 314-### `:FZF[!]`242+#### `:FZF[!]`243 244 If you have set up fzf for Vim, `:FZF` command will be added.245 246 ```vim29 unmodified lines276 Refer to the [this wiki277 page](https://github.com/junegunn/fzf/wiki/fzf-with-MacVim-and-iTerm2) to see278 how to set up.279 352-### `fzf#run([options])`280+#### `fzf#run([options])`281 282 For more advanced uses, you can call `fzf#run()` function which returns the list283 of the selected items.284 10 unmodified lines295 | `tmux_width` | number/string | Use tmux vertical split with the given height (e.g. `20`, `50%`) |296 | `tmux_height` | number/string | Use tmux horizontal split with the given height (e.g. `20`, `50%`) |297 | `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |298 371-#### Examples299+##### Examples300 301 If `sink` option is not given, `fzf#run` will simply return the list.302 303 ```vim46 unmodified lines350 \ 'tmux_height': '40%'351 \ })<CR>352 ```353 426-### Articles354+More examples can be found on [the wiki355+page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).356 357+#### Articles358+359 - [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)360 361 Tips362 ----363 433-### Rendering issues364+#### Rendering issues365 366 If you have any rendering issues, check the followings:367 368 1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it5 unmodified lines374 option. And if it solves your problem, I recommend including it in375 `FZF_DEFAULT_OPTS` for further convenience.376 4. If you still have problem, try `--no-256` option or even `--no-color`.377 447-### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`378+#### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`379 380 [ag](https://github.com/ggreer/the_silver_searcher) or381 [pt](https://github.com/monochromegane/the_platinum_searcher) will do the382 filtering:8 unmodified lines391 # Now fzf (w/o pipe) will use ag instead of find392 fzf393 ```394 464-### `git ls-tree` for fast traversal395+#### `git ls-tree` for fast traversal396 397 If you're running fzf in a large git repository, `git ls-tree` can boost up the398 speed of the traversal.399 11 unmodified lines411 fi412 }413 ```414 484-### Using fzf with tmux splits415+#### Using fzf with tmux panes416 486-It isn't too hard to write your own fzf-tmux combo like the default487-CTRL-T key binding. (Or is it?)417+The supplied [fzf-tmux](bin/fzf-tmux) script should suffice in most of the418+cases, but if you want to be able to update command line like the default419+`CTRL-T` key binding, you'll have to use `send-keys` command of tmux. The420+following example will show you how it can be done.421 422 ```sh423 # This is a helper function that splits the current pane to start the given424 # command ($1) and sends its output back to the original pane with any number of21 unmodified lines446 # Bind CTRL-X-CTRL-D to fzf_tmux_dir447 bind '"\C-x\C-d": "$(fzf_tmux_dir)\e\C-e"'448 ```449 517-### Fish shell450+#### Fish shell451 452 It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)453 that it doesn't allow reading from STDIN in command substitution, which means454 simple `vim (fzf)` won't work as expected. The workaround is to store the result14 unmodified lines469 end470 end471 ```472 540-### Handling UTF-8 NFD paths on OSX473+#### Handling UTF-8 NFD paths on OSX474 475 Use iconv to convert NFD paths to NFC:476 477 ```sh12 unmodified lines