Within helm I'd like to find all of the files that have the content 'foo' and 'bar', even if 'foo' and 'bar' are on different lines.
2 Answers
You can use helm-ff-do-grep
inside helm-find-files
session.
- Inside
helm-find-files
session. You can invokehelm-ff-run-grep
with C-s and C-u as prefix argument. Then choose where/what to search then it prompt you a pattern to search.
helm-ff-do-grep
use ack or grep as backend. see https://tuhdo.github.io/helm-intro.html#orgheadline11
Or You can use helm-org-rifle
In contrast with org-occur and similar commands, helm-org-rifle is entry-based (i.e. a heading and all of its contents, not including subheadings), while org-occur is line-based. So org-occur will show you entire lines that contain matching words, without any reference to the heading the line is under, while helm-org-rifle will show the heading of the entry that matches, followed by context around each matching word in the entry. In other words, helm-org-rifle is sort of like Google, while org-occur is sort of like grep.
If you are not limited to Helm, you can do this easily using Icicles.
C-x C-f
C-M-j
foo
S-SPC
C-M-j
bar
Translation:
C-x C-f
is the command for finding a file (icicle-file
). It uses multi-completion input (like the Helm command you are used to, I presume). This means that you can match either file name or file content, or both. In this case, you apparently do not care about the file names, so you match only the content (see next).C-M-j
is (by default) the key that separates file-name pattern from file-content pattern. You can use any key you like for this -- it's customizable (but there is a good reason for this odd-seeming default value). You use this key immediately, because there is no file-name pattern to match in your case. You use it to go on to specifying a file-content pattern.foo
is the first pattern that you want the file content to match.S-SPC
is the key to narrow to the current set of completion candidates, in this case, all files that contain a match forfoo
(which in this case is a literal pattern, so all files that containfoo
). You can think of this key as "locking-in" a set of matches so that they become the new domain to match against. (Again, the key that does this is onlyS-SPC
by default; you can customize it to be any other key.)C-M-j
again, to skip a file-name pattern and provide another file-content pattern.bar
is the second pattern that you want the file content to match.
What you see now is the set of files you are after. You can choose one to visit, or you can visit all of them at once (using C-!
).
Here is another example you can follow, with explanations:
comm -12 <(grep -lr "foo" . ) <(grep -lr "bar" . )
, which you can enter inM-x
grep
, but the filenames wont be "click"-able.helm-projectile-grep
? See the heading "Search in project" over here: tuhdo.github.io/helm-projectile.htmlGrep v3.1
supports multi-line pattern.