Skip to content
Myr

Replace "default find command" with built-in directory traversal

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Junegunn Choi · committed by Junegunn Choi · parent c65d11b · Browse files · 9 files+57 added−82 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+57 added−82 removed

  • other:+57 added−82 removed

9 files+57 added−82 removed

modified:doc/fzf.txt+10 added-21 removed
1fzf.txt fzf Last change: January 1 20241fzf.txt fzf Last change: February 15 20242FZF - TABLE OF CONTENTS *fzf* *fzf-toc*3==============================================================================45 FZF Vim integration |fzf-vim-integration|
257 unmodified lines
263>264 call fzf#run({'sink': 'e'})265<266We haven't specified the `source`, so this is equivalent to starting fzf on267command line without standard input pipe; fzf will use find command (or268`$FZF_DEFAULT_COMMAND` if defined) to list the files under the current269directory. When you select one, it will open it with the sink, `:e` command.270If you want to open it in a new tab, you can pass `:tabedit` command instead271as the sink.267command line without standard input pipe; fzf will traverse the file system268under the current directory to get the list of files. (If269`$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command270instead.) When you select one, it will open it with the sink, `:e` command. If271you want to open it in a new tab, you can pass `:tabedit` command instead as272the sink.273>274 call fzf#run({'sink': 'tabedit'})275<275Instead of using the default find command, you can use any shell command as276the source. The following example will list the files managed by git. It's277equivalent to running `git ls-files | fzf` on shell.276You can use any shell command as the source to generate the list. The277following example will list the files managed by git. It's equivalent to278running `git ls-files | fzf` on shell.279>280 call fzf#run({'source': 'git ls-files', 'sink': 'e'})281<282fzf options can be specified as `options` entry in spec dictionary.
134 unmodified lines
417418< fzf inside terminal buffer >________________________________________________~419 *fzf-inside-terminal-buffer*420420The latest versions of Vim and Neovim include builtin terminal emulator421(`:terminal`) and fzf will start in a terminal buffer in the following cases:421423 - On Neovim424 - On GVim425 - On Terminal Vim with a non-default layout426 - `call fzf#run({'left': '30%'})` or `let g:fzf_layout = {'left': '30%'}`427422On the latest versions of Vim and Neovim, fzf will start in a terminal buffer.423If you find the default ANSI colors to be different, consider configuring the424colors using `g:terminal_ansi_colors` in regular Vim or `g:terminal_color_x`425in Neovim.426433 *g:terminal_color_15* *g:terminal_color_14* *g:terminal_color_13*434*g:terminal_color_12* *g:terminal_color_11* *g:terminal_color_10* *g:terminal_color_9*435 *g:terminal_color_8* *g:terminal_color_7* *g:terminal_color_6* *g:terminal_color_5*436 *g:terminal_color_4* *g:terminal_color_3* *g:terminal_color_2* *g:terminal_color_1*437 *g:terminal_color_0*427>428 " Terminal colors for seoul256 color scheme429 if has('nvim')430 let g:terminal_color_0 = '#4e4e4e'
77 unmodified lines
modified:src/constants.go-11 removed
1package fzf23import (4 "math"5 "os"5 "time"67 "github.com/junegunn/fzf/src/util"8)
43 unmodified lines
52 // Jump labels53 defaultJumpLabels string = "asdfghjklqwertyuiopzxcvbnm1234567890ASDFGHJKLQWERTYUIOPZXCVBNM`~;:,<.>/?'\"!@#$%^&*()[{]}-_=+"54)5557var defaultCommand string5859func init() {60 if !util.IsWindows() {61 defaultCommand = `set -o pipefail; command find -L . -mindepth 1 \( -path '*/.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \) -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-`62 } else if os.Getenv("TERM") == "cygwin" {63 defaultCommand = `sh -c "command find -L . -mindepth 1 -path '*/.*' -prune -o -type f -print -o -type l -print 2> /dev/null | cut -b3-"`64 }65}6656// fzf events57const (58 EvtReadNew util.EventType = iota59 EvtReadFin
15 unmodified lines
modified:src/reader.go+11 added-27 removed
4 unmodified lines
5 "context"6 "io"7 "os"8 "os/exec"9 "path"9 "path/filepath"10 "sync"11 "sync/atomic"12 "time"1314 "github.com/charlievieth/fastwalk"15 "github.com/junegunn/fzf/src/util"16 "github.com/saracen/walker"16)1718// Reader reads from command or standard input19type Reader struct {
55 unmodified lines
75}7677func (r *Reader) terminate() {78 r.mutex.Lock()80 defer func() { r.mutex.Unlock() }()8179 r.killed = true80 if r.exec != nil && r.exec.Process != nil {81 util.KillCommand(r.exec)85 } else if defaultCommand != "" {82 } else {83 os.Stdin.Close()84 }85 r.mutex.Unlock()86}8788func (r *Reader) restart(command string) {89 r.event = int32(EvtReady)
6 unmodified lines
96func (r *Reader) ReadSource() {97 r.startEventPoller()98 var success bool99 if util.IsTty() {102 // The default command for *nix requires a shell that supports "pipefail"103 // https://unix.stackexchange.com/a/654932/62171104 shell := "bash"105 currentShell := os.Getenv("SHELL")106 currentShellName := path.Base(currentShell)107 for _, shellName := range []string{"bash", "zsh", "ksh", "ash", "hush", "mksh", "yash"} {108 if currentShellName == shellName {109 shell = currentShell110 break111 }112 }100 cmd := os.Getenv("FZF_DEFAULT_COMMAND")101 if len(cmd) == 0 {115 if defaultCommand != "" {116 success = r.readFromCommand(&shell, defaultCommand)117 } else {118 success = r.readFiles()102 success = r.readFiles()119 }103 } else {104 success = r.readFromCommand(nil, cmd)105 }106 } else {
38 unmodified lines
145}146147func (r *Reader) readFiles() bool {148 r.killed = false149 conf := fastwalk.Config{Follow: true}166 fn := func(path string, mode os.FileInfo) error {150 fn := func(path string, de os.DirEntry, err error) error {151 if err != nil {152 return nil153 }154 path = filepath.Clean(path)155 if path != "." {169 isDir := mode.Mode().IsDir()156 isDir := de.IsDir()157 if isDir && filepath.Base(path)[0] == '.' {158 return filepath.SkipDir159 }160 if !isDir && r.pusher([]byte(path)) {
6 unmodified lines
167 return context.Canceled168 }169 return nil170 }184 cb := walker.WithErrorCallback(func(pathname string, err error) error {185 return nil186 })187 return walker.Walk(".", fn, cb) == nil171 return fastwalk.Walk(&conf, ".", fn) == nil172}173174func (r *Reader) readFromCommand(shell *string, command string) bool {175 r.mutex.Lock()
20 unmodified lines
modified:BUILD.md+2 added
modified:CHANGELOG.md+14 added
1CHANGELOG2=========340.47.05------6- Replaced ["the default find command"][find] with a built-in directory traversal to simplify the code and to achieve better performance and consistent behavior across platforms.7 This doesn't affect you if you have `$FZF_DEFAULT_COMMAND` set.8 - Breaking changes:9 - Unlike [the previous "find" command][find], the new traversal code will list hidden files, but hidden directories will still be ignored10 - No filtering of `devtmpfs` or `proc` types11 - Traversal is parallelized, so the order of the entries will be different each time12 - You would wonder why fzf implements directory traversal anyway when it's a filter program following the Unix philosophy.13 But fzf has had [the traversal code for years][walker] to tackle the performance problem on Windows. And I decided to use the same approach on different platforms as well for the benefits listed above.1415[find]: https://github.com/junegunn/fzf/blob/0.46.1/src/constants.go#L60-L6416[walker]: https://github.com/junegunn/fzf/pull/184717180.46.119------20- Bug fixes and improvements21- Fixed Windows binaries
1860 unmodified lines
modified:go.mod+1 added-2 removed
1module github.com/junegunn/fzf23require (4 github.com/charlievieth/fastwalk v1.0.15 github.com/gdamore/tcell/v2 v2.7.06 github.com/mattn/go-isatty v0.0.177 github.com/mattn/go-shellwords v1.0.128 github.com/rivo/uniseg v0.4.78 github.com/saracen/walker v0.1.39 golang.org/x/sys v0.17.010 golang.org/x/term v0.17.011)1213require (14 github.com/gdamore/encoding v1.0.0 // indirect15 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect16 github.com/mattn/go-runewidth v0.0.15 // indirect17 golang.org/x/sync v0.5.0 // indirect17 golang.org/x/text v0.14.0 // indirect18)1920go 1.17
modified:go.sum+4 added-5 removed
1github.com/charlievieth/fastwalk v1.0.1 h1:jW01w8OCFdKS9JvAcnI+JHhWU/FuIEmNb24Ri9p7OVg=2github.com/charlievieth/fastwalk v1.0.1/go.mod h1:dryXgMJyGHbMrAmmnF0/EJNBbZaihlwcNud5IuGyogU=3github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=4github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=5github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=6github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=7github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=8github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=9github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=10github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=11github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=12github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
4 unmodified lines
17github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=18github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=19github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=20github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=17github.com/saracen/walker v0.1.3 h1:YtcKKmpRPy6XJTHJ75J2QYXXZYWnZNQxPCVqZSHVV/g=18github.com/saracen/walker v0.1.3/go.mod h1:FU+7qU8DeQQgSZDmmThMJi93kPkLFgy0oVAcLxurjIk=21github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=22golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=23golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=24golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
2 unmodified lines
27golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=28golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=29golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=30golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=29golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=31golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=32golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=32golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=33golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=33golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=34golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=35golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=36golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23 unmodified lines
modified:README-VIM.md+9 added-8 removed
236 unmodified lines
237call fzf#run({'sink': 'e'})238```239240We haven't specified the `source`, so this is equivalent to starting fzf on241command line without standard input pipe; fzf will use find command (or242`$FZF_DEFAULT_COMMAND` if defined) to list the files under the current243directory. When you select one, it will open it with the sink, `:e` command.244If you want to open it in a new tab, you can pass `:tabedit` command instead245as the sink.241command line without standard input pipe; fzf will traverse the file system242under the current directory to get the list of files. (If243`$FZF_DEFAULT_COMMAND` is set, fzf will use the output of the command244instead.) When you select one, it will open it with the sink, `:e` command. If245you want to open it in a new tab, you can pass `:tabedit` command instead as246the sink.247248```vim249call fzf#run({'sink': 'tabedit'})250```251251Instead of using the default find command, you can use any shell command as252the source. The following example will list the files managed by git. It's253equivalent to running `git ls-files | fzf` on shell.252You can use any shell command as the source to generate the list. The253following example will list the files managed by git. It's equivalent to254running `git ls-files | fzf` on shell.255256```vim257call fzf#run({'source': 'git ls-files', 'sink': 'e'})258```
235 unmodified lines
modified:README.md+6 added-8 removed
228 unmodified lines
229```sh230find * -type f | fzf > selected231```232233Without STDIN pipe, fzf will use find command to fetch the list of234files excluding hidden ones. (You can override the default command with235`FZF_DEFAULT_COMMAND`)233Without STDIN pipe, fzf will traverse the file system under the current234directory to get the list of files, skipping hidden directories. (You can235override the default behavior with `FZF_DEFAULT_COMMAND`)236237```sh238vim $(fzf)239```
247 unmodified lines
487488# Options to fzf command489export FZF_COMPLETION_OPTS='--border --info=inline'490491# Use fd (https://github.com/sharkdp/fd) instead of the default find492# command for listing path candidates.491# Use fd (https://github.com/sharkdp/fd) for listing path candidates.492# - The first argument to the function ($1) is the base path to start traversal493# - See the source code (completion.{bash,zsh}) for the details.494_fzf_compgen_path() {495 fd --hidden --follow --exclude ".git" . "$1"
276 unmodified lines
772### Respecting `.gitignore`773774You can use [fd](https://github.com/sharkdp/fd),775[ripgrep](https://github.com/BurntSushi/ripgrep), or [the silver777searcher](https://github.com/ggreer/the_silver_searcher) instead of the778default find command to traverse the file system while respecting779`.gitignore`.776searcher](https://github.com/ggreer/the_silver_searcher) to traverse the file777system while respecting `.gitignore`.778779```sh780# Feed the output of fd into fzf781fd --type f --strip-cwd-prefix | fzf
55 unmodified lines