Finding file by fuzzy searching name and content with fzf

Viewed 1619

problem

I need to fuzzy search a directory of textfiles, where the contents of the files is searched too. I am currently using ripgrep and fzf to do this, the problem is that each line of each file becomes a search item, i need each item to contain all lines of its file.

Example:

We have following two files:

example.md

something something foo something

something something foo something

something something foo something

something something foo something

useful-example.md

something something foo something

We run rg . | fzf in the directory containing them, write foo, and get following results:

useful-example.md:something something foo something

example.md:something something foo something

example.md:something something foo something

example.md:something something foo something

example.md:something something foo something

The problem here is that each line of example.md is a item, it should be one item containing each file. It should have looked something like this:

useful-example.md:something something foo something

example.md:something something foo something

......................something foo something

......................something foo something

......................something foo something

question

How can i go about making this? have somebody done this that im missing?

1 Answers

Ripgrep comes which a --heading flag that (from the man page)

prints the file path above clusters of matches from each file instead of printing the file path as a prefix for each matched line

It looks like this:

rg --heading --line-number --column . | fzf --layout=reverse

useful-example.txt
1:1:something something foo something

example.txt
1:1:something something foo something
2:1:something something foo something
3:1:something something foo something
4:1:something something foo something

If you are only interested in files with matches, add the flag --files-with-matches:

rg --files-with-matches . | fzf
useful-example.txt
example.txt
Related