7
votes

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
5
Two working solutions so far:dmgus
the first by Stefan (see below) and the second is by Drew Adams article.gmane.org/gmane.emacs.help/88152 He added it also to ISearch+ emacswiki.org/emacs/IsearchPlusdmgus

5 Answers

3
votes

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)))
2
votes

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.

1
votes

I think the problem is that you're not thinking about searching in a way that's congruent with the way isearch has been designed to work, and so your question doesn't really make sense within the context of isearch as it currently exists.

Isearch does already give you exactly the feature what you want, but you have to tell it that you want it to happen by typing that C-g you seem so vehemently opposed to typing. If you don't tell isearch what you want to do, and when you want it to do it, how is it supposed to know what to do?

As @Tom tried to explain, the default way isearch starts from the current position in the buffer, and can restart at the beginning of the buffer if you've typed some failed characters and then press C-s, is a very valuable feature. I'm sure many people rely on this behaviour. Your method of using a macro to always start an isearch at the beginning of the buffer would confound and confuse many of us, though of course it's not a bad thing for someone such as yourself who is accustomed to it. It does mean though that the rest of us are quite confused by your dislike for having to press C-g to delete the non-matching text.

Think also for a moment about what a second C-s does if you press it immediately after starting isearch (any time but the first time in a session) (i.e. before you type any other character). Note in particular what happens if your previous search string would only partly match something in the current buffer, and then you press C-g (and also note how the failed search string is presented, regardless of whether it would partly match something in the current buffer or not).

Think also about how your feature might adversely affect the use of multi-isearch-next-buffer-function.

Claiming that other editors can do what you want isearch to do in emacs doesn't really help your case much.

I think what you really want is some slightly different type of search function which only allows you to search for text that it is possible to find in the current buffer, instead of isearch's ability to search for anything whether that text happens to exist in the current buffer or not.

Perhaps isearch-mode could be adapted to do what you want it to do, but one way or another I think you'll have to write some elisp code. Perhaps you could implement your new search mode as an option within isearch-mode that can be toggled on and off in the same way case sensitivity can be toggled on and off; and that can be set by default, again in the same way that case sensitivity can be turned on or off by default.

0
votes

If you make a typo during incremental search all you need to do it press backspace to correct the typo.

  • 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 backspace
  • We press s
  • "keys" is found and highlited
0
votes

As with most of the other answers, I'm just pointing out another feature which helps mitigate the described problem. This one is particularly useful if you've continued to type several would-have-been-good characters after the bad one.

M-e is the binding for editing the isearch string, and in the case where there are no matches for the current string, it rather helpfully places point at the first non-matching character.

So if you have made a small typo, you can quickly type M-e, fix the mistake, and type RET to return to isearch using the corrected search string.