How to Write grep in Emacs Lisp (tutorial)

Tassilo Horn tassilo at
Wed Feb 9 20:48:44 CET 2011

Xah Lee <xahlee at> writes:

>> You can rely on shell globbing, so that grep gets a list of all files in
>> all subdirectories.  For example, I can grep all header files of the
>> linux kernel using
>>   % grep FOO /usr/src/linux/**/*.h
> say, i want to search in the dir
> ~/web/xahlee_org/
> but no more than 2 levels deep, and only files ending in “.html”. This
> is not a toy question. I actually need to do that.

% grep ~/web/xahlee_org/*{,/*}.html FOO

That'll grep files like ~/web/xahlee_org/bla.html as well as
~/web/xahlee_org/bla/bla.html, but not any deeper.

>> However, on older systems or on windows, that may produce a too long
>> command line.  Alternatively, you can use the -R option to grep a
>> directory recursively, and specify an include globbing pattern (or many,
>> and/or one or many exclude patterns).
>>   % grep -R FOO --include='*.h' /usr/src/linux/
>> You can also use a combination of `find', `xargs' and `grep' (with some
>> complications for allowing spaces in file names [-print0 to find]), or,
>> when using zsh, you can use
>>   % zargs /usr/src/linux/**/*.h -- grep FOO
>> which does all relevant quoting and stuff for you.
> problem with find xargs is that they spawn grep for each file, which
> becomes too slow to be usable.

I can see not speed difference in find | xargs grep or grep with glob...

> To not use xargs but “find ... -exec” instead is possible of course
> but i always have problems with the syntax...

Yeah, there are so many ways. ;-)

>> There are many things you can also do with a plain shell script.  I'm
>> always amazed how good and concise you can do all sorts of file/text
>> manipulation using `zsh' builtins.
> never really got into bash for shell scripting... sometimes tried but
> the ratio power/syntax isn't tolerable. Knowing perl well pretty much
> killed any possible incentive left.

Yeah, perl is a swiss army knife, but I never got comfortable with it.

> Also note, this shell script can't be replaced by elisp, because elisp
> is not suitable when the file size is large.

You could chunk the file and handle the parts separately, in order to
not have everything in an emacs buffer and thus getting out of RAM.


More information about the Python-list mailing list