1

I want to be able to do a live fuzzy search on all source-controlled python files for example, or just all source-controlled files. Live, meaning updating in real time. Is there a way to do it?

Initially thought of telescope. It may be quite simple. But I am quite confused from all telescope's terms. There is https://github.com/davvid/telescope-git-grep.nvim which allow for non-fuzzy search, and there is live grep which allows for fuzzy search if you know how to use it. But it is on all files inside certain directory.

require'telescope.builtin'.grep_string{ shorten_path = true, word_match = "-w", only_sort_text = true, search = '' }

A simpler question might be: Is there a way to filter on files when doing live grep that is also generic? (because getting the right file names is quite simple).

PS. I did commit and added to ripgrep an option of filelist that gets list that contains files https://github.com/BurntSushi/ripgrep/pull/2598 . I think it can be done with something like the following code :

function! GitF(onlypy)
    let tmp=getcwd()
    :exe ":cd ". expand("%:p:h")
    let top = systemlist("git rev-parse --show-toplevel")[0]
    echo top
    :exe ':cd '. top
    let a=systemlist("git ls-files --full-name" )

    let a = (a:onlypy ? filter(a,{idx,val -> val =~ ".*py$"}): a)
    echo a
    :call writefile(a,'c:\temp\filelist.txt')
    
    "py3 t=[os.path.join(vim.eval("top"),y) for y in vim.eval("a")]
    "echo join(a,' --iglob ')mF
    :exec ":Leaderf rg " . ' --filelist c:\temp\filelist.txt'
    :exe ':cd '.tmp
endfunction

However, it still doesn't work for one reason or another. And I do prefer other plugin than leaderf.

3
  • If there is other plugin with that feature, please suggest.
    – eyal karni
    Aug 4, 2023 at 1:04
  • I understand that you want to fuzzy search a term only within the files under source control and skip the one ignored by source control. Do you confirm my understanding? Aug 25, 2023 at 3:46
  • 1
    Yes I confirm. It is has to be live, meaning to update in real time as you type.
    – eyal karni
    Aug 25, 2023 at 3:52

3 Answers 3

1
+300

:Telescope live_grep is supposed to do that:

  • Live search on files
  • Filtering out the .gitignore

The telescope-live-grep-args provides:

  • Live search on files (bringing result while searching)
  • Passing arguments to the underlying rg and in particular the -g and -g! argument that allow filtering against file patterns.

For searching for file content I personally use the CtrlSF plugin that provides:

  • Live search on files (bringing result while searching)
  • Restricting on some file type

e.g.:

:CtrlSF -T py foo

Search for foo in all the Python files.

12
  • 1
    While it may ignores the .gitignore, doesn't it include all files that are not in git ignore, even if they are not in source control.
    – eyal karni
    Aug 25, 2023 at 13:35
  • I initially forgot that I wanted to filter on file names (/extension).
    – eyal karni
    Aug 25, 2023 at 13:38
  • 1
    This plugin is cute. Almost does what I wanted. Even though I defined the backend to be rg, it seems to do exact search.
    – eyal karni
    Aug 25, 2023 at 13:53
  • 1
    A bit out of scope. But I didn't succeed in making telescope work with rg. So could you say a few words about it?
    – eyal karni
    Aug 26, 2023 at 22:09
  • 1
    I believe you have to set grepprg to rg more precisely: set grepprg=rg\ --vimgrep Aug 27, 2023 at 6:52
0

There is require("telescope.builtin").git_files() which does what you want.

There is also require("telescope.builtin").find_files() and with require("telescope.builtin").find_files { hidden = true, no_ignore = true } you can search for hidden files

The grep things in Telescope are for searching words in files, not files itself

6
  • It doesn't. Live grep meaning grep inside files, not grep on file names. Sorry for not being clear enough.
    – eyal karni
    Aug 24, 2023 at 22:36
  • I'm just curious if you really need to find files with grep, fuzzy find feels so much more suitable for this Aug 25, 2023 at 8:59
  • I want fuzzy search inside files. I didn't understand your comment.
    – eyal karni
    Aug 25, 2023 at 13:41
  • I think you are confusing things and confusing me. Fuzzy - when you input some text and search for this text no matter where it's in line and which order. You asking how to fuzzy live grep which doesn't even make sense. You either use regex or fuzzy search, not both Aug 27, 2023 at 8:13
  • Fuzzy means to match even if it is not exact match "dscription" matches lines with description.. it can be formulated in terms of Levinstein distance.
    – eyal karni
    Aug 27, 2023 at 19:03
0

You will need to use my branch on both: https://github.com/eyalk11/ripgrep/tree/filelist and https://github.com/eyalk11/leaderf/tree/filelist

filelist is an option I added to both leaderf rg and rg that contains paths that it adds to the paths to consider.

Then use the following function:

function! GitF(regfilter)
    let tmp=getcwd()
    :exe ":cd ". expand("%:p:h")
    let top = systemlist("git rev-parse --show-toplevel")[0]
    "echo top
    :exe ':cd '. top
    let a=systemlist("git ls-files --full-name" )
    :if len(a:regfilter)>0
    let a = filter(a,{idx,val -> (val =~ a:regfilter)})
    let a= map(a, {idx,fname -> fnamemodify(fname, ':p')})
    :endif 
    ":echo a
    :call writefile(a,'c:\temp\filelist.txt')
    try
        echohl Question
        let pattern = input("Search pattern: ")
        let pattern = escape(pattern,'"')
    finally
        echohl None
    endtry
    
    :exe ':cd '.tmp
    exec printf("Leaderf rg --filelist c:\\temp\\filelist.txt %s\"%s\"", pattern =~ '^\s*$' ? '' : '-e ', pattern )

endfunction

command! -nargs=1 LfGitExt :call GitF(".*\.". <f-args> ."$" )<CR>
command! -nargs=1 LfGitGen :call GitF(<f-args>)<CR>

This function generates the right list of files. Asks you about constant intial input (similar to leaderfinteractive) and does fuzzy search over the files.

To use it , you can use the defined commands

:LfGitExt vim 

(live grep over all vim files in current git proj)

It didn't occur to me(so silly) that ripgrep doesn't do fuzzy search. In fact, no tool does on recursive search. But leaderf in interactive mode allows to fuzzy search over results obtained by constant search .

That is probably the closest you can get to recursive fuzzy search.

2
  • 1
    The result is really good btw.
    – eyal karni
    Sep 1, 2023 at 14:03
  • ripgrep does not do fuzzy search, but ugrep does, and it is fast too. Perhaps give ugrep a try? Dec 15, 2023 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.