mirror of
https://github.com/junegunn/fzf.git
synced 2025-07-27 18:21:59 -07:00
Compare commits
266 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
80819f3c44 | ||
|
7571baadb4 | ||
|
da03a66e69 | ||
|
3c47b7fa5f | ||
|
ba9365c438 | ||
|
db37e67575 | ||
|
76a3ef8c37 | ||
|
6fd6fff3a6 | ||
|
d1387bf512 | ||
|
4c923a2d19 | ||
|
4ee85f11e8 | ||
|
829c7f909c | ||
|
990fa00660 | ||
|
77592825f0 | ||
|
ce53b9b2a5 | ||
|
175fe158ed | ||
|
80efafcceb | ||
|
b241409e4b | ||
|
11967be017 | ||
|
6ee811ea03 | ||
|
d5e7303a25 | ||
|
2924fd3e23 | ||
|
75b44aac13 | ||
|
86c73105ee | ||
|
2d00abc7cb | ||
|
1e07b3b1c2 | ||
|
4313c1c25c | ||
|
cc9938d4c9 | ||
|
a54784cd53 | ||
|
22989b0488 | ||
|
892aa1e78b | ||
|
b9ab7d2413 | ||
|
69b2a0a733 | ||
|
13cd4ed546 | ||
|
7261d3afcd | ||
|
84fc73ad9c | ||
|
4103f5c3cc | ||
|
5390616694 | ||
|
daf08f801f | ||
|
4e2a1fe5c8 | ||
|
03f155484c | ||
|
89298a8d23 | ||
|
3b14c5230c | ||
|
91401514ab | ||
|
91d986b6c0 | ||
|
4d72bd098a | ||
|
502973ff75 | ||
|
3e91c189ae | ||
|
b0f80b686c | ||
|
b824928b0b | ||
|
ccca34f9f7 | ||
|
b5350b24ff | ||
|
56ace10a37 | ||
|
72ec0a3408 | ||
|
05118cc440 | ||
|
e392da20e8 | ||
|
6e69339f6b | ||
|
30cdc06bcd | ||
|
9ce43d46f6 | ||
|
de09656197 | ||
|
3827a1b09e | ||
|
61ba8d5a11 | ||
|
4a3a5ee70d | ||
|
f58a53a001 | ||
|
65c1b53275 | ||
|
0b43f988c7 | ||
|
f8e357fa19 | ||
|
c3a4e4cd23 | ||
|
9dac12cb32 | ||
|
d76a3646b7 | ||
|
d7c734acd6 | ||
|
ed13fc8618 | ||
|
edcd7c6aa6 | ||
|
b0fdd6db99 | ||
|
edf27f47f2 | ||
|
3b218b77eb | ||
|
1e02471940 | ||
|
1b9dadb3d3 | ||
|
c3827dea10 | ||
|
6a1b916598 | ||
|
a2c7b001d5 | ||
|
3c6e938bb1 | ||
|
5a0afc5fea | ||
|
f37be006c3 | ||
|
459c332351 | ||
|
153a87d84a | ||
|
05da892cd2 | ||
|
f6b1a6278f | ||
|
db58182483 | ||
|
6e9f0882da | ||
|
7ed18579dc | ||
|
f250fc8f86 | ||
|
6eea9603c2 | ||
|
20915529b7 | ||
|
b3efccca81 | ||
|
809d465de5 | ||
|
7d15071c63 | ||
|
89eb1575e7 | ||
|
5d6ed935a4 | ||
|
0528435386 | ||
|
fe22213b51 | ||
|
aab42eaaba | ||
|
16031b0d54 | ||
|
ded184daaf | ||
|
ecf90bd25b | ||
|
d82e38adc1 | ||
|
af677e7e35 | ||
|
6ad38bdad3 | ||
|
8b80136a87 | ||
|
97de919152 | ||
|
0eafa725b9 | ||
|
fa212efe5f | ||
|
a9056ce90c | ||
|
16682a3f92 | ||
|
02c01c81a0 | ||
|
22d3929ae3 | ||
|
ab9fbf1967 | ||
|
608ec2b806 | ||
|
e5ae4f0ef6 | ||
|
67ba87d390 | ||
|
77d45cb173 | ||
|
d83febea46 | ||
|
546a315884 | ||
|
af616457e3 | ||
|
1a100a2919 | ||
|
a85bb93b69 | ||
|
057eda060c | ||
|
48f9ee6763 | ||
|
52b74abb99 | ||
|
ec4b8a59fa | ||
|
cf8dbf8047 | ||
|
995d380200 | ||
|
ae86cdf09a | ||
|
2b346659a0 | ||
|
49081711a9 | ||
|
e7439ce193 | ||
|
b8e438b6be | ||
|
678e950b6d | ||
|
9ea651f1cd | ||
|
bd98a08b89 | ||
|
f02bb4fdac | ||
|
0a8352a5cd | ||
|
737423995d | ||
|
2916bf7ee4 | ||
|
fa54c5d9b0 | ||
|
693b6651b4 | ||
|
5c71ecb267 | ||
|
1ba50eba98 | ||
|
2c8a256b13 | ||
|
f4c5aa03d7 | ||
|
c6acb2a639 | ||
|
2296013174 | ||
|
8a3e8c2d81 | ||
|
ae84d8c7a4 | ||
|
dbd627c38a | ||
|
d172c3ce03 | ||
|
9904f5354e | ||
|
f345bf7983 | ||
|
875f9b6534 | ||
|
871dfb709d | ||
|
19e24bd644 | ||
|
457a240457 | ||
|
bbf4567dd8 | ||
|
27d3b52843 | ||
|
dcb4694ec1 | ||
|
2fb8ae010f | ||
|
65ae6cabb5 | ||
|
86a66da04d | ||
|
d66b02b0cd | ||
|
b3182c3304 | ||
|
2dbca00bfb | ||
|
b22fd6de6d | ||
|
245ee42763 | ||
|
98bef4600c | ||
|
f5d53b94fe | ||
|
00c8a68430 | ||
|
c1be834ff9 | ||
|
2c0dc2f3b1 | ||
|
1c94fef720 | ||
|
b711d76b8e | ||
|
4396ab7548 | ||
|
2b8c2b9f2a | ||
|
426284c87e | ||
|
089691faaf | ||
|
301290663d | ||
|
1155da7e1c | ||
|
eca0a99fb4 | ||
|
96215c4619 | ||
|
b2d2be55ef | ||
|
7280e8ebc2 | ||
|
c7e86ad4f1 | ||
|
f2b2c022be | ||
|
7747daa9ec | ||
|
c2943e7681 | ||
|
d5fc03d867 | ||
|
b0eca20dc2 | ||
|
aad335475c | ||
|
c3676bf986 | ||
|
6fb4b6d097 | ||
|
6aa168833b | ||
|
0d83cae2ec | ||
|
773d9976a0 | ||
|
3723829b0a | ||
|
13cb198b5c | ||
|
79f645aa6c | ||
|
42d479d071 | ||
|
d7f50b1e41 | ||
|
39eb85596c | ||
|
bff7e9edf5 | ||
|
98ccc03a21 | ||
|
3b668ed448 | ||
|
33b28be941 | ||
|
76fe23b928 | ||
|
622c54f4a3 | ||
|
e09993f919 | ||
|
7ee6fd1f6d | ||
|
2dca6f0cb2 | ||
|
159dd7f069 | ||
|
b30f21e074 | ||
|
636c86cf6f | ||
|
5483e41b2a | ||
|
1c89994c94 | ||
|
e1bc4b983e | ||
|
cb3645ea95 | ||
|
04ebaddf5e | ||
|
45e1f1ae57 | ||
|
c1d5f7cef7 | ||
|
df663c4e41 | ||
|
d3742782f3 | ||
|
faff17b2a9 | ||
|
9a3cddc92e | ||
|
bd2763d863 | ||
|
b2bb22d883 | ||
|
ad8ec7f387 | ||
|
cf0ca8578c | ||
|
07aee79bd8 | ||
|
344b57fe33 | ||
|
18a2fbf54a | ||
|
39af56cf8f | ||
|
2d3a0a1034 | ||
|
655fa5d9aa | ||
|
9a49a29c7f | ||
|
89ae45cda4 | ||
|
f660ad35b2 | ||
|
c61738ae43 | ||
|
c4dec4d34b | ||
|
a797604255 | ||
|
25840d3bc7 | ||
|
4745d50931 | ||
|
04bf3abe99 | ||
|
57f7963eee | ||
|
2fa21e5dd6 | ||
|
9c4c37aa36 | ||
|
2540c9062f | ||
|
f28274109f | ||
|
724724bd8c | ||
|
64541cb5f8 | ||
|
179b00ed6c | ||
|
a9fd496691 | ||
|
b14c57e656 | ||
|
fa5617e076 | ||
|
e52a1d5fad | ||
|
423e26b0c9 | ||
|
84921df0e3 | ||
|
6a5e1de6f3 | ||
|
90adda73b0 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
pkg
|
||||
Gemfile.lock
|
||||
.DS_Store
|
||||
|
9
.travis.yml
Normal file
9
.travis.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
language: ruby
|
||||
rvm:
|
||||
- "1.8.7"
|
||||
- "1.9.3"
|
||||
- "2.0.0"
|
||||
- "2.1.1"
|
||||
|
||||
install: gem install curses minitest
|
||||
|
557
README.md
557
README.md
@@ -16,54 +16,47 @@ fzf requires Ruby (>= 1.8.5).
|
||||
Installation
|
||||
------------
|
||||
|
||||
Download [fzf executable](https://raw.github.com/junegunn/fzf/master/fzf) and
|
||||
put it somewhere in your search $PATH.
|
||||
|
||||
```sh
|
||||
mkdir -p ~/bin
|
||||
wget https://raw.github.com/junegunn/fzf/master/fzf -O ~/bin/fzf
|
||||
chmod +x ~/bin/fzf
|
||||
```
|
||||
|
||||
Or you can just clone this repository and run
|
||||
Clone this repository and run
|
||||
[install](https://github.com/junegunn/fzf/blob/master/install) script.
|
||||
|
||||
```sh
|
||||
git clone https://github.com/junegunn/fzf.git
|
||||
fzf/install
|
||||
git clone https://github.com/junegunn/fzf.git ~/.fzf
|
||||
~/.fzf/install
|
||||
```
|
||||
|
||||
Make sure that ~/bin is included in $PATH.
|
||||
In case you don't have git installed:
|
||||
|
||||
```sh
|
||||
export PATH=$PATH:~/bin
|
||||
mkdir -p ~/.fzf
|
||||
curl -L https://github.com/junegunn/fzf/archive/master.tar.gz |
|
||||
tar xz --strip-components 1 -C ~/.fzf
|
||||
~/.fzf/install
|
||||
```
|
||||
|
||||
### Install as Ruby gem
|
||||
The script will setup:
|
||||
|
||||
fzf can be installed as a Ruby gem
|
||||
- `fzf` function (bash, zsh, fish)
|
||||
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)
|
||||
- Fuzzy auto-completion (bash)
|
||||
|
||||
```
|
||||
gem install fzf
|
||||
```
|
||||
|
||||
It's a bit easier to install and update the script but the Ruby gem version
|
||||
takes slightly longer to start.
|
||||
If you don't use any of the aforementioned shells, you have to manually place
|
||||
fzf executable in a directory included in `$PATH`. Key bindings and
|
||||
auto-completion will not be available in that case.
|
||||
|
||||
### Install as Vim plugin
|
||||
|
||||
You can use any Vim plugin manager to install fzf for Vim. If you don't use one,
|
||||
I recommend you try [vim-plug](https://github.com/junegunn/vim-plug).
|
||||
Once you have cloned the repository, add the following line to your .vimrc.
|
||||
|
||||
1. [Install vim-plug](https://github.com/junegunn/vim-plug#usage)
|
||||
2. Edit your .vimrc
|
||||
```vim
|
||||
set rtp+=~/.fzf
|
||||
```
|
||||
|
||||
call plug#begin()
|
||||
Plug 'junegunn/fzf'
|
||||
" ...
|
||||
call plug#end()
|
||||
Or you may use [vim-plug](https://github.com/junegunn/vim-plug) to manage fzf
|
||||
inside Vim:
|
||||
|
||||
3. Run `:PlugInstall`
|
||||
```vim
|
||||
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' }
|
||||
```
|
||||
|
||||
Usage
|
||||
-----
|
||||
@@ -71,12 +64,40 @@ Usage
|
||||
```
|
||||
usage: fzf [options]
|
||||
|
||||
-m, --multi Enable multi-select
|
||||
-x, --extended Extended-search mode
|
||||
-s, --sort=MAX Maximum number of matched items to sort. Default: 1000
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
+i Case-sensitive match
|
||||
+c, --no-color Disable colors
|
||||
Search
|
||||
-x, --extended Extended-search mode
|
||||
-e, --extended-exact Extended-search mode (exact match)
|
||||
-i Case-insensitive match (default: smart-case match)
|
||||
+i Case-sensitive match
|
||||
-n, --nth=N[,..] Comma-separated list of field index expressions
|
||||
for limiting search scope. Each can be a non-zero
|
||||
integer or a range expression ([BEGIN]..[END])
|
||||
--with-nth=N[,..] Transform the item using index expressions for search
|
||||
-d, --delimiter=STR Field delimiter regex for --nth (default: AWK-style)
|
||||
|
||||
Search result
|
||||
-s, --sort=MAX Maximum number of matched items to sort (default: 1000)
|
||||
+s, --no-sort Do not sort the result. Keep the sequence unchanged.
|
||||
|
||||
Interface
|
||||
-m, --multi Enable multi-select with tab/shift-tab
|
||||
--no-mouse Disable mouse
|
||||
+c, --no-color Disable colors
|
||||
+2, --no-256 Disable 256-color
|
||||
--black Use black background
|
||||
--reverse Reverse orientation
|
||||
--prompt=STR Input prompt (default: '> ')
|
||||
|
||||
Scripting
|
||||
-q, --query=STR Start the finder with the given query
|
||||
-1, --select-1 Automatically select the only match
|
||||
-0, --exit-0 Exit immediately when there's no match
|
||||
-f, --filter=STR Filter mode. Do not start interactive finder.
|
||||
--print-query Print query as the first line
|
||||
|
||||
Environment variables
|
||||
FZF_DEFAULT_COMMAND Default command to use when input is tty
|
||||
FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
|
||||
```
|
||||
|
||||
fzf will launch curses-based finder, read the list from STDIN, and write the
|
||||
@@ -101,21 +122,26 @@ If you want to preserve the exact sequence of the input, provide `--no-sort` (or
|
||||
history | fzf +s
|
||||
```
|
||||
|
||||
### Key binding
|
||||
### Keys
|
||||
|
||||
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, press
|
||||
enter key to select the item. CTRL-C will terminate the finder.
|
||||
enter key to select the item. CTRL-C, CTRL-G, or ESC will terminate the finder.
|
||||
|
||||
The following readline key bindings should also work as expected.
|
||||
|
||||
- CTRL-A / CTRL-E
|
||||
- CTRL-B / CTRL-F
|
||||
- CTRL-W / CTRL-U
|
||||
- CTRL-H / CTRL-D
|
||||
- CTRL-W / CTRL-U / CTRL-Y
|
||||
- ALT-B / ALT-F
|
||||
|
||||
If you enable multi-select mode with `-m` option, you can select multiple items
|
||||
with TAB or Shift-TAB key.
|
||||
|
||||
You can also use mouse. Double-click on an item to select it or shift-click (or
|
||||
ctrl-click) to select multiple items. Use mouse wheel to move the cursor up and
|
||||
down.
|
||||
|
||||
### Extended-search mode
|
||||
|
||||
With `-x` or `--extended` option, fzf will start in "extended-search mode".
|
||||
@@ -132,129 +158,406 @@ such as: `^music .mp3$ sbtrkt !rmx`
|
||||
| `'wild` | Items that include `wild` | exact-match (quoted) |
|
||||
| `!'fire` | Items that do not include `fire` | inverse-exact-match |
|
||||
|
||||
Usage as Vim plugin
|
||||
-------------------
|
||||
If you don't need fuzzy matching and do not wish to "quote" every word, start
|
||||
fzf with `-e` or `--extended-exact` option.
|
||||
|
||||
If you install fzf as a Vim plugin, `:FZF` command will be added.
|
||||
|
||||
```vim
|
||||
:FZF
|
||||
:FZF --no-sort
|
||||
```
|
||||
|
||||
You can override the command which produces input to fzf.
|
||||
|
||||
```vim
|
||||
let g:fzf_command = 'find . -type f'
|
||||
```
|
||||
|
||||
Most of the time, you will prefer native Vim plugins with better integration
|
||||
with Vim. The only reason one might consider using fzf in Vim is its speed. For
|
||||
a very large list of files, fzf is significantly faster and it does not block.
|
||||
|
||||
Useful bash examples
|
||||
--------------------
|
||||
Useful examples
|
||||
---------------
|
||||
|
||||
```sh
|
||||
# vimf - Open selected file in Vim
|
||||
vimf() {
|
||||
FILE=$(fzf) && vim "$FILE"
|
||||
# fe [FUZZY PATTERN] - Open the selected file with the default editor
|
||||
# - Bypass fuzzy finder if there's only one match (--select-1)
|
||||
# - Exit if there's no match (--exit-0)
|
||||
fe() {
|
||||
local file
|
||||
file=$(fzf --query="$1" --select-1 --exit-0)
|
||||
[ -n "$file" ] && ${EDITOR:-vim} "$file"
|
||||
}
|
||||
|
||||
# fd - cd to selected directory
|
||||
fd() {
|
||||
DIR=$(find ${1:-*} -path '*/\.*' -prune -o -type d -print 2> /dev/null | fzf) && cd "$DIR"
|
||||
}
|
||||
|
||||
# fda - including hidden directories
|
||||
fda() {
|
||||
DIR=$(find ${1:-*} -type d 2> /dev/null | fzf) && cd "$DIR"
|
||||
local dir
|
||||
dir=$(find ${1:-*} -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf +m) &&
|
||||
cd "$dir"
|
||||
}
|
||||
|
||||
# fh - repeat history
|
||||
fh() {
|
||||
eval $(history | fzf +s | sed 's/ *[0-9]* *//')
|
||||
eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s | sed 's/ *[0-9]* *//')
|
||||
}
|
||||
|
||||
# fkill - kill process
|
||||
fkill() {
|
||||
ps -ef | sed 1d | fzf -m | awk '{print $2}' | xargs kill -${1:-9}
|
||||
}
|
||||
|
||||
# (Assuming you don't use the default CTRL-T and CTRL-R)
|
||||
|
||||
# CTRL-T - Paste the selected file path into the command line
|
||||
bind '"\er": redraw-current-line'
|
||||
bind '"\C-t": " \C-u \C-a\C-k$(fzf)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
bind '"\C-r": " \C-e\C-u$(history | fzf +s | sed \"s/ *[0-9]* *//\")\e\C-e\er"'
|
||||
```
|
||||
|
||||
zsh widgets
|
||||
-----------
|
||||
For more examples, see [the wiki
|
||||
page](https://github.com/junegunn/fzf/wiki/examples).
|
||||
|
||||
Key bindings for command line
|
||||
-----------------------------
|
||||
|
||||
The install script will setup the following key bindings for bash, zsh, and
|
||||
fish.
|
||||
|
||||
- `CTRL-T` - Paste the selected file path(s) into the command line
|
||||
- `CTRL-R` - Paste the selected command from history into the command line
|
||||
- `ALT-C` - cd into the selected directory
|
||||
|
||||
If you're on a tmux session, `CTRL-T` will launch fzf in a new split-window. You
|
||||
may disable this tmux integration by setting `FZF_TMUX` to 0, or change the
|
||||
height of the window with `FZF_TMUX_HEIGHT` (e.g. `20`, `50%`).
|
||||
|
||||
If you use vi mode on bash, you need to add `set -o vi` *before* `source
|
||||
~/.fzf.bash` in your .bashrc, so that it correctly sets up key bindings for vi
|
||||
mode.
|
||||
|
||||
If you want to customize the key bindings, consider editing the
|
||||
installer-generated source code: `~/.fzf.bash`, `~/.fzf.zsh`, and
|
||||
`~/.config/fish/functions/fzf_key_bindings.fish`.
|
||||
|
||||
Auto-completion
|
||||
---------------
|
||||
|
||||
Disclaimer: *Auto-completion feature is currently experimental, it can change
|
||||
over time*
|
||||
|
||||
### bash
|
||||
|
||||
#### Files and directories
|
||||
|
||||
Fuzzy completion for files and directories can be triggered if the word before
|
||||
the cursor ends with the trigger sequence which is by default `**`.
|
||||
|
||||
- `COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>`
|
||||
|
||||
```sh
|
||||
# CTRL-T - Paste the selected file(s) path into the command line
|
||||
fzf-file-widget() {
|
||||
local FILES
|
||||
local IFS="
|
||||
"
|
||||
FILES=($(
|
||||
find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m))
|
||||
unset IFS
|
||||
FILES=$FILES:q
|
||||
LBUFFER="${LBUFFER%% #} $FILES"
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-file-widget
|
||||
bindkey '^T' fzf-file-widget
|
||||
# Files under current directory
|
||||
# - You can select multiple items with TAB key
|
||||
vim **<TAB>
|
||||
|
||||
# ALT-C - cd into the selected directory
|
||||
fzf-cd-widget() {
|
||||
cd "${$(find * -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||
zle reset-prompt
|
||||
}
|
||||
zle -N fzf-cd-widget
|
||||
bindkey '\ec' fzf-cd-widget
|
||||
# Files under parent directory
|
||||
vim ../**<TAB>
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
fzf-history-widget() {
|
||||
LBUFFER=$(history | fzf +s | sed "s/ *[0-9]* *//")
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-history-widget
|
||||
bindkey '^R' fzf-history-widget
|
||||
# Files under parent directory that match `fzf`
|
||||
vim ../fzf**<TAB>
|
||||
|
||||
# Files under your home directory
|
||||
vim ~/**<TAB>
|
||||
|
||||
|
||||
# Directories under current directory (single-selection)
|
||||
cd **<TAB>
|
||||
|
||||
# Directories under ~/github that match `fzf`
|
||||
cd ~/github/fzf**<TAB>
|
||||
```
|
||||
|
||||
#### Process IDs
|
||||
|
||||
Fuzzy completion for PIDs is provided for kill command. In this case
|
||||
there is no trigger sequence, just press tab key after kill command.
|
||||
|
||||
```sh
|
||||
# Can select multiple processes with <TAB> or <Shift-TAB> keys
|
||||
kill -9 <TAB>
|
||||
```
|
||||
|
||||
#### Host names
|
||||
|
||||
For ssh and telnet commands, fuzzy completion for host names is provided. The
|
||||
names are extracted from /etc/hosts and ~/.ssh/config.
|
||||
|
||||
```sh
|
||||
ssh **<TAB>
|
||||
telnet **<TAB>
|
||||
```
|
||||
|
||||
#### Environment variables / Aliases
|
||||
|
||||
```sh
|
||||
unset **<TAB>
|
||||
export **<TAB>
|
||||
unalias **<TAB>
|
||||
```
|
||||
|
||||
#### Settings
|
||||
|
||||
```sh
|
||||
# Use ~~ as the trigger sequence instead of the default **
|
||||
export FZF_COMPLETION_TRIGGER='~~'
|
||||
|
||||
# Options to fzf command
|
||||
export FZF_COMPLETION_OPTS='+c -x'
|
||||
```
|
||||
|
||||
### zsh
|
||||
|
||||
TODO :smiley:
|
||||
|
||||
(Pull requests are appreciated.)
|
||||
|
||||
Usage as Vim plugin
|
||||
-------------------
|
||||
|
||||
(Note: To use fzf in GVim, an external terminal emulator is required.)
|
||||
|
||||
### `:FZF[!]`
|
||||
|
||||
If you have set up fzf for Vim, `:FZF` command will be added.
|
||||
|
||||
```vim
|
||||
" Look for files under current directory
|
||||
:FZF
|
||||
|
||||
" Look for files under your home directory
|
||||
:FZF ~
|
||||
|
||||
" With options
|
||||
:FZF --no-sort -m /tmp
|
||||
```
|
||||
|
||||
Note that the environment variables `FZF_DEFAULT_COMMAND` and `FZF_DEFAULT_OPTS`
|
||||
also apply here.
|
||||
|
||||
If you're on a tmux session, `:FZF` will launch fzf in a new split-window whose
|
||||
height can be adjusted with `g:fzf_tmux_height` (default: '40%'). However, the
|
||||
bang version (`:FZF!`) will always start in fullscreen.
|
||||
|
||||
In GVim, you need an external terminal emulator to start fzf with. `xterm`
|
||||
command is used by default, but you can customize it with `g:fzf_launcher`.
|
||||
|
||||
```vim
|
||||
" This is the default. %s is replaced with fzf command
|
||||
let g:fzf_launcher = 'xterm -e bash -ic %s'
|
||||
|
||||
" Use urxvt instead
|
||||
let g:fzf_launcher = 'urxvt -geometry 120x30 -e sh -c %s'
|
||||
```
|
||||
|
||||
If you're running MacVim on OSX, I recommend you to use iTerm2 as the launcher.
|
||||
Refer to the [this wiki
|
||||
page](https://github.com/junegunn/fzf/wiki/fzf-with-MacVim-and-iTerm2) to see
|
||||
how to set up.
|
||||
|
||||
### `fzf#run([options])`
|
||||
|
||||
For more advanced uses, you can call `fzf#run()` function which returns the list
|
||||
of the selected items.
|
||||
|
||||
`fzf#run()` may take an options-dictionary:
|
||||
|
||||
| Option name | Type | Description |
|
||||
| --------------- | ------------- | ------------------------------------------------------------------ |
|
||||
| `source` | string | External command to generate input to fzf (e.g. `find .`) |
|
||||
| `source` | list | Vim list as input to fzf |
|
||||
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
|
||||
| `sink` | funcref | Reference to function to process each selected item |
|
||||
| `options` | string | Options to fzf |
|
||||
| `dir` | string | Working directory |
|
||||
| `tmux_width` | number/string | Use tmux vertical split with the given height (e.g. `20`, `50%`) |
|
||||
| `tmux_height` | number/string | Use tmux horizontal split with the given height (e.g. `20`, `50%`) |
|
||||
| `launcher` | string | External terminal emulator to start fzf with (Only used in GVim) |
|
||||
|
||||
#### Examples
|
||||
|
||||
If `sink` option is not given, `fzf#run` will simply return the list.
|
||||
|
||||
```vim
|
||||
let items = fzf#run({ 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||
```
|
||||
|
||||
But if `sink` is given as a string, the command will be executed for each
|
||||
selected item.
|
||||
|
||||
```vim
|
||||
" Each selected item will be opened in a new tab
|
||||
let items = fzf#run({ 'sink': 'tabe', 'options': '-m +c', 'dir': '~', 'source': 'ls' })
|
||||
```
|
||||
|
||||
We can also use a Vim list as the source as follows:
|
||||
|
||||
```vim
|
||||
" Choose a color scheme with fzf
|
||||
nnoremap <silent> <Leader>C :call fzf#run({
|
||||
\ 'source':
|
||||
\ map(split(globpath(&rtp, "colors/*.vim"), "\n"),
|
||||
\ "substitute(fnamemodify(v:val, ':t'), '\\..\\{-}$', '', '')"),
|
||||
\ 'sink': 'colo',
|
||||
\ 'options': '+m',
|
||||
\ 'tmux_width': 20,
|
||||
\ 'launcher': 'xterm -geometry 20x30 -e bash -ic %s'
|
||||
\ })<CR>
|
||||
```
|
||||
|
||||
`sink` option can be a function reference. The following example creates a
|
||||
handy mapping that selects an open buffer.
|
||||
|
||||
```vim
|
||||
" List of buffers
|
||||
function! BufList()
|
||||
redir => ls
|
||||
silent ls
|
||||
redir END
|
||||
return split(ls, '\n')
|
||||
endfunction
|
||||
|
||||
function! BufOpen(e)
|
||||
execute 'buffer '. matchstr(a:e, '^[ 0-9]*')
|
||||
endfunction
|
||||
|
||||
nnoremap <silent> <Leader><Enter> :call fzf#run({
|
||||
\ 'source': reverse(BufList()),
|
||||
\ 'sink': function('BufOpen'),
|
||||
\ 'options': '+m',
|
||||
\ 'tmux_height': '40%'
|
||||
\ })<CR>
|
||||
```
|
||||
|
||||
### Articles
|
||||
|
||||
- [fzf+vim+tmux](http://junegunn.kr/2014/04/fzf+vim+tmux)
|
||||
|
||||
Tips
|
||||
----
|
||||
|
||||
### Faster startup with `--disable-gems` options
|
||||
### Rendering issues
|
||||
|
||||
If you're running Ruby 1.9 or above, you can improve the startup time with
|
||||
`--disable-gems` option to Ruby.
|
||||
If you have any rendering issues, check the followings:
|
||||
|
||||
- `time ruby ~/bin/fzf -h`
|
||||
- 0.077 sec
|
||||
- `time ruby --disable-gems ~/bin/fzf -h`
|
||||
- 0.025 sec
|
||||
1. Make sure `$TERM` is correctly set. fzf will use 256-color only if it
|
||||
contains `256` (e.g. `xterm-256color`)
|
||||
2. If you're on screen or tmux, `$TERM` should be either `screen` or
|
||||
`screen-256color`
|
||||
3. Some terminal emulators (e.g. mintty) have problem displaying default
|
||||
background color and make some text unable to read. In that case, try `--black`
|
||||
option. And if it solves your problem, I recommend including it in
|
||||
`FZF_DEFAULT_OPTS` for further convenience.
|
||||
4. If you still have problem, try `--no-256` option or even `--no-color`.
|
||||
5. Ruby 1.9 or above is required for correctly displaying unicode characters.
|
||||
|
||||
Define fzf alias with the option as follows:
|
||||
### Ranking algorithm
|
||||
|
||||
fzf sorts the result first by the length of the matched substring, then by the
|
||||
length of the whole string. However it only does so when the number of matches
|
||||
is less than the limit which is by default 1000, in order to avoid the cost of
|
||||
sorting a large list and limit the response time of the query.
|
||||
|
||||
This limit can be adjusted with `-s` option, or with the environment variable
|
||||
`FZF_DEFAULT_OPTS`.
|
||||
|
||||
```sh
|
||||
alias fzf='ruby --disable-gems ~/bin/fzf'
|
||||
export FZF_DEFAULT_OPTS="--sort 20000"
|
||||
```
|
||||
|
||||
### Incorrect display on Ruby 1.8
|
||||
### Respecting `.gitignore`, `.hgignore`, and `svn:ignore`
|
||||
|
||||
It is reported that the output of fzf can become unreadable on some terminals
|
||||
when it's running on Ruby 1.8. If you experience the problem, upgrade your Ruby
|
||||
to 1.9 or above. Ruby 1.9 or above is also required for displaying Unicode
|
||||
characters.
|
||||
[ag](https://github.com/ggreer/the_silver_searcher) or
|
||||
[pt](https://github.com/monochromegane/the_platinum_searcher) will do the
|
||||
filtering:
|
||||
|
||||
```sh
|
||||
# Feed the output of ag into fzf
|
||||
ag -l -g "" | fzf
|
||||
|
||||
# Setting ag as the default source for fzf
|
||||
export FZF_DEFAULT_COMMAND='ag -l -g ""'
|
||||
|
||||
# Now fzf (w/o pipe) will use ag instead of find
|
||||
fzf
|
||||
```
|
||||
|
||||
### `git ls-tree` for fast traversal
|
||||
|
||||
If you're running fzf in a large git repository, `git ls-tree` can boost up the
|
||||
speed of the traversal.
|
||||
|
||||
```sh
|
||||
# Copy the original fzf function to __fzf
|
||||
declare -f __fzf > /dev/null ||
|
||||
eval "$(echo "__fzf() {"; declare -f fzf | \grep -v '^{' | tail -n +2)"
|
||||
|
||||
# Use git ls-tree when possible
|
||||
fzf() {
|
||||
if [ -n "$(git rev-parse HEAD 2> /dev/null)" ]; then
|
||||
FZF_DEFAULT_COMMAND="git ls-tree -r --name-only HEAD" __fzf "$@"
|
||||
else
|
||||
__fzf "$@"
|
||||
fi
|
||||
}
|
||||
```
|
||||
|
||||
### Using fzf with tmux splits
|
||||
|
||||
It isn't too hard to write your own fzf-tmux combo like the default
|
||||
CTRL-T key binding. (Or is it?)
|
||||
|
||||
```sh
|
||||
# This is a helper function that splits the current pane to start the given
|
||||
# command ($1) and sends its output back to the original pane with any number of
|
||||
# optional keys (shift; $*).
|
||||
fzf_tmux_helper() {
|
||||
[ -n "$TMUX_PANE" ] || return
|
||||
local cmd=$1
|
||||
shift
|
||||
tmux split-window -p 40 \
|
||||
"bash -c \"\$(tmux send-keys -t $TMUX_PANE \"\$(source ~/.fzf.bash; $cmd)\" $*)\""
|
||||
}
|
||||
|
||||
# This is the function we are going to run in the split pane.
|
||||
# - "find" to list the directories
|
||||
# - "sed" will escape spaces in the paths.
|
||||
# - "paste" will join the selected paths into a single line
|
||||
fzf_tmux_dir() {
|
||||
fzf_tmux_helper \
|
||||
'find * -path "*/\.*" -prune -o -type d -print 2> /dev/null |
|
||||
fzf --multi |
|
||||
sed "s/ /\\\\ /g" |
|
||||
paste -sd" " -' Space
|
||||
}
|
||||
|
||||
# Bind CTRL-X-CTRL-D to fzf_tmux_dir
|
||||
bind '"\C-x\C-d": "$(fzf_tmux_dir)\e\C-e"'
|
||||
```
|
||||
|
||||
### Fish shell
|
||||
|
||||
It's [a known bug of fish](https://github.com/fish-shell/fish-shell/issues/1362)
|
||||
that it doesn't allow reading from STDIN in command substitution, which means
|
||||
simple `vim (fzf)` won't work as expected. The workaround is to store the result
|
||||
of fzf to a temporary file.
|
||||
|
||||
```sh
|
||||
function vimf
|
||||
if fzf > $TMPDIR/fzf.result
|
||||
vim (cat $TMPDIR/fzf.result)
|
||||
end
|
||||
end
|
||||
|
||||
function fe
|
||||
set tmp $TMPDIR/fzf.result
|
||||
fzf --query="$argv[1]" --select-1 --exit-0 > $tmp
|
||||
if [ (cat $tmp | wc -l) -gt 0 ]
|
||||
vim (cat $tmp)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### Windows
|
||||
|
||||
fzf works on [Cygwin](http://www.cygwin.com/) and
|
||||
[MSYS2](http://sourceforge.net/projects/msys2/). You may need to use `--black`
|
||||
option on MSYS2 to avoid rendering issues.
|
||||
|
||||
### Handling UTF-8 NFD paths on OSX
|
||||
|
||||
Use iconv to convert NFD paths to NFC:
|
||||
|
||||
```sh
|
||||
find . | iconv -f utf-8-mac -t utf8//ignore | fzf
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
|
1
Rakefile
1
Rakefile
@@ -6,3 +6,4 @@ Rake::TestTask.new(:test) do |test|
|
||||
test.verbose = true
|
||||
end
|
||||
|
||||
task :default => :test
|
||||
|
9
ext/mkrf_conf.rb
Normal file
9
ext/mkrf_conf.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
require 'rubygems/dependency_installer'
|
||||
|
||||
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.1.0')
|
||||
Gem::DependencyInstaller.new.install 'curses', '~> 1.0'
|
||||
end
|
||||
|
||||
File.open(File.expand_path('../Rakefile', __FILE__), 'w') do |f|
|
||||
f.puts 'task :default'
|
||||
end
|
240
fzf-completion.bash
Normal file
240
fzf-completion.bash
Normal file
@@ -0,0 +1,240 @@
|
||||
#!/bin/bash
|
||||
# ____ ____
|
||||
# / __/___ / __/
|
||||
# / /_/_ / / /_
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/-completion.bash
|
||||
#
|
||||
# - $FZF_COMPLETION_TRIGGER (default: '**')
|
||||
# - $FZF_COMPLETION_OPTS (default: empty)
|
||||
|
||||
_fzf_orig_completion_filter() {
|
||||
sed 's/.*-F *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\2=\1;/' |
|
||||
sed 's/[^a-z0-9_= ;]/_/g'
|
||||
}
|
||||
|
||||
_fzf_opts_completion() {
|
||||
local cur prev opts
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
||||
opts="
|
||||
-x --extended
|
||||
-e --extended-exact
|
||||
-i +i
|
||||
-n --nth
|
||||
-d --delimiter
|
||||
-s --sort +s
|
||||
-m --multi
|
||||
--no-mouse
|
||||
+c --no-color
|
||||
+2 --no-256
|
||||
--black
|
||||
--reverse
|
||||
--prompt
|
||||
-q --query
|
||||
-1 --select-1
|
||||
-0 --exit-0
|
||||
-f --filter
|
||||
--print-query"
|
||||
|
||||
case "${prev}" in
|
||||
--sort|-s)
|
||||
COMPREPLY=( $(compgen -W "$(seq 2000 1000 10000)" -- ${cur}) )
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ ${cur} =~ ^-|\+ ]]; then
|
||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
_fzf_path_completion() {
|
||||
local cur base dir leftover matches trigger cmd orig
|
||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||
COMPREPLY=()
|
||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
if [[ ${cur} == *"$trigger" ]]; then
|
||||
base=${cur:0:${#cur}-${#trigger}}
|
||||
eval base=$base
|
||||
|
||||
dir="$base"
|
||||
while [ 1 ]; do
|
||||
if [ -z "$dir" -o -d "$dir" ]; then
|
||||
leftover=${base/#"$dir"}
|
||||
leftover=${leftover/#\/}
|
||||
[ "$dir" = './' ] && dir=''
|
||||
tput sc
|
||||
matches=$(find -L "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
|
||||
printf '%q ' "$item"
|
||||
done)
|
||||
matches=${matches% }
|
||||
if [ -n "$matches" ]; then
|
||||
COMPREPLY=( "$matches" )
|
||||
else
|
||||
COMPREPLY=( "$cur" )
|
||||
fi
|
||||
tput rc
|
||||
return 0
|
||||
fi
|
||||
dir=$(dirname "$dir")
|
||||
[[ "$dir" =~ /$ ]] || dir="$dir"/
|
||||
done
|
||||
else
|
||||
shift
|
||||
shift
|
||||
orig=$(eval "echo \$_fzf_orig_completion_$cmd")
|
||||
[ -n "$orig" ] && type "$orig" > /dev/null 2>&1 && $orig "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_list_completion() {
|
||||
local cur selected trigger cmd src ret
|
||||
read -r src
|
||||
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
|
||||
trigger=${FZF_COMPLETION_TRIGGER:-**}
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
if [[ ${cur} == *"$trigger" ]]; then
|
||||
cur=${cur:0:${#cur}-${#trigger}}
|
||||
|
||||
tput sc
|
||||
selected=$(eval "$src | fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
|
||||
selected=${selected% }
|
||||
tput rc
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
COMPREPLY=("$selected")
|
||||
return 0
|
||||
fi
|
||||
else
|
||||
shift
|
||||
orig=$(eval "echo \$_fzf_orig_completion_$cmd")
|
||||
if [ -n "$orig" ] && type "$orig" > /dev/null; then
|
||||
$orig "$@"
|
||||
elif [ -n "$_fzf_completion_loader" ]; then
|
||||
_completion_loader "$@"
|
||||
ret=$?
|
||||
eval $(complete | \grep "\-F.* $cmd$" | _fzf_orig_completion_filter)
|
||||
source $BASH_SOURCE
|
||||
return $ret
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_all_completion() {
|
||||
_fzf_path_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type d -print -o -type f -print -o -type l -print" \
|
||||
"-m" "$@"
|
||||
}
|
||||
|
||||
_fzf_file_completion() {
|
||||
_fzf_path_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
|
||||
"-m" "$@"
|
||||
}
|
||||
|
||||
_fzf_dir_completion() {
|
||||
_fzf_path_completion \
|
||||
"-name .git -prune -o -name .svn -prune -o -type d -print" \
|
||||
"" "$@"
|
||||
}
|
||||
|
||||
_fzf_kill_completion() {
|
||||
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
|
||||
|
||||
local selected
|
||||
tput sc
|
||||
selected=$(ps -ef | sed 1d | fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
|
||||
tput rc
|
||||
|
||||
if [ -n "$selected" ]; then
|
||||
COMPREPLY=( "$selected" )
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
_fzf_telnet_completion() {
|
||||
_fzf_list_completion '+m' "$@" << "EOF"
|
||||
\grep -v '^\s*\(#\|$\)' /etc/hosts | awk '{if (length($2) > 0) {print $2}}' | sort -u
|
||||
EOF
|
||||
}
|
||||
|
||||
_fzf_ssh_completion() {
|
||||
_fzf_list_completion '+m' "$@" << "EOF"
|
||||
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i ^host | \grep -v '*') <(\grep -v '^\s*\(#\|$\)' /etc/hosts) | awk '{print $2}' | sort -u
|
||||
EOF
|
||||
}
|
||||
|
||||
_fzf_env_var_completion() {
|
||||
_fzf_list_completion '-m' "$@" << "EOF"
|
||||
declare -xp | sed 's/=.*//' | sed 's/.* //'
|
||||
EOF
|
||||
}
|
||||
|
||||
_fzf_alias_completion() {
|
||||
_fzf_list_completion '-m' "$@" << "EOF"
|
||||
alias | sed 's/=.*//' | sed 's/.* //'
|
||||
EOF
|
||||
}
|
||||
|
||||
# fzf options
|
||||
complete -F _fzf_opts_completion fzf
|
||||
|
||||
d_cmds="cd pushd rmdir"
|
||||
f_cmds="
|
||||
awk cat diff diff3
|
||||
emacs ex file ftp g++ gcc gvim head hg java
|
||||
javac ld less more mvim patch perl python ruby
|
||||
sed sftp sort source tail tee uniq vi view vim wc"
|
||||
a_cmds="
|
||||
basename bunzip2 bzip2 chmod chown curl cp dirname du
|
||||
find git grep gunzip gzip hg jar
|
||||
ln ls mv open rm rsync scp
|
||||
svn tar unzip zip"
|
||||
x_cmds="kill ssh telnet unset unalias export"
|
||||
|
||||
# Preserve existing completion
|
||||
if [ "$_fzf_completion_loaded" != '0.8.6-1' ]; then
|
||||
# Really wish I could use associative array but OSX comes with bash 3.2 :(
|
||||
eval $(complete | \grep '\-F' | \grep -v _fzf_ |
|
||||
\grep -E " ($(echo $d_cmds $f_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
|
||||
export _fzf_completion_loaded=0.8.6-1
|
||||
fi
|
||||
|
||||
if type _completion_loader > /dev/null 2>&1; then
|
||||
_fzf_completion_loader=1
|
||||
fi
|
||||
|
||||
# Directory
|
||||
for cmd in $d_cmds; do
|
||||
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# File
|
||||
for cmd in $f_cmds; do
|
||||
complete -F _fzf_file_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# Anything
|
||||
for cmd in $a_cmds; do
|
||||
complete -F _fzf_all_completion -o default -o bashdefault $cmd
|
||||
done
|
||||
|
||||
# Kill completion
|
||||
complete -F _fzf_kill_completion -o nospace -o default -o bashdefault kill
|
||||
|
||||
# Host completion
|
||||
complete -F _fzf_ssh_completion -o default -o bashdefault ssh
|
||||
complete -F _fzf_telnet_completion -o default -o bashdefault telnet
|
||||
|
||||
# Environment variables / Aliases
|
||||
complete -F _fzf_env_var_completion -o default -o bashdefault unset
|
||||
complete -F _fzf_env_var_completion -o default -o bashdefault export
|
||||
complete -F _fzf_alias_completion -o default -o bashdefault unalias
|
||||
|
||||
unset cmd d_cmds f_cmds a_cmds x_cmds
|
9
fzf-completion.zsh
Normal file
9
fzf-completion.zsh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/zsh
|
||||
# ____ ____
|
||||
# / __/___ / __/
|
||||
# / /_/_ / / /_
|
||||
# / __/ / /_/ __/
|
||||
# /_/ /___/_/-completion.zsh
|
||||
#
|
||||
|
||||
# TODO
|
@@ -1,7 +1,7 @@
|
||||
# coding: utf-8
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = 'fzf'
|
||||
spec.version = '0.4.0'
|
||||
spec.version = '0.8.4'
|
||||
spec.authors = ['Junegunn Choi']
|
||||
spec.email = ['junegunn.c@gmail.com']
|
||||
spec.description = %q{Fuzzy finder for your shell}
|
||||
@@ -12,4 +12,6 @@ Gem::Specification.new do |spec|
|
||||
spec.bindir = '.'
|
||||
spec.files = %w[fzf.gemspec]
|
||||
spec.executables = 'fzf'
|
||||
|
||||
spec.extensions += ['ext/mkrf_conf.rb']
|
||||
end
|
||||
|
380
install
380
install
@@ -1,7 +1,381 @@
|
||||
#!/bin/bash
|
||||
|
||||
cd `dirname $BASH_SOURCE`
|
||||
mkdir -p ~/bin
|
||||
ln -sf `pwd`/fzf ~/bin/fzf
|
||||
chmod +x ~/bin/fzf
|
||||
fzf_base=`pwd`
|
||||
|
||||
# ruby executable
|
||||
echo -n "Checking Ruby executable ... "
|
||||
ruby=`which ruby`
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "ruby executable not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# System ruby is preferred
|
||||
system_ruby=/usr/bin/ruby
|
||||
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
||||
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
||||
[ $? -eq 0 ] && ruby=$system_ruby
|
||||
fi
|
||||
|
||||
echo "OK ($ruby)"
|
||||
|
||||
# Curses-support
|
||||
echo -n "Checking Curses support ... "
|
||||
"$ruby" -rcurses -e0 2> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "Not found"
|
||||
echo "Installing 'curses' gem ... "
|
||||
if (( EUID )); then
|
||||
/usr/bin/env gem install curses --user-install
|
||||
else
|
||||
/usr/bin/env gem install curses
|
||||
fi
|
||||
if [ $? -ne 0 ]; then
|
||||
echo
|
||||
echo "Failed to install 'curses' gem."
|
||||
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
||||
echo "Make sure that base-devel package group is installed."
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Ruby version
|
||||
echo -n "Checking Ruby version ... "
|
||||
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
||||
if [ $? -eq 0 ]; then
|
||||
echo ">= 1.9"
|
||||
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
||||
else
|
||||
fzf_cmd="$ruby $fzf_base/fzf"
|
||||
fi
|
||||
else
|
||||
echo "< 1.9"
|
||||
fzf_cmd="$ruby $fzf_base/fzf"
|
||||
fi
|
||||
|
||||
# Auto-completion
|
||||
read -p "Do you want to add auto-completion support? ([y]/n) " -n 1 -r
|
||||
echo
|
||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||
auto_completion=$?
|
||||
|
||||
# Key-bindings
|
||||
read -p "Do you want to add key bindings? ([y]/n) " -n 1 -r
|
||||
echo
|
||||
[[ ! $REPLY =~ ^[Nn]$ ]]
|
||||
key_bindings=$?
|
||||
|
||||
echo
|
||||
for shell in bash zsh; do
|
||||
echo -n "Generate ~/.fzf.$shell ... "
|
||||
src=~/.fzf.${shell}
|
||||
|
||||
fzf_completion="[[ \$- =~ i ]] && source $fzf_base/fzf-completion.${shell}"
|
||||
if [ $auto_completion -ne 0 ]; then
|
||||
fzf_completion="# $fzf_completion"
|
||||
fi
|
||||
|
||||
cat > $src << EOF
|
||||
# Setup fzf function
|
||||
# ------------------
|
||||
unalias fzf 2> /dev/null
|
||||
fzf() {
|
||||
$fzf_cmd "\$@"
|
||||
}
|
||||
export -f fzf > /dev/null
|
||||
|
||||
# Auto-completion
|
||||
# ---------------
|
||||
$fzf_completion
|
||||
|
||||
EOF
|
||||
|
||||
if [ $key_bindings -eq 0 ]; then
|
||||
if [ $shell = bash ]; then
|
||||
cat >> $src << "EOFZF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
__fsel() {
|
||||
command find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null | 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 '*/\.*' -prune -o -type d -print 2> /dev/null | 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 +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 +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
|
||||
EOFZF
|
||||
else
|
||||
cat >> $src << "EOFZF"
|
||||
# Key bindings
|
||||
# ------------
|
||||
# CTRL-T - Paste the selected file path(s) into the command line
|
||||
__fsel() {
|
||||
set -o nonomatch
|
||||
command find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null | fzf -m | while read item; do
|
||||
printf '%q ' "$item"
|
||||
done
|
||||
echo
|
||||
}
|
||||
|
||||
if [[ $- =~ i ]]; then
|
||||
|
||||
if [ -n "$TMUX_PANE" -a ${FZF_TMUX:-1} -ne 0 -a ${LINES:-40} -gt 15 ]; then
|
||||
fzf-file-widget() {
|
||||
local height
|
||||
height=${FZF_TMUX_HEIGHT:-40%}
|
||||
if [[ $height =~ %$ ]]; then
|
||||
height="-p ${height%\%}"
|
||||
else
|
||||
height="-l $height"
|
||||
fi
|
||||
tmux split-window $height "zsh -c 'source ~/.fzf.zsh; tmux send-keys -t $TMUX_PANE \"\$(__fsel)\"'"
|
||||
}
|
||||
else
|
||||
fzf-file-widget() {
|
||||
LBUFFER="${LBUFFER}$(__fsel)"
|
||||
zle redisplay
|
||||
}
|
||||
fi
|
||||
zle -N fzf-file-widget
|
||||
bindkey '^T' fzf-file-widget
|
||||
|
||||
# ALT-C - cd into the selected directory
|
||||
fzf-cd-widget() {
|
||||
cd "${$(set -o nonomatch; command find -L * -path '*/\.*' -prune \
|
||||
-o -type d -print 2> /dev/null | fzf):-.}"
|
||||
zle reset-prompt
|
||||
}
|
||||
zle -N fzf-cd-widget
|
||||
bindkey '\ec' fzf-cd-widget
|
||||
|
||||
# CTRL-R - Paste the selected command from history into the command line
|
||||
fzf-history-widget() {
|
||||
LBUFFER=$(fc -l 1 | fzf +s +m -n2..,.. | sed "s/ *[0-9*]* *//")
|
||||
zle redisplay
|
||||
}
|
||||
zle -N fzf-history-widget
|
||||
bindkey '^R' fzf-history-widget
|
||||
|
||||
fi
|
||||
EOFZF
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "OK"
|
||||
done
|
||||
|
||||
# fish
|
||||
has_fish=0
|
||||
if [ -n "$(which fish)" ]; then
|
||||
has_fish=1
|
||||
echo -n "Generate ~/.config/fish/functions/fzf.fish ... "
|
||||
mkdir -p ~/.config/fish/functions
|
||||
cat > ~/.config/fish/functions/fzf.fish << EOFZF
|
||||
function fzf
|
||||
$fzf_cmd \$argv
|
||||
end
|
||||
EOFZF
|
||||
echo "OK"
|
||||
|
||||
if [ $key_bindings -eq 0 ]; then
|
||||
echo -n "Generate ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
||||
cat > ~/.config/fish/functions/fzf_key_bindings.fish << "EOFZF"
|
||||
function fzf_key_bindings
|
||||
# Due to a bug of fish, we cannot use command substitution,
|
||||
# so we use temporary file instead
|
||||
if [ -z "$TMPDIR" ]
|
||||
set -g TMPDIR /tmp
|
||||
end
|
||||
|
||||
function __fzf_list
|
||||
command find * -path '*/\.*' -prune \
|
||||
-o -type f -print \
|
||||
-o -type d -print \
|
||||
-o -type l -print 2> /dev/null
|
||||
end
|
||||
|
||||
function __fzf_list_dir
|
||||
command find -L * -path '*/\.*' -prune -o -type d -print 2> /dev/null
|
||||
end
|
||||
|
||||
function __fzf_escape
|
||||
while read item
|
||||
echo -n (echo -n "$item" | sed -E 's/([ "$~'\''([{<>})])/\\\\\\1/g')' '
|
||||
end
|
||||
end
|
||||
|
||||
function __fzf_ctrl_t
|
||||
if [ -n "$TMUX_PANE" -a "$FZF_TMUX" != "0" ]
|
||||
tmux split-window (__fzf_tmux_height) "fish -c 'fzf_key_bindings; __fzf_ctrl_t_tmux \\$TMUX_PANE'"
|
||||
else
|
||||
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||
and commandline -i (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||
commandline -f repaint
|
||||
rm -f $TMPDIR/fzf.result
|
||||
end
|
||||
end
|
||||
|
||||
function __fzf_ctrl_t_tmux
|
||||
__fzf_list | fzf -m > $TMPDIR/fzf.result
|
||||
and tmux send-keys -t $argv[1] (cat $TMPDIR/fzf.result | __fzf_escape)
|
||||
rm -f $TMPDIR/fzf.result
|
||||
end
|
||||
|
||||
function __fzf_ctrl_r
|
||||
history | fzf +s +m > $TMPDIR/fzf.result
|
||||
and commandline (cat $TMPDIR/fzf.result)
|
||||
commandline -f repaint
|
||||
rm -f $TMPDIR/fzf.result
|
||||
end
|
||||
|
||||
function __fzf_alt_c
|
||||
# Fish hangs if the command before pipe redirects (2> /dev/null)
|
||||
__fzf_list_dir | fzf +m > $TMPDIR/fzf.result
|
||||
[ (cat $TMPDIR/fzf.result | wc -l) -gt 0 ]
|
||||
and cd (cat $TMPDIR/fzf.result)
|
||||
commandline -f repaint
|
||||
rm -f $TMPDIR/fzf.result
|
||||
end
|
||||
|
||||
function __fzf_tmux_height
|
||||
if set -q FZF_TMUX_HEIGHT
|
||||
set height $FZF_TMUX_HEIGHT
|
||||
else
|
||||
set height 40%
|
||||
end
|
||||
if echo $height | \grep -q -E '%$'
|
||||
echo "-p "(echo $height | sed 's/%$//')
|
||||
else
|
||||
echo "-l $height"
|
||||
end
|
||||
set -e height
|
||||
end
|
||||
|
||||
bind \ct '__fzf_ctrl_t'
|
||||
bind \cr '__fzf_ctrl_r'
|
||||
bind \ec '__fzf_alt_c'
|
||||
end
|
||||
EOFZF
|
||||
echo "OK"
|
||||
fi
|
||||
fi
|
||||
|
||||
append_line() {
|
||||
echo "Update $2:"
|
||||
echo " - $1"
|
||||
[ -f "$2" ] || touch "$2"
|
||||
if [ $# -lt 3 ]; then
|
||||
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||
else
|
||||
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
||||
fi
|
||||
if [ -n "$line" ]; then
|
||||
echo " - Already exists: line #$line"
|
||||
else
|
||||
echo "$1" >> "$2"
|
||||
echo " + Added"
|
||||
fi
|
||||
echo
|
||||
}
|
||||
|
||||
echo
|
||||
for shell in bash zsh; do
|
||||
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
|
||||
done
|
||||
|
||||
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
||||
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||
append_line "fzf_key_bindings" "$bind_file"
|
||||
|
||||
echo ' * Due to a known bug of fish, you may have issues running fzf on fish.'
|
||||
echo ' * If that happens, try the following:'
|
||||
echo ' - Remove ~/.config/fish/functions/fzf.fish'
|
||||
echo ' - Place fzf executable in a directory included in $PATH'
|
||||
echo
|
||||
fi
|
||||
|
||||
cat << EOF
|
||||
Finished. Restart your shell or reload config file.
|
||||
source ~/.bashrc # bash
|
||||
source ~/.zshrc # zsh
|
||||
EOF
|
||||
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
||||
|
||||
Use uninstall script to remove fzf.
|
||||
|
||||
For more information, see: https://github.com/junegunn/fzf
|
||||
EOF
|
||||
|
||||
|
240
plugin/fzf.vim
240
plugin/fzf.vim
@@ -1,4 +1,4 @@
|
||||
" Copyright (c) 2013 Junegunn Choi
|
||||
" Copyright (c) 2014 Junegunn Choi
|
||||
"
|
||||
" MIT License
|
||||
"
|
||||
@@ -21,25 +21,229 @@
|
||||
" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
let s:exec = expand('<sfile>:h:h').'/fzf'
|
||||
let s:min_tmux_width = 10
|
||||
let s:min_tmux_height = 3
|
||||
let s:default_tmux_height = '40%'
|
||||
let s:launcher = 'xterm -e bash -ic %s'
|
||||
let s:fzf_rb = expand('<sfile>:h:h').'/fzf'
|
||||
|
||||
function! s:fzf(args)
|
||||
try
|
||||
let tf = tempname()
|
||||
let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''
|
||||
let fzf = executable(s:exec) ? s:exec : 'fzf'
|
||||
execute "silent !".prefix.fzf." ".a:args." > ".tf
|
||||
if !v:shell_error
|
||||
let file = join(readfile(tf), '')
|
||||
if !empty(file)
|
||||
execute 'silent e '.file
|
||||
endif
|
||||
let s:cpo_save = &cpo
|
||||
set cpo&vim
|
||||
|
||||
function! s:fzf_exec()
|
||||
if !exists('s:exec')
|
||||
call system('type fzf')
|
||||
if v:shell_error
|
||||
let s:exec = executable(s:fzf_rb) ? s:fzf_rb : ''
|
||||
else
|
||||
let s:exec = 'fzf'
|
||||
endif
|
||||
finally
|
||||
silent! call delete(tf)
|
||||
redraw!
|
||||
endtry
|
||||
return s:fzf_exec()
|
||||
elseif empty(s:exec)
|
||||
unlet s:exec
|
||||
throw 'fzf executable not found'
|
||||
else
|
||||
return s:exec
|
||||
endif
|
||||
endfunction
|
||||
|
||||
command! -nargs=* FZF call s:fzf(<q-args>)
|
||||
function! s:tmux_enabled()
|
||||
if has('gui_running')
|
||||
return 0
|
||||
endif
|
||||
|
||||
if exists('s:tmux')
|
||||
return s:tmux
|
||||
endif
|
||||
|
||||
let s:tmux = 0
|
||||
if exists('$TMUX')
|
||||
let output = system('tmux -V')
|
||||
let s:tmux = !v:shell_error && output >= 'tmux 1.7'
|
||||
endif
|
||||
return s:tmux
|
||||
endfunction
|
||||
|
||||
function! s:shellesc(arg)
|
||||
return '"'.substitute(a:arg, '"', '\\"', 'g').'"'
|
||||
endfunction
|
||||
|
||||
function! s:escape(path)
|
||||
return substitute(a:path, ' ', '\\ ', 'g')
|
||||
endfunction
|
||||
|
||||
function! fzf#run(...) abort
|
||||
let dict = exists('a:1') ? a:1 : {}
|
||||
let temps = { 'result': tempname() }
|
||||
let optstr = get(dict, 'options', '')
|
||||
try
|
||||
let fzf_exec = s:fzf_exec()
|
||||
catch
|
||||
throw v:exception
|
||||
endtry
|
||||
|
||||
if has_key(dict, 'source')
|
||||
let source = dict.source
|
||||
let type = type(source)
|
||||
if type == 1
|
||||
let prefix = source.'|'
|
||||
elseif type == 3
|
||||
let temps.input = tempname()
|
||||
call writefile(source, temps.input)
|
||||
let prefix = 'cat '.s:shellesc(temps.input).'|'
|
||||
else
|
||||
throw 'Invalid source type'
|
||||
endif
|
||||
else
|
||||
let prefix = ''
|
||||
endif
|
||||
let command = prefix.fzf_exec.' '.optstr.' > '.temps.result
|
||||
|
||||
if s:tmux_enabled() && s:tmux_splittable(dict)
|
||||
return s:execute_tmux(dict, command, temps)
|
||||
else
|
||||
return s:execute(dict, command, temps)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:tmux_splittable(dict)
|
||||
return
|
||||
\ min([&columns, get(a:dict, 'tmux_width', 0)]) >= s:min_tmux_width ||
|
||||
\ min([&lines, get(a:dict, 'tmux_height', get(a:dict, 'tmux', 0))]) >= s:min_tmux_height
|
||||
endfunction
|
||||
|
||||
function! s:pushd(dict)
|
||||
if !empty(get(a:dict, 'dir', ''))
|
||||
let a:dict.prev_dir = getcwd()
|
||||
execute 'chdir '.s:escape(a:dict.dir)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:popd(dict)
|
||||
if has_key(a:dict, 'prev_dir')
|
||||
execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:execute(dict, command, temps)
|
||||
call s:pushd(a:dict)
|
||||
silent !clear
|
||||
if has('gui_running')
|
||||
let launcher = get(a:dict, 'launcher', get(g:, 'fzf_launcher', s:launcher))
|
||||
let command = printf(launcher, "'".substitute(a:command, "'", "'\"'\"'", 'g')."'")
|
||||
else
|
||||
let command = a:command
|
||||
endif
|
||||
execute 'silent !'.command
|
||||
redraw!
|
||||
if v:shell_error
|
||||
" Do not print error message on exit status 1
|
||||
if v:shell_error > 1
|
||||
echohl ErrorMsg
|
||||
echo 'Error running ' . command
|
||||
endif
|
||||
return []
|
||||
else
|
||||
return s:callback(a:dict, a:temps, 0)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:env_var(name)
|
||||
if exists('$'.a:name)
|
||||
return a:name . "='". substitute(expand('$'.a:name), "'", "'\\\\''", 'g') . "' "
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:execute_tmux(dict, command, temps)
|
||||
let command = s:env_var('FZF_DEFAULT_OPTS').s:env_var('FZF_DEFAULT_COMMAND').a:command
|
||||
if !empty(get(a:dict, 'dir', ''))
|
||||
let command = 'cd '.s:escape(a:dict.dir).' && '.command
|
||||
endif
|
||||
|
||||
let splitopt = '-v'
|
||||
if has_key(a:dict, 'tmux_width')
|
||||
let splitopt = '-h'
|
||||
let size = a:dict.tmux_width
|
||||
else
|
||||
let size = get(a:dict, 'tmux_height', get(a:dict, 'tmux'))
|
||||
endif
|
||||
|
||||
if type(size) == 1 && size =~ '%$'
|
||||
let sizeopt = '-p '.size[0:-2]
|
||||
else
|
||||
let sizeopt = '-l '.size
|
||||
endif
|
||||
|
||||
let s:pane = substitute(
|
||||
\ system(
|
||||
\ printf(
|
||||
\ 'tmux split-window %s %s -P -F "#{pane_id}" %s',
|
||||
\ splitopt, sizeopt, s:shellesc(command))), '\n', '', 'g')
|
||||
let s:dict = a:dict
|
||||
let s:temps = a:temps
|
||||
|
||||
augroup fzf_tmux
|
||||
autocmd!
|
||||
autocmd VimResized * nested call s:tmux_check()
|
||||
augroup END
|
||||
endfunction
|
||||
|
||||
function! s:tmux_check()
|
||||
let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')
|
||||
|
||||
if index(panes, s:pane) < 0
|
||||
augroup fzf_tmux
|
||||
autocmd!
|
||||
augroup END
|
||||
|
||||
call s:callback(s:dict, s:temps, 1)
|
||||
redraw
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:callback(dict, temps, cd)
|
||||
if !filereadable(a:temps.result)
|
||||
let lines = []
|
||||
else
|
||||
if a:cd | call s:pushd(a:dict) | endif
|
||||
|
||||
let lines = readfile(a:temps.result)
|
||||
if has_key(a:dict, 'sink')
|
||||
for line in lines
|
||||
if type(a:dict.sink) == 2
|
||||
call a:dict.sink(line)
|
||||
else
|
||||
execute a:dict.sink.' '.s:escape(line)
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
endif
|
||||
|
||||
for tf in values(a:temps)
|
||||
silent! call delete(tf)
|
||||
endfor
|
||||
|
||||
call s:popd(a:dict)
|
||||
|
||||
return lines
|
||||
endfunction
|
||||
|
||||
function! s:cmd(bang, ...) abort
|
||||
let args = copy(a:000)
|
||||
let opts = {}
|
||||
if len(args) > 0 && isdirectory(expand(args[-1]))
|
||||
let opts.dir = remove(args, -1)
|
||||
endif
|
||||
if !a:bang
|
||||
let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height)
|
||||
endif
|
||||
call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))
|
||||
endfunction
|
||||
|
||||
command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)
|
||||
|
||||
let &cpo = s:cpo_save
|
||||
unlet s:cpo_save
|
||||
|
||||
|
37
test/fzf.vader
Normal file
37
test/fzf.vader
Normal file
@@ -0,0 +1,37 @@
|
||||
Execute (Setup):
|
||||
let g:dir = fnamemodify(g:vader_file, ':p:h')
|
||||
Log 'Test directory: ' . g:dir
|
||||
|
||||
Execute (fzf#run with dir option):
|
||||
let result = fzf#run({ 'options': '--filter=vdr', 'dir': g:dir })
|
||||
AssertEqual ['fzf.vader'], result
|
||||
|
||||
let result = sort(fzf#run({ 'options': '--filter e', 'dir': g:dir }))
|
||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
||||
|
||||
Execute (fzf#run with Funcref command):
|
||||
let g:ret = []
|
||||
function! g:proc(e)
|
||||
call add(g:ret, a:e)
|
||||
endfunction
|
||||
let result = sort(fzf#run({ 'sink': function('g:proc'), 'options': '--filter e', 'dir': g:dir }))
|
||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], result
|
||||
AssertEqual ['fzf.vader', 'test_fzf.rb'], sort(g:ret)
|
||||
|
||||
Execute (fzf#run with string source):
|
||||
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
||||
AssertEqual ['hi'], result
|
||||
|
||||
Execute (fzf#run with list source):
|
||||
let result = sort(fzf#run({ 'source': ['hello', 'world'], 'options': '-f e' }))
|
||||
AssertEqual ['hello'], result
|
||||
let result = sort(fzf#run({ 'source': ['hello', 'world'], 'options': '-f o' }))
|
||||
AssertEqual ['hello', 'world'], result
|
||||
|
||||
Execute (fzf#run with string source):
|
||||
let result = sort(fzf#run({ 'source': 'echo hi', 'options': '-f i' }))
|
||||
AssertEqual ['hi'], result
|
||||
|
||||
Execute (Cleanup):
|
||||
unlet g:dir
|
||||
Restore
|
614
test/test_fzf.rb
614
test/test_fzf.rb
@@ -1,59 +1,225 @@
|
||||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
|
||||
require 'rubygems'
|
||||
require 'curses'
|
||||
require 'timeout'
|
||||
require 'stringio'
|
||||
require 'minitest/autorun'
|
||||
require 'tempfile'
|
||||
$LOAD_PATH.unshift File.expand_path('../..', __FILE__)
|
||||
ENV['FZF_EXECUTABLE'] = '0'
|
||||
load 'fzf'
|
||||
|
||||
class MockTTY
|
||||
def initialize
|
||||
@buffer = ''
|
||||
@mutex = Mutex.new
|
||||
@condv = ConditionVariable.new
|
||||
end
|
||||
|
||||
def read_nonblock sz
|
||||
@mutex.synchronize do
|
||||
take sz
|
||||
end
|
||||
end
|
||||
|
||||
def take sz
|
||||
if @buffer.length >= sz
|
||||
ret = @buffer[0, sz]
|
||||
@buffer = @buffer[sz..-1]
|
||||
ret
|
||||
end
|
||||
end
|
||||
|
||||
def getc
|
||||
sleep 0.1
|
||||
while true
|
||||
@mutex.synchronize do
|
||||
if char = take(1)
|
||||
return char
|
||||
else
|
||||
@condv.wait(@mutex)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def << str
|
||||
@mutex.synchronize do
|
||||
@buffer << str
|
||||
@condv.broadcast
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class TestFZF < MiniTest::Unit::TestCase
|
||||
def setup
|
||||
ENV.delete 'FZF_DEFAULT_SORT'
|
||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||
ENV.delete 'FZF_DEFAULT_COMMAND'
|
||||
end
|
||||
|
||||
def test_default_options
|
||||
fzf = FZF.new []
|
||||
assert_equal 1000, fzf.sort
|
||||
assert_equal 1000, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal Regexp::IGNORECASE, fzf.rxflag
|
||||
assert_equal true, fzf.color
|
||||
assert_equal nil, fzf.rxflag
|
||||
assert_equal true, fzf.mouse
|
||||
assert_equal nil, fzf.nth
|
||||
assert_equal nil, fzf.with_nth
|
||||
assert_equal true, fzf.color
|
||||
assert_equal false, fzf.black
|
||||
assert_equal true, fzf.ansi256
|
||||
assert_equal '', fzf.query
|
||||
assert_equal false, fzf.select1
|
||||
assert_equal false, fzf.exit0
|
||||
assert_equal nil, fzf.filter
|
||||
assert_equal nil, fzf.extended
|
||||
assert_equal false, fzf.reverse
|
||||
assert_equal '> ', fzf.prompt
|
||||
assert_equal false, fzf.print_query
|
||||
end
|
||||
|
||||
begin
|
||||
ENV['FZF_DEFAULT_SORT'] = '1500'
|
||||
fzf = FZF.new []
|
||||
assert_equal 1500, fzf.sort
|
||||
ensure
|
||||
ENV.delete 'FZF_DEFAULT_SORT'
|
||||
end
|
||||
def test_environment_variables
|
||||
# Deprecated
|
||||
ENV['FZF_DEFAULT_SORT'] = '20000'
|
||||
fzf = FZF.new []
|
||||
assert_equal 20000, fzf.sort
|
||||
assert_equal nil, fzf.nth
|
||||
|
||||
ENV['FZF_DEFAULT_OPTS'] =
|
||||
'-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<
|
||||
'--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'
|
||||
fzf = FZF.new []
|
||||
assert_equal 10000, fzf.sort
|
||||
assert_equal ' hello world ',
|
||||
fzf.query
|
||||
assert_equal 'goodbye world',
|
||||
fzf.filter
|
||||
assert_equal :fuzzy, fzf.extended
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal true, fzf.black
|
||||
assert_equal false, fzf.mouse
|
||||
assert_equal true, fzf.select1
|
||||
assert_equal true, fzf.exit0
|
||||
assert_equal true, fzf.reverse
|
||||
assert_equal true, fzf.print_query
|
||||
assert_equal [2..2, -1..-1, 1..1], fzf.nth
|
||||
assert_equal [2..2, -3..-1, 1..1], fzf.with_nth
|
||||
end
|
||||
|
||||
def test_option_parser
|
||||
# Long opts
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal 0, fzf.rxflag
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1
|
||||
--exit-0 --filter=howdy --extended-exact
|
||||
--no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)
|
||||
--print-query]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal false, fzf.black
|
||||
assert_equal false, fzf.mouse
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query
|
||||
assert_equal true, fzf.select1
|
||||
assert_equal true, fzf.exit0
|
||||
assert_equal 'howdy', fzf.filter
|
||||
assert_equal :exact, fzf.extended
|
||||
assert_equal [0..0], fzf.nth
|
||||
assert_equal nil, fzf.with_nth
|
||||
assert_equal true, fzf.reverse
|
||||
assert_equal '(hi)', fzf.prompt
|
||||
assert_equal true, fzf.print_query
|
||||
|
||||
# Long opts (left-to-right)
|
||||
fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello
|
||||
--filter a --filter b --no-256 --black --nth -1 --nth -2
|
||||
--select-1 --exit-0 --no-select-1 --no-exit-0
|
||||
--no-sort -i --color --no-multi --256
|
||||
--reverse --no-reverse --prompt (hi) --prompt=(HI)
|
||||
--print-query --no-print-query]
|
||||
assert_equal nil, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal true, fzf.ansi256
|
||||
assert_equal true, fzf.black
|
||||
assert_equal true, fzf.mouse
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'b', fzf.filter
|
||||
assert_equal 'hello', fzf.query
|
||||
assert_equal false, fzf.select1
|
||||
assert_equal false, fzf.exit0
|
||||
assert_equal nil, fzf.extended
|
||||
assert_equal [-2..-2], fzf.nth
|
||||
assert_equal false, fzf.reverse
|
||||
assert_equal '(HI)', fzf.prompt
|
||||
assert_equal false, fzf.print_query
|
||||
|
||||
# Short opts
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal 0, fzf.rxflag
|
||||
fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]
|
||||
assert_equal 2000, fzf.sort
|
||||
assert_equal true, fzf.multi
|
||||
assert_equal false, fzf.color
|
||||
assert_equal false, fzf.ansi256
|
||||
assert_equal 0, fzf.rxflag
|
||||
assert_equal 'hello', fzf.query
|
||||
assert_equal 'howdy', fzf.filter
|
||||
assert_equal :fuzzy, fzf.extended
|
||||
assert_equal [2..2], fzf.nth
|
||||
assert_equal true, fzf.select1
|
||||
assert_equal true, fzf.exit0
|
||||
|
||||
# Left-to-right
|
||||
fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,5
|
||||
-s 3000 -c +m -i -q world +x -fworld -2 --black --no-black
|
||||
-1 -0 +1 +0
|
||||
]
|
||||
assert_equal 3000, fzf.sort
|
||||
assert_equal false, fzf.multi
|
||||
assert_equal true, fzf.color
|
||||
assert_equal true, fzf.ansi256
|
||||
assert_equal false, fzf.black
|
||||
assert_equal 1, fzf.rxflag
|
||||
assert_equal 'world', fzf.query
|
||||
assert_equal false, fzf.select1
|
||||
assert_equal false, fzf.exit0
|
||||
assert_equal 'world', fzf.filter
|
||||
assert_equal nil, fzf.extended
|
||||
assert_equal [3..3, 4..4], fzf.nth
|
||||
rescue SystemExit => e
|
||||
assert false, "Exited"
|
||||
end
|
||||
|
||||
def test_invalid_option
|
||||
[%w[-s 2000 +s], %w[yo dawg]].each do |argv|
|
||||
[
|
||||
%w[--unknown],
|
||||
%w[yo dawg],
|
||||
%w[--nth=0],
|
||||
%w[-n 0],
|
||||
%w[-n 1..2..3],
|
||||
%w[-n 1....],
|
||||
%w[-n ....3],
|
||||
%w[-n 1....3],
|
||||
%w[-n 1..0],
|
||||
%w[--nth ..0],
|
||||
].each do |argv|
|
||||
assert_raises(SystemExit) do
|
||||
fzf = FZF.new argv
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME Only on 1.9 or above
|
||||
def test_width
|
||||
fzf = FZF.new []
|
||||
assert_equal 5, fzf.width('abcde')
|
||||
assert_equal 4, fzf.width('한글')
|
||||
assert_equal 5, fzf.width('한글.')
|
||||
end
|
||||
end if RUBY_VERSION >= '1.9'
|
||||
|
||||
def test_trim
|
||||
fzf = FZF.new []
|
||||
@@ -66,7 +232,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)
|
||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)
|
||||
assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)
|
||||
end
|
||||
end if RUBY_VERSION >= '1.9'
|
||||
|
||||
def test_format
|
||||
fzf = FZF.new []
|
||||
@@ -139,15 +305,59 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
# TODO : partial_cache
|
||||
end
|
||||
|
||||
def test_fuzzy_matcher_rxflag
|
||||
assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag
|
||||
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')
|
||||
assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')
|
||||
assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')
|
||||
end
|
||||
|
||||
def test_fuzzy_matcher_case_sensitive
|
||||
# Smart-case match (Uppercase found)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
|
||||
# Smart-case match (Uppercase not-found)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort
|
||||
|
||||
# Case-sensitive match (-i)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
|
||||
# Case-insensitive match (+i)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::FuzzyMatcher.new(Regexp::IGNORECASE).
|
||||
match(%w[Fruit Grapefruit], 'Fruit', '', '').sort
|
||||
end
|
||||
|
||||
def test_extended_fuzzy_matcher_case_sensitive
|
||||
%w['Fruit Fruit$].each do |q|
|
||||
# Smart-case match (Uppercase found)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
|
||||
# Smart-case match (Uppercase not-found)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort
|
||||
|
||||
# Case-sensitive match (-i)
|
||||
assert_equal [['Fruit', [[0, 5]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
|
||||
# Case-insensitive match (+i)
|
||||
assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],
|
||||
FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).
|
||||
match(%w[Fruit Grapefruit], q, '', '').sort
|
||||
end
|
||||
end
|
||||
|
||||
def test_extended_fuzzy_matcher
|
||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE
|
||||
list = %w[
|
||||
@@ -184,6 +394,11 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
assert_equal list.length, match.call('j', '').length
|
||||
assert_equal list.length - 1, match.call('^j', '').length
|
||||
|
||||
# ^ + $
|
||||
assert_equal 0, match.call('^juici$', '').length
|
||||
assert_equal 1, match.call('^juice$', '').length
|
||||
assert_equal 0, match.call('^.*$', '').length
|
||||
|
||||
# !
|
||||
assert_equal 0, match.call('!j', '').length
|
||||
|
||||
@@ -250,7 +465,7 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
["0____1", [[0, 6]]],
|
||||
["0_____1", [[0, 7]]],
|
||||
["0______1", [[0, 8]]]],
|
||||
FZF.new([]).sort_by_rank(matcher.match(list, '01', '', '')))
|
||||
FZF.sort(matcher.match(list, '01', '', '')))
|
||||
|
||||
assert_equal(
|
||||
[["01", [[0, 1], [1, 2]]],
|
||||
@@ -261,46 +476,335 @@ class TestFZF < MiniTest::Unit::TestCase
|
||||
["____0_1", [[4, 5], [6, 7]]],
|
||||
["0______1", [[0, 1], [7, 8]]],
|
||||
["___01___", [[3, 4], [4, 5]]]],
|
||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '0 1', '', '')))
|
||||
FZF.sort(xmatcher.match(list, '0 1', '', '')))
|
||||
|
||||
assert_equal(
|
||||
[["_01_", [[1, 3], [0, 4]]],
|
||||
["0____1", [[0, 6], [1, 3]]],
|
||||
["0_____1", [[0, 7], [1, 3]]],
|
||||
["0______1", [[0, 8], [1, 3]]],
|
||||
["___01___", [[3, 5], [0, 2]]],
|
||||
["____0_1", [[4, 7], [0, 2]]]],
|
||||
FZF.new([]).sort_by_rank(xmatcher.match(list, '01 __', '', '')))
|
||||
[["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],
|
||||
["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],
|
||||
["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],
|
||||
["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],
|
||||
["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],
|
||||
["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],
|
||||
FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|
|
||||
tuple << FZF.rank(tuple)
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
if RUBY_PLATFORM =~ /darwin/
|
||||
NFD = '한글'
|
||||
def test_nfc
|
||||
assert_equal 6, NFD.length
|
||||
assert_equal ["한글", [[0, 1], [1, 2]]],
|
||||
FZF::UConv.nfc(NFD, [[0, 3], [3, 6]])
|
||||
def test_extended_exact_mode
|
||||
exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact
|
||||
fuzzy = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy
|
||||
list = %w[
|
||||
extended-exact-mode-not-fuzzy
|
||||
extended'-fuzzy-mode
|
||||
]
|
||||
assert_equal 2, fuzzy.match(list, 'extended', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, 'mode extended', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, 'xtndd', '', '').length
|
||||
assert_equal 2, fuzzy.match(list, "'-fuzzy", '', '').length
|
||||
|
||||
nfd2 = 'before' + NFD + 'after'
|
||||
assert_equal 6 + 6 + 5, nfd2.length
|
||||
assert_equal 2, exact.match(list, 'extended', '', '').length
|
||||
assert_equal 2, exact.match(list, 'mode extended', '', '').length
|
||||
assert_equal 0, exact.match(list, 'xtndd', '', '').length
|
||||
assert_equal 1, exact.match(list, "'-fuzzy", '', '').length
|
||||
assert_equal 2, exact.match(list, "-fuzzy", '', '').length
|
||||
end
|
||||
|
||||
nfc, offsets = FZF::UConv.nfc(nfd2, [[4, 14], [9, 13]])
|
||||
o1, o2 = offsets
|
||||
assert_equal 'before한글after', nfc
|
||||
assert_equal 're한글af', nfc[(o1.first...o1.last)]
|
||||
assert_equal '글a', nfc[(o2.first...o2.last)]
|
||||
end
|
||||
# ^$ -> matches empty item
|
||||
def test_format_empty_item
|
||||
fzf = FZF.new []
|
||||
item = ['', [[0, 0]]]
|
||||
line, offsets = item
|
||||
tokens = fzf.format line, 80, offsets
|
||||
assert_equal [], tokens
|
||||
end
|
||||
|
||||
def test_nfd
|
||||
nfc = '한글'
|
||||
nfd = FZF::UConv.nfd(nfc)
|
||||
assert_equal 6, nfd.length
|
||||
assert_equal NFD, nfd
|
||||
def test_mouse_event
|
||||
interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL
|
||||
me = FZF::MouseEvent.new nil
|
||||
me.v = 10
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal false, me.double?(20)
|
||||
me.v = 20
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal false, me.double?(20)
|
||||
me.v = 20
|
||||
assert_equal false, me.double?(10)
|
||||
assert_equal true, me.double?(20)
|
||||
sleep interval
|
||||
assert_equal false, me.double?(20)
|
||||
end
|
||||
|
||||
def test_nth_match
|
||||
list = [
|
||||
' first second third',
|
||||
'fourth fifth sixth',
|
||||
]
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE
|
||||
assert_equal list, matcher.match(list, 'f', '', '').map(&:first)
|
||||
assert_equal [
|
||||
[list[0], [[2, 5]]],
|
||||
[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1]
|
||||
assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2]
|
||||
assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')
|
||||
|
||||
# Comma-separated
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2, 0..0]
|
||||
assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||
|
||||
# Ordered
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0, 2..2]
|
||||
assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')
|
||||
|
||||
regex = FZF.build_delim_regex "\t"
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex
|
||||
assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex
|
||||
assert_equal [], matcher.match(list, 'r', '', '')
|
||||
assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||
|
||||
# Negative indexing
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1..-1], regex
|
||||
assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')
|
||||
assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')
|
||||
|
||||
# Regex delimiter
|
||||
regex = FZF.build_delim_regex "[ \t]+"
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex
|
||||
assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex
|
||||
assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')
|
||||
end
|
||||
|
||||
def test_nth_match_range
|
||||
list = [
|
||||
' first second third',
|
||||
'fourth fifth sixth',
|
||||
]
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..2]
|
||||
assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')
|
||||
assert_equal [], matcher.match(list, 'fo', '', '')
|
||||
|
||||
matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..-1, 0..0]
|
||||
assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')
|
||||
assert_equal [[list[1], [[0, 2]]]], matcher.match(list, 'fo', '', '')
|
||||
|
||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..0, 1..2]
|
||||
assert_equal [], matcher.match(list, '^t', '', '')
|
||||
|
||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..1, 2..2]
|
||||
assert_equal [[list[0], [[16, 17]]]], matcher.match(list, '^t', '', '')
|
||||
|
||||
matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [1..-1]
|
||||
assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')
|
||||
end
|
||||
|
||||
def stream_for str, delay = 0
|
||||
StringIO.new(str).tap do |sio|
|
||||
sio.instance_eval do
|
||||
alias org_gets gets
|
||||
|
||||
def gets
|
||||
org_gets.tap { |e| sleep(@delay) unless e.nil? }
|
||||
end
|
||||
|
||||
def reopen _
|
||||
end
|
||||
end
|
||||
sio.instance_variable_set :@delay, delay
|
||||
end
|
||||
end
|
||||
|
||||
def test_split
|
||||
assert_equal ["a", "b", "c", "\xFF", "d", "e", "f"],
|
||||
FZF::UConv.split("abc\xFFdef")
|
||||
def assert_fzf_output opts, given, expected
|
||||
stream = stream_for given
|
||||
output = stream_for ''
|
||||
|
||||
def sorted_lines line
|
||||
line.split($/).sort
|
||||
end
|
||||
|
||||
begin
|
||||
tty = MockTTY.new
|
||||
$stdout = output
|
||||
fzf = FZF.new(opts, stream)
|
||||
fzf.instance_variable_set :@tty, tty
|
||||
thr = block_given? && Thread.new { yield tty }
|
||||
fzf.start
|
||||
thr && thr.join
|
||||
rescue SystemExit => e
|
||||
assert_equal 0, e.status
|
||||
assert_equal sorted_lines(expected), sorted_lines(output.string)
|
||||
ensure
|
||||
$stdout = STDOUT
|
||||
end
|
||||
end
|
||||
|
||||
def test_filter
|
||||
{
|
||||
%w[--filter=ol] => 'World',
|
||||
%w[--filter=ol --print-query] => "ol\nWorld",
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1
|
||||
{
|
||||
%w[--query=ol --select-1] => 'World',
|
||||
%w[--query=ol --select-1 --print-query] => "ol\nWorld",
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_select_1_without_query
|
||||
assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'
|
||||
end
|
||||
|
||||
def test_select_1_ambiguity
|
||||
begin
|
||||
Timeout::timeout(0.5) do
|
||||
assert_fzf_output %w[--query=o --select-1], "hello\nworld", "should not match"
|
||||
end
|
||||
rescue Timeout::Error
|
||||
Curses.close_screen
|
||||
end
|
||||
end
|
||||
|
||||
def test_exit_0
|
||||
{
|
||||
%w[--query=zz --exit-0] => '',
|
||||
%w[--query=zz --exit-0 --print-query] => 'zz',
|
||||
}.each do |opts, expected|
|
||||
assert_fzf_output opts, "Hello\nWorld", expected
|
||||
end
|
||||
end
|
||||
|
||||
def test_exit_0_without_query
|
||||
assert_fzf_output %w[--exit-0], '', ''
|
||||
end
|
||||
|
||||
def test_with_nth
|
||||
source = "hello world\nbatman"
|
||||
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],
|
||||
source, 'hello world'
|
||||
assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],
|
||||
source, 'hello world'
|
||||
assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],
|
||||
source, ''
|
||||
assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],
|
||||
source, 'hello world'
|
||||
assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],
|
||||
source, 'batman'
|
||||
end
|
||||
|
||||
def test_with_nth_transform
|
||||
fzf = FZF.new %w[--with-nth 2..,1]
|
||||
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||
assert_equal 'my world hello', fzf.transform('hello my world')
|
||||
assert_equal 'my world hello', fzf.transform('hello my world ')
|
||||
|
||||
fzf = FZF.new %w[--with-nth 2,-1,2]
|
||||
assert_equal 'my world my', fzf.transform('hello my world')
|
||||
assert_equal 'world world world', fzf.transform('hello world')
|
||||
assert_equal 'world world world', fzf.transform('hello world ')
|
||||
end
|
||||
|
||||
def test_ranking_overlap_match_regions
|
||||
list = [
|
||||
'1 3 4 2',
|
||||
'1 2 3 4'
|
||||
]
|
||||
assert_equal [
|
||||
['1 2 3 4', [[0, 13], [16, 22]]],
|
||||
['1 3 4 2', [[0, 24], [12, 17]]],
|
||||
], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))
|
||||
end
|
||||
|
||||
def test_constrain
|
||||
fzf = FZF.new []
|
||||
|
||||
# [#**** ]
|
||||
assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)
|
||||
|
||||
# *****[**#** ... ] => [**#******* ... ]
|
||||
assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)
|
||||
|
||||
# [**********]**#** => ***[*********#]**
|
||||
assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)
|
||||
|
||||
# *****[**#** ] => ***[**#****]
|
||||
assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)
|
||||
|
||||
# *****[**#** ] => ****[**#***]
|
||||
assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)
|
||||
|
||||
# ***** [#] => ****[#]
|
||||
assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)
|
||||
|
||||
# [ ] #**** => [#]****
|
||||
assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)
|
||||
|
||||
# [ ] **#** => **[#]**
|
||||
assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)
|
||||
|
||||
# [***** #] => [****# ]
|
||||
assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)
|
||||
|
||||
# **[***** #] => [******# ]
|
||||
assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)
|
||||
end
|
||||
|
||||
def test_invalid_utf8
|
||||
tmp = Tempfile.new('fzf')
|
||||
tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*')
|
||||
tmp.close
|
||||
begin
|
||||
Timeout::timeout(0.5) do
|
||||
FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start
|
||||
end
|
||||
rescue Timeout::Error
|
||||
Curses.close_screen
|
||||
end
|
||||
ensure
|
||||
tmp.unlink
|
||||
end
|
||||
|
||||
def test_with_nth_mock_tty
|
||||
# Manual selection with input
|
||||
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||
tty << "world"
|
||||
tty << "hell"
|
||||
tty << "\r"
|
||||
end
|
||||
|
||||
# Manual selection without input
|
||||
assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|
|
||||
tty << "\r"
|
||||
end
|
||||
|
||||
# Manual selection with input and --multi
|
||||
lines = "hello world\ngoodbye world"
|
||||
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||
tty << "o"
|
||||
tty << "\e[Z\e[Z"
|
||||
tty << "\r"
|
||||
end
|
||||
|
||||
# Manual selection without input and --multi
|
||||
assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|
|
||||
tty << "\e[Z\e[Z"
|
||||
tty << "\r"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
78
uninstall
Executable file
78
uninstall
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
|
||||
confirm() {
|
||||
while [ 1 ]; do
|
||||
read -p "$1" -n 1 -r
|
||||
echo
|
||||
if [[ "$REPLY" =~ ^[Yy] ]]; then
|
||||
return 0
|
||||
elif [[ "$REPLY" =~ ^[Nn] ]]; then
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
remove() {
|
||||
echo "Remove $1"
|
||||
rm -f "$1"
|
||||
}
|
||||
|
||||
remove_line() {
|
||||
src=$(readlink "$1")
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Remove from $1 ($src):"
|
||||
else
|
||||
src=$1
|
||||
echo "Remove from $1:"
|
||||
fi
|
||||
|
||||
shift
|
||||
line_no=1
|
||||
match=0
|
||||
while [ -n "$1" ]; do
|
||||
line=$(sed -n "$line_no,\$p" "$src" | \grep -m1 -nF "$1")
|
||||
if [ $? -ne 0 ]; then
|
||||
shift
|
||||
line_no=1
|
||||
continue
|
||||
fi
|
||||
line_no=$(( $(sed 's/:.*//' <<< "$line") + line_no - 1 ))
|
||||
content=$(sed 's/^[0-9]*://' <<< "$line")
|
||||
match=1
|
||||
echo " - Line #$line_no: $content"
|
||||
[ "$content" = "$1" ] || confirm " - Remove (y/n) ? "
|
||||
if [ $? -eq 0 ]; then
|
||||
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
|
||||
mv "$src.bak" "$src" || break
|
||||
echo " - Removed"
|
||||
else
|
||||
echo " - Skipped"
|
||||
line_no=$(( line_no + 1 ))
|
||||
fi
|
||||
done
|
||||
[ $match -eq 0 ] && echo " - Nothing found"
|
||||
echo
|
||||
}
|
||||
|
||||
for shell in bash zsh; do
|
||||
remove ~/.fzf.${shell}
|
||||
remove_line ~/.${shell}rc \
|
||||
"[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" \
|
||||
"source ~/.fzf.${shell}"
|
||||
done
|
||||
|
||||
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
||||
if [ -f "$bind_file" ]; then
|
||||
remove_line "$bind_file" "fzf_key_bindings"
|
||||
fi
|
||||
|
||||
if [ -d ~/.config/fish/functions ]; then
|
||||
remove ~/.config/fish/functions/fzf.fish
|
||||
|
||||
if [ "$(ls -A ~/.config/fish/functions)" ]; then
|
||||
echo "Can't delete non-empty directory: \"~/.config/fish/functions\""
|
||||
else
|
||||
rmdir ~/.config/fish/functions
|
||||
fi
|
||||
fi
|
||||
|
Reference in New Issue
Block a user