Skip to content
Myr

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

4 files+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]45args=()6opt=""7while [ $# -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 shift31done3233if [ -z "$TMUX_PANE" ]; then34 fzf "${args[@]}"35 exit $?36fi3738set -e3940# Build arguments to fzf41[ ${#args[@]} -gt 0 ] && fzf_args=$(printf '\\"%s\\" ' "${args[@]}"; echo '')4243# Clean up named pipes on exit44id=$RANDOM45fifo1=/tmp/fzf-fifo1-$id46fifo2=/tmp/fzf-fifo2-$id47fifo3=/tmp/fzf-fifo3-$id48cleanup() {49 rm -f $fifo1 $fifo2 $fifo350}51trap cleanup EXIT SIGINT SIGTERM5253fail() {54 >&2 echo "$1"55 exit 156}57fzf=$(which fzf 2> /dev/null) || fail "fzf executable not found"5859mkfifo $fifo260mkfifo $fifo361if [ -t 0 ]; then62 tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' > '$fifo2'; echo \$? > '$fifo3'"'63else64 mkfifo $fifo165 tmux split-window $opt 'sh -c "'$fzf' '"$fzf_args"' < '$fifo1' > '$fifo2'; echo \$? > '$fifo3'"'66 cat <&0 > $fifo1 &67fi68cat $fifo269[ "$(cat $fifo3)" = '0' ]70
modified:plugin/fzf.vim+16 added-9 removed
31 unmodified lines
32set cpo&vim3334function! 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 endif57endfunction
201 unmodified lines
modified:test/test_go.rb+2 added-2 removed
136 unmodified lines
137 self.until { |lines| lines.empty? }138 end139private140 def defaults opts141 { timeout: 5, pane: 0 }.merge(opts)141 { timeout: 10, pane: 0 }.merge(opts)142 end143144 def wait opts = {}145 timeout, pane = defaults(opts).values_at(:timeout, :pane)
62 unmodified lines
208 end209210 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 lines
modified:README.md+56 added-123 removed
12 unmodified lines
1314fzf project consists of the followings:1516- `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)2122You can [download fzf executable][bin] alone, but it's recommended that you23install the extra stuff using the attached install script.2425[bin]: https://github.com/junegunn/fzf-bin/releases2626### Using git (recommended)27#### Using git (recommended)2829Clone this repository and run30[install](https://github.com/junegunn/fzf/blob/master/install) script.31
1 unmodified line
33git clone https://github.com/junegunn/fzf.git ~/.fzf34~/.fzf/install35```3636### Using curl37#### Using curl3839In case you don't have git installed:4041```sh
2 unmodified lines
44 tar xz --strip-components 1 -C ~/.fzf45~/.fzf/install46```4747### Using Homebrew48#### Using Homebrew4950On OS X, you can use [Homebrew](http://brew.sh/) to install fzf.5152```sh
2 unmodified lines
55# Install shell extensions - this should be done whenever fzf is updated56/usr/local/Cellar/fzf/$(fzf --version)/install57```5858### Install as Vim plugin59#### Install as Vim plugin6061Once you have cloned the repository, add the following line to your .vimrc.6263```vim64set rtp+=~/.fzf65```6666Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf:67Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf68(recommended):6970```vim71Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' }72```7374Usage75-----7675```76usage: fzf [options]7778 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)8889 Search result90 +s, --no-sort Do not sort the result91 --tac Reverse the order of the input92 (e.g. 'history | fzf --tac --no-sort')9394 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: '> ')102103 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')111112 Environment variables113 FZF_DEFAULT_COMMAND Default command to use when input is tty114 FZF_DEFAULT_OPTS Defaults options. (e.g. '-x -m')115```11677fzf will launch curses-based finder, read the list from STDIN, and write the78selected item to STDOUT.7980```sh
7 unmodified lines
88```sh89vim $(fzf)90```91132### Keys92#### Using the finder93134Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press135enter 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 mode100137The following readline key bindings should also work as expected.101#### Extended-search mode102139- CTRL-A / CTRL-E140- CTRL-B / CTRL-F141- CTRL-H / CTRL-D142- CTRL-W / CTRL-U / CTRL-Y143- ALT-B / ALT-F144145If you enable multi-select mode with `-m` option, you can select multiple items146with TAB or Shift-TAB key.147148You can also use mouse. Double-click on an item to select it or shift-click (or149ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and150down.151152### Extended-search mode153103With `-x` or `--extended` option, fzf will start in "extended-search mode".104105In this mode, you can specify multiple patterns delimited by spaces,106such as: `^music .mp3$ sbtrkt !rmx`
9 unmodified lines
116117If you don't need fuzzy matching and do not wish to "quote" every word, start118fzf with `-e` or `--extended-exact` option.119171Useful examples172---------------120Examples121--------122174```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)178fe() {179 local file180 file=$(fzf --query="$1" --select-1 --exit-0)181 [ -n "$file" ] && ${EDITOR:-vim} "$file"182}123Many useful examples can be found on [the wiki124page](https://github.com/junegunn/fzf/wiki/examples). Feel free to add your125own as well.126184# fd - cd to selected directory185fd() {186 local dir187 dir=$(find ${1:-*} -path '*/\.*' -prune \188 -o -type d -print 2> /dev/null | fzf +m) &&189 cd "$dir"190}191192# fh - repeat history193fh() {194 eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//')195}196197# fkill - kill process198fkill() {199 ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}200}201```202203For more examples, see [the wiki204page](https://github.com/junegunn/fzf/wiki/examples).205127Key bindings for command line128-----------------------------129130The install script will setup the following key bindings for bash, zsh, and
14 unmodified lines
145If you want to customize the key bindings, consider editing the146installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and147`~/.config/fish/functions/fzf_key_bindings.fish`.148228Auto-completion229---------------149`fzf-tmux` script150-----------------151231Disclaimer: *Auto-completion feature is currently experimental, it can change232over time*152[fzf-tmux](bin/fzf-tmux) is a bash script that opens fzf in a tmux pane.153234### bash154```sh155# usage: fzf-tmux [-h HEIGHT[%]] [-w WIDTH[%]] [--] [FZF OPTIONS]156157# select git branches in horizontal split (15 lines)158git branch | fzf-tmux -h 15159160# select multiple words in vertical split (20% of screen width)161cat /usr/share/dict/words | fzf-tmux -w 20% --multi162```163164It 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.166167Fuzzy completion for bash168-------------------------169170#### Files and directories171172Fuzzy completion for files and directories can be triggered if the word before173the cursor ends with the trigger sequence which is by default `**`.
59 unmodified lines
233# Options to fzf command234export FZF_COMPLETION_OPTS='+c -x'235```236303### zsh304305TODO :smiley:306307(Pull requests are appreciated.)308237Usage as Vim plugin238-------------------239240(Note: To use fzf in GVim, an external terminal emulator is required.)241314### `:FZF[!]`242#### `:FZF[!]`243244If you have set up fzf for Vim, `:FZF` command will be added.245246```vim
29 unmodified lines
276Refer to the [this wiki277page](https://github.com/junegunn/fzf/wiki/fzf-with-MacVim-and-iTerm2) to see278how to set up.279352### `fzf#run([options])`280#### `fzf#run([options])`281282For more advanced uses, you can call `fzf#run()` function which returns the list283of the selected items.284
10 unmodified lines
295| `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) |298371#### Examples299##### Examples300301If `sink` option is not given, `fzf#run` will simply return the list.302303```vim
46 unmodified lines
350\ 'tmux_height': '40%'351\ })<CR>352```353426### Articles354More examples can be found on [the wiki355page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).356357#### Articles358359- [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)360361Tips362----363433### Rendering issues364#### Rendering issues365366If you have any rendering issues, check the followings:3673681. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
5 unmodified lines
374 option. And if it solves your problem, I recommend including it in375 `FZF_DEFAULT_OPTS` for further convenience.3764. If you still have problem, try `--no-256` option or even `--no-color`.377447### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`378#### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`379380[ag](https://github.com/ggreer/the_silver_searcher) or381[pt](https://github.com/monochromegane/the_platinum_searcher) will do the382filtering:
8 unmodified lines
391# Now fzf (w/o pipe) will use ag instead of find392fzf393```394464### `git ls-tree` for fast traversal395#### `git ls-tree` for fast traversal396397If you're running fzf in a large git repository, `git ls-tree` can boost up the398speed of the traversal.399
11 unmodified lines
411 fi412}413```414484### Using fzf with tmux splits415#### Using fzf with tmux panes416486It isn't too hard to write your own fzf-tmux combo like the default487CTRL-T key binding. (Or is it?)417The supplied [fzf-tmux](bin/fzf-tmux) script should suffice in most of the418cases, 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. The420following example will show you how it can be done.421422```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 of
21 unmodified lines
446# Bind CTRL-X-CTRL-D to fzf_tmux_dir447bind '"\C-x\C-d": "$(fzf_tmux_dir)\e\C-e"'448```449517### Fish shell450#### Fish shell451452It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)453that it doesn't allow reading from STDIN in command substitution, which means454simple `vim (fzf)` won't work as expected. The workaround is to store the result
14 unmodified lines
469 end470end471```472540### Handling UTF-8 NFD paths on OSX473#### Handling UTF-8 NFD paths on OSX474475Use iconv to convert NFD paths to NFC:476477```sh
12 unmodified lines