4

Can I assign a key binding starting with C-s such as:

(global-set-key (kbd "C-s C-w") 'isearch-forward-symbol-at-point)

but I want to keep (kbd "C-s") for i-search.


I am having following error:

error: Key sequence C-s C-w starts with non-prefix key C-s

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.
6

2 Answers 2

4

As I understand your question, you want C-s to start an interactive search, as it normally does, but if you press C-w after you've started that search, you want it to switch to isearch-symbol-at-point.

First off, the feature you're after is almost the same as the default behaviour bound to C-s C-M-w. That will call isearch-yank-symbol-or-char, which is close, but not identical to isearch-symbol-at-point. Maybe that's close enough.

If you really want to call isearch-forward-symbol-at-point, without overwriting the default behaviour of C-s, you'll need to add the new binding to the isearch-mode-map:

(define-key isearch-mode-map (kbd "C-w")
  'isearch-forward-symbol-at-point)

Note that this will overwrite the default definition of C-s C-w, which is to call the function isearch-yank-word-or-char.

The default options to isearch are described in the manual (info "(emacs) Isearch Yank")

6
  • When I use this in emacs v28 I am seeing 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 when the search is completed and come back to top of the file. Is it possible to fix it?
    – alper
    Feb 21, 2021 at 20:24
  • @alper I cannot reproduce this error in Emacs 28.0.50. Have you tried starting from emacs -Q, and evaluating only the define-key form from my answer? I suspect there's something else in your config causing the problem.
    – Tyler
    Feb 22, 2021 at 14:17
  • I found a small bug, not sure could it be related to this. On following example (gist.github.com/avatar-lavventura/…) when I do C-s on top of first _PATH work it does not see the second _PATH. I think instead of capturing _PATH it captures _PATH=
    – alper
    Mar 4, 2021 at 22:22
  • this matches the symbol at point, and I think in this mode both _ and = are recognized as part of a symbol name, not as punctuation. So this is the expected behaviour. To change this, you would need to use a different function, or modify the syntax classes for this mode.
    – Tyler
    Mar 5, 2021 at 1:38
  • Got it seems like its Shell-script mode where can I find its syntax classes?
    – alper
    Mar 5, 2021 at 11:28
0

The C-s needs to be assigned as a prefix command.

eg

(define-prefix-command 'myprefix)
(global-set-key (kbd "C-s") 'myprefix)
(global-set-key (kbd "C-s C-s") 'helm-occur)
2
  • I am confused I want to keep C-s key binding for the isearch @RichieHH
    – alper
    Feb 5, 2020 at 10:08
  • 2
    @alper: then how is emacs to guess that you mean to type a second C-s? It can either be a prefix or it can be a key bound to a command, but it cannot be both.
    – NickD
    Feb 5, 2020 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.