Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for simple variable names like 'c' or 'x' in Emacs

Tags:

regex

emacs

I often wish to search for variables that are simply called 'c' or 'count'.

For example

int c, count;

Unfortunately when I use an incremental search for 'c' or 'count' I get a lot of unnecessary hits like the 'c' in 'choice', or the 'count' in 'wordcount' which do not interest me. I know Emacs can do i-searches with regular expressions but I don't know the correct regular expression needed to match just 'c' and 'count'. These words are often surrounded by any number of white spaces. Anyone know the regex I can use to narrow my search?

like image 467
wp123 Avatar asked Dec 07 '22 03:12

wp123


2 Answers

Use isearch-forward-regexp, usually bound to C-M-s, and search for \bc\b or \bcount\b

like image 80
Andrew Stein Avatar answered Jan 09 '23 01:01

Andrew Stein


In Emacs23, there's M-x isearch-forward-word (bound to M-s w) which does takes care of the word boundary (at the front) for you.

M-s w count

And, another keyboard shortcut for Andrew Stein's answer of using isearch-forward-regexp is C-u C-s.

like image 24
Trey Jackson Avatar answered Jan 09 '23 01:01

Trey Jackson