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
  • This is surely a duplicate question, but I don't have the time to look it up. Hope someone will. Commented Feb 5, 2020 at 0:58
  • sure sounds like a duplicate, but I couldn't find one. Check the manual: gnu.org/software/emacs/manual/html_node/elisp/… , especially the description of global-unset-key. A longer explanation is at the end this article: masteringemacs.org/article/mastering-key-bindings-emacs Commented Feb 5, 2020 at 3:23
  • I changed your title to something more descriptive, I think. Change it back if you don't like it! Commented Feb 5, 2020 at 14:44
  • Thanks, I like it :-) @Tyler Commented Feb 5, 2020 at 14:48
  • @Drew this question is a more specific version of emacs.stackexchange.com/questions/5716/… Should it be closed, or is it useful to have a separate question for isearch specifically? Commented Feb 6, 2020 at 17:12

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? Commented 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. Commented 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= Commented 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. Commented Mar 5, 2021 at 1:38
  • Got it seems like its Shell-script mode where can I find its syntax classes? Commented 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 Commented 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. Commented Feb 5, 2020 at 12:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.