4

In vim for example, you search names like test1, test2 or test9 if you press /\<test\d\>. I'd like to have this "feature" like finding filesnames which have the word test<num> in their filename. I tried it out:

enter image description here

But it doesn't work, as you can see.

Here are my FZF-Environment-Variables:

  • $FZF_DEFAULT_COMMAND rg --files --hidden --glob "!.git/*"

  • $FZF_DEFAULT_OPTS --prompt=' ' --cycle --preview='bat {}' +i --bind=ctrl-j:preview-half-page-down,ctrl-k:preview-half-page-up

How can I use the regex patterns like in vim and sed to find files (and if it's possible to find words in these files)?

4
  • Please don't post images of text. Copy and paste the text itself into your question and format it as code by selecting it and pressing Ctrl-K or by using the editor's {} icon.
    – cas
    May 5 '21 at 8:57
  • Can I only use these three characters? !, $, ^? @mattb
    – TornaxO7
    May 7 '21 at 20:07
  • hm... ok, could you write your comment as an answer please so I can tick this question as answered?
    – TornaxO7
    May 7 '21 at 23:11
  • Answer posted. Cheers
    – mattb
    May 8 '21 at 12:02
3

I don't know if this is possible with fzf. But it does have a couple of special characters like ^ (must start with), $ (must end with), ! (must not contain). As for searching within files, you can do this with either the silver searcher (ag) or rigrep (rg) assuming they're installed by using the commands :Ag or :Rg

You can also take advantage of adding multiple patterns, each one delimited with spaces: e.g. ^core .py$ !test will match files that begin with 'core' and end with '.py'. and do not contain 'test' You can keep adding more patterns to narrow the search results.

1

In addition to the accepted answer, I want to add that spaces in your search pattern matter too, in order to separate fuzzy search pattern arguments.

Let's say you have this file tree. Notice that each dir has a test.txt and test file in it:

temp/dir1/test.txt
temp/dir1/test

temp/dir2/test.txt
temp/dir2/test

temp/dir3/test.txt
temp/dir3/test

Let's try to find all 3 files above which have the search pattern temp somewhere in their path, and are named test and have no file extension:

In default search mode in fzf:

  1. This search will find no files:
    temp/test$
    
  2. This search will find all 6 files:
    temp/test
    
  3. So, if you're trying to find just the test files which have no extension, you must add a space into your search pattern, like this! This search will find just the 3 test files which have no file extension (and have the pattern temp somewhere higher up in the path):
    temp /test$
    

Adding that one space between the two fuzzy search patterns (temp and /test$) makes all the difference!

Note that this search too will find all 6 files:

temp /test

In summary, to find just the 3 test files with no extension, you must use the space to separate fuzzy search parameters, AND use the $ at the end to indicate "end of line":

temp /test$

Of course, the same requirement for the space between fuzzy search parameters applies to the ^ "beginning of line" search character too. (This example doesn't make this point well, but the principle is the same.):

^temp /test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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