Fork me on GitHub
#emacs
<
2023-09-06
>
adi21:09:37

So, Emacs's built in sh-script mode defines keybindings with hard-coded prefix C-c in sh-mode-map [1]. This clobbers some of my C-c-prefixed keybindings (e.g. counsel), when sh-script mode is active. sh-script mode offers no way to specify a custom prefix (e.g. C-c s). - If I want my custom prefix on all the sh-mode-map bindings, then do I have to first keymap-unset all the keys individually, and then custom-set them again, individually? - And how do I prevent sh-mode-map from clobbering my counsel keybindings in the first place? [1] sh-mode-map keybindings https://repo.or.cz/emacs.git/blob/HEAD:/lisp/progmodes/sh-script.el#l498

bg04:09:42

One way I do this is by setting the mode keymap to nil to unbind all the keybindings for the mode, and then selectively add new bindings to the functions that I actually use. In your case, you can start with something like (setcdr sh-mode-map nil)

bg04:09:28

You may use the bind-key package to create a new set of prefixed bindings like so: (not tested)

(bind-keys :map sh-mode-map
           :prefix "C-x"
           :prefix-map custom-prefix-map
           ("(" . sh-function)
           ("C-w" . sh-while))

bg04:09:00

bind-key is a part of GNU Emacs

adi05:09:36

Thanks for the tip! I was coming around to the same realisation. Also, good to know about setcdr . My elisp-fu is weak. Apropos (re)binding keys, use-package has me covered.

emacs 2
bg05:09:56

I don't know how to set bindings with a prefix without repetition within the use-package macro. Let me know if you're able to figure it out.

Ed09:09:45

not sure if it's helpful, but I have things like this in my setup which rebinds C-x C-c C-d to run find-dired (and I have a whole bunch of other keybindings in there too):

(define-prefix-command 'ed/C-x-C-c-prefix)
(global-set-key (kbd "C-x C-c") 'ed/C-x-C-c-prefix)
(define-key ed/C-x-C-c-prefix (kbd "C-d") 'find-dired)
cos who needs to quit emacs ever?

😜 2
adi16:09:45

> to set bindings with a prefix without repetition within the use-package macro I am under the impression :bind-keymap is the way to do this: https://github.com/jwiegley/use-package#binding-to-keymaps