Take the 2-minute tour ×

I'm looking through a very large hydrodynamics code in c which has, often, some very poor variables choices. Including a global variable named just 'g'. Similarly, there's a file with a variable named 'geom' and lots of other variables which contain the substring 'geom' (e.g. geometry, geomAL, geom_arb, etc.).

Is there any way to search for variables that exactly match a regex, instead of partially?

For example: searching for 'geom' does not match 'geomAL'. Obviously emacs doesn't a priori know where a variable starts or ends, but could this be constructed as a function for c-mode?

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

The Emacs regular expression engine (C-M-s <regexp>) has various operands for this sort of thing, such as the word boundary \< and \> zero-width assertions. So \<geom\> would match geom alone and (depending on your mode's syntax table) perhaps also the prefix in geom_something. Try \<geom\>[^_] if you need to exclude the underscore suffix.

share|improve this answer
Sorry folks, I edited this answer instead of my own by accident. Is there a way to cancel edits? –  mvw Aug 30 '13 at 13:52
@mvw: It was rejected by the reviewers. You can also rollback an edit by clicking on the "edited" link and looking for the rollback link in the header of the previous edit before the unwanted one. –  tripleee Aug 30 '13 at 14:04
I personally prefer the \b operand, which I find a little easier to type then, < or >. –  Bruce Connor Aug 30 '13 at 20:38
@BruceConnor Thanks! That's indeed a little more pleasant –  zhermes Aug 31 '13 at 14:04
add comment

You can use C-u C-s \_<g\_> which will search for the symbol g using a regular expression search with symbol-boundary markers. Or in a recent enough Emacs you can do M-s _ g which will do essentially the same (M-s is the "search prefix key" in which M-s _ is isearch-forward-symbol).

share|improve this answer
add comment

http://www.emacswiki.org/emacs/RegularExpression has pretty much everything you need if you want to use emacs regexps.

share|improve this answer
add comment

Have you tried out the Emacs TAGS system? It should be able to parse the vars out and it might offer exact lookups. See here: http://www.emacswiki.org/emacs/EmacsTags

Generate the tags table with the etags helper:

etags *.c  

Look for a tag with

M-. your-var-name
share|improve this answer
add comment

Your Answer


