Finding Simple Variable Names, such as 'c' or 'x' in Emacs

I often want to look for variables that are simply called "c" or "count".

for instance

int c, count; 

Unfortunately, when I use the incremental search 'c' or 'count', I get a lot of unnecessary hits, such as 'c' in 'choice', or 'count' in 'wordcount' that don't interest me. I know that Emacs can execute i-queries with regular expressions, but I don’t know the correct regular expression needed to match only “c” and “count”. These words are often surrounded by any number of white spaces. Does anyone know a regex that I can use to narrow my search?

+4
source share
4 answers

Use isearch-forward-regexp , usually tied to CMs , and search for \bc\b or \bcount\b

+7
source

In Emacs23, there is Mx isearch-forward-word (associated with Ms w ) that really cares about the word boundary (front) for you.

 Ms w count 

And another key combination for Andrew Stein answer using isearch-forward-regexp is Cu Cs .

+6
source

You want to do a search (isearch-forward-regexp) for your string with word-boundaries ( \b ) on both sides:

 \bc\b \bcount\b 
+2
source

Others told you how to search for what you want. Here is my advice: from now on, just don't do it - do not create the need for such a search in the first place.

30 years ago, I stopped using variable names such as x and i . If a variable cannot or should not be called something more useful, I will simply call it xx or ii .

This simple trick cuts a lot of stupid regular expression searches, for example (correctly), described on this page.

And usually there is nothing to justify, not to do something like that. I think often single letters are used only out of laziness. But this practice may mean more effort in the end.

Just an offer ...

(Now it’s obvious that some letters are not easily processed. nn , for example, is common in English words. Use your opinion.)

0
source

Source: https://habr.com/ru/post/1305020/


All Articles