2

How to make C-s (isearch-forward) and C-r (isearch-backward) search only for words that start with the argument passed to them, instead of searching for the argument even if it's in the middle of a word?

2
  • 1
    For 2) see section "Position of the Cursor after Searching" here emacswiki.org/emacs/IncrementalSearch
    – andrej
    Commented Nov 29, 2018 at 10:38
  • Please post two separate questions for your two separate question. As it stands now, the question is too broad and may be deleted.
    – Drew
    Commented Nov 29, 2018 at 18:06

3 Answers 3

3

You can either toggle word-search-mode by doing M-s w after starting isearch, or you can use start in word search mode directly using isearch-forward-word, also bound to M-s w.

2
  • I tried it first, but unfortunately, it doesn't seem to have an equivalent isearch-backward-word. And it only shows the first matched word, with no way (apparently) to go to the next match.
    – Bite Bytes
    Commented Nov 29, 2018 at 16:08
  • 2
    isearch-backward-word doesn't exist, but you can do C-r M-s w, or write it and choose your own binding. Once you're in word mode you can just use C-s or C-r to go to the next match.
    – rpluim
    Commented Nov 29, 2018 at 16:28
2

word-search-mode is the usual way to constrain your searches to words rather than letters, as @rpluim shows.

An alternative, more general approach is to use regular expression searches, isearch-forward-regexp, or C-M-s, and also the reverse isearch-backward-regexp and C-M-r. You can then use a regular expression to limit your search: \bin will match in when it occurs at the start of a word.

This is not as convenient as word-search-mode, but it is more flexible. For instance, you could find words ending with er with er\b.

0

Here's what I use to do this

;; Use isearch-forward-word by default (treats a space as any whitespace or punctuation)
(global-set-key (kbd "C-s") 'isearch-forward-word)
;; This is the same as isearch-forward-word except with the first argument to
;; isesarch-mode set to nil to do reverse search
(defun isearch-backward-word (&optional not-word no-recursive-edit)
  "Do incremental search backward for a sequence of words.
With a prefix argument, do a regular string search instead.
Like ordinary incremental search except that your input is treated
as a sequence of words without regard to how the words are separated.
See the command `isearch-forward' for more information.
This command does not support character folding, and lax space matching
has no effect on it."
  (interactive "P\np")
  (isearch-mode nil nil nil (not no-recursive-edit) (null not-word)))
(global-set-key (kbd "C-r") 'isearch-backward-word)

isearch-backward-word doesn't exist so I just copied the source of isearch-forward-word and changed the first argument of isearch-mode to nil to do reverse isearch.

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.