3

Sometimes I want to search for a specific word but I don't want to find words which have my search string as a substring.

For example, if I search for "pan" (bread, in Spanish), I don't want to find the words "expansión", "sepan", or "panorama". A partial work around would be to search for " pan " (with spaces around), but in this case, I will not find the word "pan" when it is at the end of the sentence ("pan.") or near a comma ("pan,").

Is there a straightforward way to accomplish that?

2 Answers 2

4

You used tag isearch so presumably you want to search interactively, not using Lisp. And you used tag words; you apparently want to search for a whole word.

In that case, just use M-s w, which does isearch-forward-word at the top-level, and which does isearch-toggle-word from within Isearch.

See the Emacs manual, node Word Search.

(I found that information in the manual by just doing i word search.)

0

You can use #'isearch-forward-regexp to search with a regex instead of a bare string. This function is bound to C-M-s by default.

So, we can search for \bpan\b. \b is a word boundary -- so it matches "the empty string, but only at the beginning or end of a word". So we search for a word boundary, then the word "pan", then another word boundary.

Alternately, passing a prefix argument to #'isearch-forward (which is bound to C-s by default) does an incremental search by regex. So you'd type C-u C-s.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.