reddit / emacs

How to create a new keybinding with a custom prefix key?

eminent101 · 2022-07-07 16:17
I am learning how to create keybindings. So far I can create simple key bindings like this: (global-set-key (kbd "C-c uni") (lambda () (interactive) (insert "universe"))) Now if I press `C-c` and type `uni`, the word `universe` is inserted to the buffer. But this key binding does not work: (global-set-key (kbd ":uni") (lambda () (interactive) (insert "universe"))) If I compile this, I get error: Debugger entered--Lisp error: (error "Key sequence : u n i starts with non-prefix key :") How can I make a successful key binding for `:uni`? Yes, I know this is going to interfere with typing literal `:` in the text and I find with that for now because I am still learning what is possible and what is not possible.
Hagge5 · 2022-07-07 16:51
This question https://stackoverflow.com/questions/9462111/emacs-error-key-sequence-m-x-g-starts-with-non-prefix-key-m-x On stackexchange makes it sound like you get this error if : is already bound to another key, presumably in the same map (in this case global-map). This is a shot in the dark, but try binding : to nil before you try to bind :uni to something. Note that in emacs, inserting a char is considered a command. Try C-h k : to see what it's bound to. Edit: Why am I getting downvoted when I'm providing the same answer as the topvoted one, posted earlier than the topvoted one, just worded differently?
dpoggio · 2022-07-07 16:58
`:` actually is bound to a function to insert “:”. You can bind inside `C-c` because it’s bound to a keymap (a list for more bindings). You can bind `:` prefixed things if you unbind it previously (`global-unset-key`) from its function. Now, the thing is, I think you are trying to just re-invent `abbrev-mode`, or `yasnippet`. So maybe check those before digging in muddy bindings, unless you just do it for fun and learning. Wasting is learning, a doctor said.
arthurno1 · 2022-07-07 23:27
> Why am I getting downvoted when I'm providing the same answer as the topvoted one, posted earlier than the topvoted one, just worded differently? Reddit is a strange place. Rules says to down voting if a comment is not constructive for the discussion, not if you dislike the opinion. However, I have notice that people downvote if they dislike the person, dislike the opinion, don't understand what person or sometimes simple just had different expactation of what will they find in an answer. It is the rule of the mobb, I would not care much about.
dpoggio · 2022-07-08 01:36
If you ask me, you are not providing an answer at all, and this is a VERY simple question. You give a SO link with some rather poor advice IMO. There is a proper entry in the GNU Emacs manual ([link](https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Commands.html)), so to me it seems blind Googling (this entry is also available through `C-h i` in the Elisp topic). Then you say: >On stackexchange makes it sound like you get this error if : is already bound to another key, presumably in the same map (in this case global-map). Actually, it's because it is being bound to a function, not a key; and it is EXACTLY in the same map. Why presumably? Why would it fail for a binding found in another map at all? >This is a shot in the dark, but try binding : to nil before you try to bind :uni to something. A shot in the dark for a very simple question is not the same as providing `global-unset-key`, which is not just THE WAY (TM) to do it, but a widely used function that almost everyone knows about (please, read the manual entry in the link I gave above). Blindly asking the PO to "try binding to nil" is IMO one of the phrases you get downvoted for, and it's also not providing an answer. Finally, the user is trying to bind `:uni` to some function to insert `universe`. Unless it's just wondering around, this is re-inventing snippets in a crappy, keybinding focused, obviously not scalable way. This is a shot in the foot, so trying to give a bit of knowledge and advice is another big difference. People tend to use votes to say: Hey, this is helpful, this isn't.
onetom · 2022-07-08 08:00
That was very insightful, thanks for taking the time to share your thinking process! I'm seeing a lot of surprising down votes lately and I was wondering a lot what the reasons might be.
_viz_ · 2022-07-09 07:24
> A shot in the dark for a very simple question is not the same as providing global-unset-key, which is not just THE WAY (TM) to do it, but a widely used function that almost everyone knows about (please, read the manual entry in the link I gave above). (defun global-unset-key (key) "Remove global binding of KEY. This is a legacy function; see `keymap-global-unset' for the recommended function to use instead. KEY is a string or vector representing a sequence of keystrokes." (interactive "kUnset key globally: ") (global-set-key key nil)) Hagge5 is not wrong.
dpoggio · 2022-07-09 22:40
Nobody said he/she is