1

When I do I-search: or I-search-backward , it starts right away from the first word it find.

Original behavior:

word1                                           |[cursor]word1 ; cursor moves here 
wor[cursor]d2 ;<= I press `ctrl+r` word [enter] |word2
word3                                           |word3

For example, when I am in I-search, I want to start doing backward search and press ctrl+r but in the first press it remains in the current found word (cursor move into its beginning) and on the second press to ctrl+r, `emacs jumps to the found word on backward.

Example:

word1                                           |word1
wor[cursor]d2 ;<= I press `ctrl+s` word [enter] |word2
word3                                           |word3[cursor]; cursor moves here

then

word1                                           |word1
word2                                           |word2
word3[cursor] ;<= I press `ctrl+r` word [enter] |[cursor]word3 ; cursor moves here                                          

at this stage I want cursor to move beginning of word2 like it should do on its original behavior.


=> I was wondering when the switch from I-search: to I-search-backward or visa versa, can the action take place on the first click instead of two?

Drew
  • 75,699
  • 9
  • 109
  • 225
alper
  • 1,238
  • 11
  • 30

1 Answers1

1

It's all based on where the cursor is when you start the search.

C-hig (emacs)Basic Isearch says:

A backward search finds matches that end before the starting point, just as a forward search finds matches that begin after it.

Hence for:

word1
wor[cursor]d2
word3

Searching backwards for word cannot find word2 because that instance of "word" doesn't end before [cursor].

word1
word2
word3[cursor]

Searching backwards here, we will initially find word3, as that instance of "word" ends before [cursor].


Edit: The following is extremely minimally tested, but give this a whirl:

(define-advice isearch-repeat (:before (direction &optional count) goto-other-end)
  "If reversing, start the search from the other end of the current match."
  (unless (eq isearch-forward (eq direction 'forward))
    (when isearch-other-end
      (goto-char isearch-other-end))))
phils
  • 48,657
  • 3
  • 76
  • 115
  • Wow your code works perfect, I don't know how it makes the magic but thank you – alper May 31 '20 at 15:33
  • I found a small bug, not sure could it be related to this. On following example (https://gist.github.com/avatar-lavventura/2a26b7c64386167f86e443656d62c614) when I do `C-s` on top of first `_PATH` work it does not see the second `_PATH`. But when I add space after the second `_PATH` word it sees that – alper Mar 04 '21 at 11:25
  • I can't replicate that. I assume you're searching for `_PATH`. Please test starting from `emacs -Q` – phils Mar 04 '21 at 14:02
  • I haven't understood the problem, in that case. Please give a recipe to reproduce this starting from `emacs -Q`. Ask a new question if you can't do it in a comment (but if you do that, please firstly verify whether the code in this Answer actually makes a difference, as there's no purpose in referencing this if it's not relevant). – phils Mar 04 '21 at 20:31
  • sorry the problem I was having is related to `(define-key isearch-mode-map (kbd "C-w") 'isearch-forward-symbol-at-point)` which was related to this answer (https://emacs.stackexchange.com/questions/55306/how-do-i-add-a-keybinding-to-use-during-an-interactive-isearch-c-s/55321#55321) . I think instead of capturing `_PATH` it captures `_PATH=` which fails the search – alper Mar 04 '21 at 22:24
  • Seems like `isearch-repeat` does not work in emacsclient 27.2 :( // I am having following error: `Wrong number of arguments: (lambda (direction) "If reversing, start the search from the other end of the current match." (if (eq isearch-forward (eq direction 'forward)) nil (if isearch-other-end (progn (goto-char isearch-other-end))))), 2 ` – alper Jul 23 '21 at 14:18
  • 1
    Emacs 27.1 added an optional argument, so I've updated the advice to match. – phils Jul 24 '21 at 02:14