Compare commits

..

24 Commits
0.4.2 ... 0.5.0

Author SHA1 Message Date
Junegunn Choi
39af56cf8f Revert "Reduce the number of Curses.refresh calls"
This reverts commit 2d3a0a1034
(which doesn't make any noticeable difference)
2013-11-24 20:40:23 +09:00
Junegunn Choi
2d3a0a1034 Reduce the number of Curses.refresh calls 2013-11-24 13:40:02 +09:00
Junegunn Choi
655fa5d9aa Update query line after update_list call
This commit is the workaround for the curses issue where the query
string on the screen is truncated after the cursor when the list is
updated: e.g. `aaac|bbb`
2013-11-24 12:56:26 +09:00
Junegunn Choi
9a49a29c7f Fix bash completion (~/abc/def/ghi**)
~/abc/def/ghi** should match ghi under ~/abc/def/, not ~/abc/def*
2013-11-23 20:37:53 +09:00
Junegunn Choi
89ae45cda4 Merge branch 'master' of github.com:junegunn/fzf
Conflicts:
	fzf-completion.bash
2013-11-23 20:16:46 +09:00
Junegunn Choi
f660ad35b2 Improve bash completion: [DIRECTORY/][FUZZY_PATTERN]**<TAB> 2013-11-23 20:12:14 +09:00
Junegunn Choi
c61738ae43 Bump up gem version 2013-11-23 20:09:02 +09:00
Junegunn Choi
c4dec4d34b Add -q option (initial query) 2013-11-23 19:21:02 +09:00
Junegunn Choi
a797604255 -o default as well as -o bashdefault 2013-11-21 11:38:14 +09:00
Junegunn Choi
25840d3bc7 -o bashdefault instead of -o default 2013-11-21 10:49:23 +09:00
Junegunn Choi
4745d50931 Add CTRL-G and ESC (C-[) as abort key (#7) 2013-11-20 21:18:51 +09:00
Junegunn Choi
04bf3abe99 Fix bash completion example 2013-11-20 15:22:25 +09:00
Junegunn Choi
57f7963eee Remove obsolete lines 2013-11-20 14:02:29 +09:00
Junegunn Choi
2fa21e5dd6 Remove obsolete lines 2013-11-20 14:01:13 +09:00
Junegunn Choi
9c4c37aa36 Adjust completion types (all/file/dir) 2013-11-20 12:28:41 +09:00
Junegunn Choi
2540c9062f The last argument doesn't have to be a path 2013-11-20 10:46:53 +09:00
Junegunn Choi
f28274109f Update Vim plugin to take path argument 2013-11-20 10:31:33 +09:00
Junegunn Choi
724724bd8c Extend the list of commands for fzf-completion 2013-11-20 02:23:30 +09:00
Junegunn Choi
64541cb5f8 Fix install script (source ~/.xxxrc has no effect) 2013-11-20 02:10:19 +09:00
Junegunn Choi
179b00ed6c Reload .bashrc/.zshrc after installation 2013-11-20 01:57:24 +09:00
Junegunn Choi
a9fd496691 Merge pull request #6 from junegunn/completion
Prototype implementation of bash auto-completion
2013-11-19 08:44:30 -08:00
Junegunn Choi
b14c57e656 Update README 2013-11-20 01:42:57 +09:00
Junegunn Choi
fa5617e076 Implement bash auto-completion with fzf 2013-11-20 01:29:36 +09:00
Junegunn Choi
e52a1d5fad Update bash example 2013-11-18 17:36:54 +09:00
8 changed files with 385 additions and 75 deletions

123
README.md
View File

@@ -16,8 +16,24 @@ 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.
### Using install script
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
~/.fzf/install
```
The script will add an alias to fzf and auto-completion support to your
`.bashrc` and `.zshrc`.
### Manual installation
Or you can just download
[fzf executable](https://raw.github.com/junegunn/fzf/master/fzf) and put it
somewhere in your search $PATH.
```sh
mkdir -p ~/bin
@@ -25,20 +41,6 @@ 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
[install](https://github.com/junegunn/fzf/blob/master/install) script.
```sh
git clone https://github.com/junegunn/fzf.git
fzf/install
```
Make sure that ~/bin is included in $PATH.
```sh
export PATH=$PATH:~/bin
```
### Install as Ruby gem
fzf can be installed as a Ruby gem
@@ -71,12 +73,13 @@ 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
-m, --multi Enable multi-select
-x, --extended Extended-search mode
-q, --query=STR Initial query
-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
```
fzf will launch curses-based finder, read the list from STDIN, and write the
@@ -104,7 +107,7 @@ history | fzf +s
### Key binding
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.
@@ -138,8 +141,14 @@ Usage as Vim plugin
If you install fzf as a Vim plugin, `:FZF` command will be added.
```vim
" Look for files under current directory
:FZF
:FZF --no-sort -m
" Look for files under your home directory
:FZF ~
" With options
:FZF --no-sort -m /tmp
```
You can override the source command which produces input to fzf.
@@ -183,6 +192,14 @@ fda() {
DIR=$(find ${1:-*} -type d 2> /dev/null | fzf) && cd "$DIR"
}
# fsel - Select multiple files in the given path
fsel() {
find ${1:-*} | fzf -m | while read item; do
echo -n "\"$item\" "
done
echo
}
# fh - repeat history
fh() {
eval $(history | fzf +s | sed 's/ *[0-9]* *//')
@@ -207,7 +224,7 @@ zsh widgets
-----------
```sh
# CTRL-T - Paste the selected file(s) path into the command line
# CTRL-T - Paste the selected file path(s) into the command line
fzf-file-widget() {
local FILES
local IFS="
@@ -242,6 +259,59 @@ zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
```
Auto-completion (experimental)
------------------------------
Disclaimer: *Auto-completion feature is currently experimental, it can change
over time*
### bash
Fuzzy completion can be triggered if the word before the cursor ends
with the trigger sequence which is by default `**`.
- `COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>`
#### Examples
```sh
# Files under current directory
# - You can select multiple items with TAB key
vim **<TAB>
# Files under parent directory
vim ../**<TAB>
# 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>
```
#### 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.)
Tips
----
@@ -261,6 +331,9 @@ Define fzf alias with the option as follows:
alias fzf='ruby --disable-gems ~/bin/fzf'
```
This is automatically set up in your .bashrc and .zshrc if you use the bundled
[install](https://github.com/junegunn/fzf/blob/master/install) script.
### Incorrect display on Ruby 1.8
It is reported that the output of fzf can become unreadable on some terminals

80
fzf
View File

@@ -10,7 +10,7 @@
# URL: https://github.com/junegunn/fzf
# Author: Junegunn Choi
# License: MIT
# Last update: November 17, 2013
# Last update: November 24, 2013
#
# Copyright (c) 2013 Junegunn Choi
#
@@ -41,7 +41,7 @@ require 'set'
class FZF
C = Curses
attr_reader :rxflag, :sort, :color, :multi
attr_reader :rxflag, :sort, :color, :multi, :query
class AtomicVar
def initialize value
@@ -67,20 +67,36 @@ class FZF
end
def initialize argv, source = $stdin
usage 0 unless (%w[--help -h] & argv).empty?
@rxflag = argv.delete('+i') ? 0 : Regexp::IGNORECASE
@sort = %w[+s --no-sort].map { |e| argv.delete e }.compact.empty? ?
ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i : nil
@color = %w[+c --no-color].map { |e| argv.delete e }.compact.empty?
@multi = !%w[-m --multi].map { |e| argv.delete e }.compact.empty?
@xmode = !%w[-x --extended].map { |e| argv.delete e }.compact.empty?
rest = argv.join ' '
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
usage 1 unless @sort
@sort = sort[2].to_i
rest = rest.delete sort[0]
@rxflag = Regexp::IGNORECASE
@sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i
@color = true
@multi = false
@xmode = false
argv = argv.dup
while o = argv.shift
case o
when '-h', '--help' then usage 0
when '-m', '--multi' then @multi = true
when '-x', '--extended' then @xmode = true
when '+i' then @rxflag = 0
when '+s', '--no-sort' then @sort = nil
when '+c', '--no-color' then @color = false
when '-q', '--query'
usage 1, 'query string required' unless query = argv.shift
@query = AtomicVar.new query.dup
when /^-q(.*)$/, /^--query=(.*)$/
@query = AtomicVar.new($1)
when '-s', '--sort'
usage 1, 'sort size required' unless sort = argv.shift
usage 1, 'invalid sort size' unless sort =~ /^[0-9]+$/
@sort = sort.to_i
when /^-s([0-9]+)$/, /^--sort=([0-9]+)$/
@sort = $1.to_i
else
usage 1, "illegal option: #{o}"
end
end
usage 1 unless rest.empty?
@source = source
@mtx = Mutex.new
@@ -88,8 +104,8 @@ class FZF
@events = {}
@new = []
@queue = Queue.new
@cursor_x = AtomicVar.new(0)
@query = AtomicVar.new('')
@query ||= AtomicVar.new('')
@cursor_x = AtomicVar.new(@query.length)
@matches = AtomicVar.new([])
@count = AtomicVar.new(0)
@vcursor = AtomicVar.new(0)
@@ -111,11 +127,13 @@ class FZF
start_loop
end
def usage x
def usage x, message = nil
$stderr.puts message if message
$stderr.puts %[usage: fzf [options]
-m, --multi Enable multi-select
-x, --extended Extended-search mode
-q, --query=STR Initial query
-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
@@ -612,6 +630,7 @@ class FZF
print_item row, tokens, chosen, selected
end
print_info
print_input
end
end
@@ -637,14 +656,13 @@ class FZF
got = nil
begin
tty = IO.open(IO.sysopen('/dev/tty'), 'r')
input = ''
cursor = 0
input = @query.get.dup
cursor = input.length
backword = proc {
cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
}
actions = {
:nop => proc { nil },
ctrl(:c) => proc { exit 1 },
:esc => proc { exit 1 },
ctrl(:d) => proc { exit 1 if input.empty? },
ctrl(:m) => proc {
got = pick
@@ -688,30 +706,30 @@ class FZF
actions[ctrl(:h)] = actions[127]
actions[ctrl(:n)] = actions[ctrl(:j)]
actions[ctrl(:p)] = actions[ctrl(:k)]
actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]
actions[:stab] = actions[9]
emit(:key) { [@query.get, cursor] } unless @query.empty?
while true
@cursor_x.set cursor
render { print_input }
ord = tty.getc.ord
ord =
case ord = tty.getc.ord
case ord = (tty.read_nonblock(1).ord rescue :esc)
when 91
case tty.getc.ord
case (tty.read_nonblock(1).ord rescue nil)
when 68 then :left
when 67 then :right
when 66 then ctrl(:j)
when 65 then ctrl(:k)
when 90 then :stab
else :nop
else next
end
when 'b'.ord
:alt_b
when 'f'.ord
:alt_f
else
ord
when 'b'.ord then :alt_b
when 'f'.ord then :alt_f
when :esc then :esc
else next
end if ord == 27
upd = actions.fetch(ord, proc { |ord|

116
fzf-completion.bash Normal file
View File

@@ -0,0 +1,116 @@
#!/bin/bash
# ____ ____
# / __/___ / __/
# / /_/_ / / /_
# / __/ / /_/ __/
# /_/ /___/_/-completion.bash
#
# - $FZF_COMPLETION_TRIGGER (default: '**')
# - $FZF_COMPLETION_OPTS (default: empty)
_fzf_opts_completion() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-m --multi -x --extended -s --sort +s +i +c --no-color"
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_generic_completion() {
local cur prev opts base dir leftover matches
COMPREPLY=()
FZF_COMPLETION_TRIGGER=${FZF_COMPLETION_TRIGGER:-**}
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${cur} == *"$FZF_COMPLETION_TRIGGER" ]]; then
base=${cur:0:${#cur}-${#FZF_COMPLETION_TRIGGER}}
eval base=$base
dir="$base"
while [ 1 ]; do
if [ -z "$dir" -o -d "$dir" ]; then
leftover=${base/#"$dir"}
leftover=${leftover/#\/}
[ "$dir" = './' ] && dir=''
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
if [[ ${item} =~ \ ]]; then
echo -n "\"$item\" "
else
echo -n "$item "
fi
done)
matches=${matches% }
if [ -n "$matches" ]; then
COMPREPLY=( "$matches" )
else
COMPREPLY=( "$cur" )
fi
return 0
fi
dir=$(dirname "$dir")
[[ "$dir" =~ /$ ]] || dir="$dir"/
done
fi
}
_fzf_all_completion() {
_fzf_generic_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_generic_completion \
"-name .git -prune -o -name .svn -prune -o -type f -print -o -type l -print" \
"-m"
}
_fzf_dir_completion() {
_fzf_generic_completion \
"-name .git -prune -o -name .svn -prune -o -type d -print" \
""
}
complete -F _fzf_opts_completion fzf
# Directory
for cmd in "cd pushd rmdir"; do
complete -F _fzf_dir_completion -o default -o bashdefault $cmd
done
# File
for cmd in "
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"; do
complete -F _fzf_file_completion -o default -o bashdefault $cmd
done
# Anything
for cmd in "
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"; do
complete -F _fzf_all_completion -o default -o bashdefault $cmd
done
bind '"\e\e": complete'
bind '"\er": redraw-current-line'
bind '"\C-i": "\e\e\er"'

9
fzf-completion.zsh Normal file
View File

@@ -0,0 +1,9 @@
#!/bin/zsh
# ____ ____
# / __/___ / __/
# / /_/_ / / /_
# / __/ / /_/ __/
# /_/ /___/_/-completion.zsh
#
# TODO

View File

@@ -1,7 +1,7 @@
# coding: utf-8
Gem::Specification.new do |spec|
spec.name = 'fzf'
spec.version = '0.4.2'
spec.version = '0.5.0'
spec.authors = ['Junegunn Choi']
spec.email = ['junegunn.c@gmail.com']
spec.description = %q{Fuzzy finder for your shell}

75
install
View File

@@ -1,7 +1,76 @@
#!/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
echo "OK"
# Curses-support
echo -n "Checking Curses support ... "
/usr/bin/env ruby -e "begin; require 'curses'; rescue Exception; exit 1; end"
if [ $? -ne 0 ]; then
echo "Your ruby does not support 'curses'"
exit 1
fi
echo "OK"
# Ruby version
echo -n "Checking Ruby version ... "
/usr/bin/env ruby -e 'exit RUBY_VERSION >= "1.9"'
if [ $? -eq 0 ]; then
echo ">= 1.9"
FZF_ALIAS="alias fzf='$RUBY --disable-gems $FZF_BASE/fzf'"
else
echo "< 1.9"
FZF_ALIAS="alias fzf='$RUBY $FZF_BASE/fzf' # fzf"
fi
# Auto-completion
read -p "Do you want to add auto-completion support? (y/n) " -n 1 -r
echo
[[ ! $REPLY =~ ^[Nn]$ ]]
AUTO_COMPLETION=$?
echo
for shell in bash zsh; do
rc=~/.${shell}rc
echo "Update $rc:"
# Install fzf alias
echo "- Add fzf alias:"
echo " - $FZF_ALIAS"
if [ $(grep "alias fzf=" $rc | wc -l) -gt 0 ]; then
echo " - (X) fzf alias already exists"
else
echo $FZF_ALIAS >> $rc
echo " - Added."
fi
# Install auto-completion support
if [ $AUTO_COMPLETION -eq 0 ]; then
FZF_COMPLETION="source $FZF_BASE/fzf-completion.${shell}"
echo "- Add auto-completion support"
echo " - $FZF_COMPLETION"
if [ $(grep "source.*fzf-completion" $rc | wc -l) -gt 0 ]; then
echo " - (X) fzf-completion.${shell} already being sourced"
else
echo $FZF_COMPLETION >> $rc
echo " - Added."
fi
fi
echo
done
echo "Finished. Reload your .bashrc or .zshrc to take effect."
echo " source ~/.bashrc # bash"
echo " source ~/.zshrc # zsh"
echo
echo "To uninstall fzf, simply remove the added lines."

View File

@@ -23,25 +23,37 @@
let s:exec = expand('<sfile>:h:h').'/fzf'
function! fzf#run(command, args)
function! s:escape(path)
return substitute(a:path, ' ', '\\ ', 'g')
endfunction
function! fzf#run(command, ...)
let cwd = getcwd()
try
let args = copy(a:000)
if len(args) > 0 && isdirectory(expand(args[-1]))
let dir = remove(args, -1)
execute 'chdir '.s:escape(dir)
endif
let argstr = join(args)
let tf = tempname()
let prefix = exists('g:fzf_source') ? g:fzf_source.'|' : ''
let fzf = executable(s:exec) ? s:exec : 'fzf'
let options = empty(a:args) ? get(g:, 'fzf_options', '') : a:args
let options = empty(argstr) ? get(g:, 'fzf_options', '') : argstr
execute "silent !".prefix.fzf.' '.options." > ".tf
if !v:shell_error
for line in readfile(tf)
if !empty(line)
execute a:command.' '.line
execute a:command.' '.s:escape(line)
endif
endfor
endif
finally
execute 'chdir '.s:escape(cwd)
redraw!
silent! call delete(tf)
endtry
endfunction
command! -nargs=* FZF call fzf#run('silent e', <q-args>)
command! -nargs=* -complete=dir FZF call fzf#run('silent e', <f-args>)

View File

@@ -25,22 +25,35 @@ class TestFZF < MiniTest::Unit::TestCase
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]
assert_equal 2000, fzf.sort
assert_equal true, fzf.multi
assert_equal false, fzf.color
assert_equal 0, fzf.rxflag
assert_equal 'hello', fzf.query.get
# 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[-s 2000 +c -m +i -qhello]
assert_equal 2000, fzf.sort
assert_equal true, fzf.multi
assert_equal false, fzf.color
assert_equal 0, fzf.rxflag
assert_equal 'hello', fzf.query.get
# Left-to-right
fzf = FZF.new %w[-qhello -s 2000 --no-sort -q world]
assert_equal nil, fzf.sort
assert_equal 'world', fzf.query.get
fzf = FZF.new %w[--query hello +s -s 2000 --query=world]
assert_equal 2000, fzf.sort
assert_equal 'world', fzf.query.get
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]].each do |argv|
assert_raises(SystemExit) do
fzf = FZF.new argv
end