IsearchOtherEnd

isearch (see IncrementalSearch) is a useful navigation mechanism. This works a little better if isearch puts you at the start of the search, not the end:

 (add-hook 'isearch-mode-end-hook 'my-goto-match-beginning)
 (defun my-goto-match-beginning ()
    (when isearch-forward (goto-char isearch-other-end)))
I agree that such a hook is useful. However, it would be a lot more useful if Emacs (21) always did as the Info doc says:
“When you exit the incremental search, it sets the mark to where point was, before the search. That is convenient for moving back there. In Transient Mark mode, incremental search sets the mark without activating it, and does so only if the mark is not already active.”
Unfortunately, if you end via ‘C-g’, and you use such a hook, the mark is not set at the search start, so you cannot get back there via ‘C-x C-x’. Too bad. (Ending the search with ‘RET’ or something else does set the mark correctly.) – DrewAdams

I was not amused by this behavior too so I checked and found a solution where C-g will still work as expected:

  (defun my-goto-match-beginning ()
    (when (and isearch-forward (not isearch-mode-end-hook-quit)) (goto-char isearch-other-end)))
‘isearch-mode-end-hook-quit’ is only non-nil if the user quits the search. In this case we do nothing and get placed back at the starting point. – steckerhalter

With an incremental search, it actually seems fairly natural to end up with point at the end of the search term; after all, you can see it progressing along the search term with each character that you enter. That’s not the case, however, for non-incremental search (search-forward, bound to C-s RET). So is it possible to do a similar thing like this for non-incremental search? I’ve only managed to do it using a wrapper so far:

 (defun my-search-forward (term)
    "Search forward, moving to the beginning of term."
    (interactive (list (read-from-minibuffer "Find: ")))
    (if (search-forward term)
        (backward-char (length term))))

But a hook approach would be better. – SeanPalmer

Surely (backward-char (length term)) is going to fail for most regex searches. You could, however, use (search-backward term) to move from the end of the match to the beginning. Doing this with ‘after’ advice and only in the interactive case might be safe, but you certainly wouldn’t want to be messing with this function this way in non-interactive circumstances – too many things depend upon its standard behaviour.


CategorySearchAndReplace