Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs incremental search - auto remove from the search string the characters that could not be found

Tags:

emacs

It's not that convenient when you do a typo during an incremental search and the search string receives the wrongly typed character. Is there a way to prevent this. As if control-g was pressed automatically on error.

For example we have the following text:

keywords
keys

Default emacs behavior:

  • We start incremental search and search for "keyz"
  • The "keyz" is displayed in the search echo area and the "key" part in "keywords" is higlighted
  • We press s
  • "keys" won't be found, the cursor stays on the "keywords" line, search echo area displays "keyzs", which is not convenient

Needed behavior:

  • We start incremental search and search for "keyz"
  • The "key" is displayed in the search echo area and the "key" part in "keywords" is higlighted
  • We press s
  • "keys" is found and highlited
like image 365
dmgus Avatar asked Aug 23 '12 20:08

dmgus


2 Answers

You could try something like

(defadvice isearch-printing-char (before drop-mismatches-on-next-char activate)
  (while (or (not isearch-success) isearch-error)
    (isearch-pop-state)))
like image 67
Stefan Avatar answered Nov 01 '22 05:11

Stefan


Emacs keeps the incorrect part, because it happens very often that you search for a string and it is not found, but not because it's incorrect, only the search string is found before the cursor. In this case it is very convenient that you can press C-s and the search starts from the beginning of the file.

It is very useful behavior and it happens to me more often than mistyping the search string. If there is indeed an error in the search string then you can simply press C-g to go back to last good search string.

like image 2
Tom Avatar answered Nov 01 '22 05:11

Tom