-
-
Notifications
You must be signed in to change notification settings - Fork 390
Grep
Helm grep
is a Helm implementation of the grep command, which searches a text file for lines matching a regular expression.
Features include:
- Incremental
- Recursive
- Faster than Emacs
grep
- Supports wildcards
- Respects
grep-fine-ignored-files
andgrep-find-ignored-directories
variables
From helm-find-files
(<helm-prefix> C-x C-f
), open the action menu with TAB
and choose grep
. A prefix argument will launch recursive grep
, or .
You can also launch grep
directly with (C-u) M-g s
without switching to the action menu.
Just launch grep
, it will search in file at point. If the file is a directory, grep
will search recursively in all files of in the directory like:
grep -nH -e pattern *
To grep marked files, just mark some files with C-<space>
and launch grep
. Marked files can be from different directories.
Go to the root of the directory you want to grep
in, then hit TAB to open the action menu and choose grep
with a prefix argument (C-u
).
You will be prompted for types of files to search. Wildcard syntax like \*.el
(which searches only files ending in . el
) is allowed.
By default, the extension of the file at point is used when the cursor is on a file name. If the cursor is at root of a directory, all the file name extensions found in the directory and not matching
the variable grep-find-ignored-files
are inserted into the prompt.
Persistent action in a Helm grep
buffer (C-z
) will bring up the buffer corresponding to the file being grepped.
C-u C-z
will record the location in the mark ring. If you want to come back later to the grepped location, there is no need to grep again; you can find the locations in the mark ring.
Accessing the mark ring in Emacs is very inconvenient. Fortunately, the helm-all-mark-rings
command (in the menu, Tools > Helm > Tools > Browse Mark Ring) brings up a list of all mark rings, both local and global.
By default, Helm applies its own highlights to matched items. However, it may be better to use the backend to highlight result with ANSI sequences.
This can be done by customizing the helm-grep-default-command
variable. See the docstring of helm-grep-default-command
for more information.
(setq helm-grep-default-command
"grep --color=always -d skip %e -n%cH -e %p %f"
helm-grep-default-recurse-command
"grep --color=always -d recurse %e -n%cH -e %p %f")
Note:
The non-matched part of the resulting lines in grep will be hidden when using --color=always
. To fix this you have to set the environmental variable GREP_COLORS
for Emacs:
(setenv "GREP_COLORS" "ms=30;43:mc=30;43:sl=01;37:cx=:fn=35:ln=32:bn=32:se=36")
This will colorize matched items in black/yellow.
(setq helm-grep-default-command
"ack-grep -Hn --smart-case --no-group %e %p %f"
helm-grep-default-recurse-command
"ack-grep -H --smart-case --no-group %e %p %f")
(setq helm-ls-git-grep-command
"git grep -n%cH --color=always --full-name -e %p %f")
The color of git-grep can be customized from global .gitconfig
file.
If you want to save the results of your grep
session, doing C-x C-s
will save your grep
results in a helm-grep-mode
buffer.
Tip:
- You can save multiple Helm
grep
buffers with different names. - If you haven’t saved your
grep
session in a named buffer, you can still retrieve the last session withhelm-resume
.
Editing the helm-grep-mode
buffer is possible with wgrep. Saving this buffer will apply changes to the corresponding files.
Helm grep
supports other tools like ag, pt, and git-grep
.
Both ag
and pt
can be used with helm-do-grep-ag
. The variable helm-grep-ag-command
controls the exact command used.
git-grep
can be used with helm-grep-do-git-grep
.
Helm grep
works with TRAMP (you can grep a file on a remote host or in su/sudo methods), but TRAMP has poor support for multiple processes launched with short delays (< 5 seconds) between them.
Therefore, I recommend that that you suspend Helm updates with =C-!= while you write your regexp. Once done, hit again C-!
to restart Helm updates.
If you don’t do that, Helm grep
should still work, but it will be slow and may result in occasional failures.
Apple OS X users need a version of grep
that accepts --exclude-dir
. Install it from Homebrew with:
brew tap homebrew/dupes
brew install homebrew/dupes/grep
You should also set helm-grep-default-command
to use ggrep
instead of grep
.
Windows users need grep
version 2.5.4 of Gnuwin32. This version should accept the --exclude-dir
option.