3 consecutive key bindings in evil normal mode - emacs

Nowadays I'm using evil-mode in Emacs.
One of my problem is,
I'm not able to define 3 consecutive key bindings.
If I set like this,
(define-key evil-normal-state-map (kbd "crh") 'my-own-function)
then,
it shows this error message.
Key sequence c r h starts with non-prefix key c
Do you know how to overcome this situation? Or is it impossible??

If you use general package you can look into general-nmap
(general-nmap
"crh" 'your-own-function)
About the non-prefix error take a look at
emacs error: Key sequence M-x g starts with non-prefix key M-x

Related

New Doom Emacs user - trying to set global-set-key

I've installed neotree in Doom Emacs. I would like to bind function key 8 ([f8]) using global-set-key. i've put
(global-set-key [f8] 'neotree-toggle)
in init.el, config.el, and custome.el. When I press f8, emacs says " is undefined".
if i type in ":neotree-toggle", it works.
Can you help?
In emacs-lisp (elisp), keys can be represented in different ways, vector or string. The usual way to represent them is to use the macro kbd (for keybinding of course) that transforms a simple string like "C-h f" to the key representing CTRL+h f.
If you want to learn more about that you can read the emacs manual about keys here, or this blog post that is pretty good here.
Finally, if you look at the description of the function global-set-key with C-h f RET global-set-key :
global-set-key is an interactive compiled Lisp function in ‘subr.el’.
(global-set-key KEY COMMAND)
Probably introduced at or before Emacs version 1.4.
Give KEY a global binding as COMMAND.
COMMAND is the command definition to use; usually it is
a symbol naming an interactively-callable function.
KEY is a key sequence; noninteractively, it is a string or vector
of characters or event types, and non-ASCII characters with codes
above 127 (such as ISO Latin-1) can be included if you use a vector.
Note that if KEY has a local binding in the current buffer,
that local binding will continue to shadow any global binding
that you make with this function.
You have to give it a key than a command, so in your case, it'll be something like :
(global-set-key (kbd "<f8>") 'neotree-toggle)
See how special keys are dealt with with kbd here.
I had a few minutes, and since I'm already using doom I did a little experimentation. This should work for you:
(global-set-key (kbd "<f8>") #'neotree)

Emacs: Use a function key (F19) as meta

I’m on a Mac and would like to use the function key F19 as meta.
(There’s a good reason, although it's a bit of a hack: My built (the otherwise excellent port by Yamamoto Mitsuharu) doesn’t support using only the left alt key as meta while preserving the native behavior (inserting special characters) of the right alt key.. So I remapped the left alt key to an unused key - F19 - on the system level with PCKeyboardHack (xmodmap is sorely missed) and would like to tell Emacs to use that as meta.)
So, how do I: use a function key (F19 in my case) as meta key in Emacs?
(I’m fairly new to Emacs and, after some googling, tried out something like
(define-key global-map [f19] \M)
but that, of course, doesn’t do the trick (Symbol’s value as variable is void: M)
Well, with
(setq x-alt-keysym symbol)
you can tell emacs what key is to be understood as meta, but afaik it only accepts 'meta, 'alt, 'super and 'hyper as symbol. Maybe try it with f19.
An alternative option that will work but which will require adaption to a new way of working would be to use F19 as a prefix key (like you use C-h or F1 to invoke help commands):
(define-prefix-command 'f19-map)
(global-set-key (kbd "<f19>") 'f19-map)
(global-set-key (kbd "<f19> x") 'execute-extended-command)
(global-set-key (kbd "<f19> u") 'upcase-word)
...
Writing the configuration shouldn't be too hard. Just press C-h b to get all existing keybindings and edit the help buffer. Get rid of all lines that don't start with M-, then use rectangles to replace all occurrences of ^M- by (global-set-key (kbd ", and so on.
Yet another option would be to bind the key to Esc instead of F19, as long as that's supported by your system, and use the esc prefix instead of the f19 prefix. That way you don't have to change the emacs configuration at all.

Make a key behave as another key

I want certain keys and key combinations to behave as other keys or key combinations in Emacs. For example, I want F5 to behave as a substitute for C-c for every possible combination involving it, or C-S- as C-. Is it possible to do that without manually rebinding all such key combinations?
The keys you are referring to are known as 'prefix keys'. A prefix key has its own keymap, so to make another key behave the same, you need to assign it to the same keymap. For control-c, you use the mode-specific-map:
(global-set-key (kbd "<f5>") mode-specific-map)
Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress to Emacs[1] until you hit another key. I'm not sure how to remap C-S- to C- within Emacs. You could do it system-wide with xmodmap, but that's probably not what you want.
[1] the control key (and shift, alt) do send a keypress to the operating system, but Emacs doesn't 'see' this unless there's another key pressed at the same time
I prefer
(define-key key-translation-map [f5] (kbd "\C-c"))
Here is a good resource.
To summarize the link given above: The disadvantage of global-set-key is that, when you define a key combination to enter a symbol, it doesn't work in isearch.
key-translation-map also has a problem. Imagine you defined a symbol | to execute a command and C-| to enter the symbol |, pressing C-| will execute the command.

emacs error: Key sequence M-x g starts with non-prefix key M-x

I have the following code in .emacs: (global-set-key (kbd "M-x g") 'gnus) to start Gnus with the keybinding M-x g. I obtain: error: Key sequence M-x g starts with non-prefix key M-x. How can I define keybindings starting with M-x? Is this a bad thing to do and should be avoided? I find it more intuitive since the "long version" is M-x gnus. Defining it as C-c g for example is no problem but then you start Gnus with C-c g and, for example, R via M-x R which is not very intuitive (in contrast to starting both via M-x + 1 letter
The key M-x is already bound to the command execute-extended-command, which then asks you to provide the name of a command to execute (in you case: gnus).
Since R is a command only one-character long, it looks like M-x R is a key sequence, but it's not: it's M-x followed by entering R in the minibuffer and you have to hit RET to validate your input.
In short:
you can not set key sequences beginning with M-x since this key is already bound to a command and is thus not a prefix (unlike C-c, which does nothing but wait for you to type another key, but should be reserved for bindings specific to the current modes).
the standard way to do things would be to continue starting gnus using M-x gnus or to rebind it to an entirely different key if you need to be very quick (you could for example use one of the F1-F12 keys)
if you really want to have a M-x + letter binding, you can define a one-letter alias to the command gnus, like this:
(defalias 'g 'gnus)

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:
(global-set-key (kbd "M-h") 'backward-kill-word)
(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)
I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character?
Best.
(I looked for libraries that may have overrided this command but I cannot find them)
On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.
If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.
This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.
There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is
(global-set-key [?\e delete] 'backward-kill-word)
The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following
C-h k M-<\delete> (to use your syntax)
or
M-x describe-key M-<\delete>
Emacs (for me) responds with:
M-DEL (translated from <M-delete>)
runs the command backward-kill-word,
which is an interactive compiled Lisp
function in `simple.el'.
It is bound to , M-DEL.
(backward-kill-word arg)
....
Which you can see shows that the representation for the key you want is M-DEL or M-delete.
Which is a long way of getting to the point that what you want is
(global-set-key (kbd "M-delete") 'backward-kill-word)
Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).
You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.
After not being able to find the library holding the conflict I found this webpage
http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding
Changing Key Bindings Interactively...
`M-x global-set-key KEY CMD '
Define KEY globally to run CMD....
Normally, C-z' is bound to the function
suspend-emacs' (when not using the X Window System), but you can
change C-z' to invoke an interactive subshell within Emacs, by binding
it toshell' as follows:
M-x global-set-key <RET> C-z shell <RET>
`global-set-key' reads the command name after the key. After you
press the key, a message like this appears so that you can confirm that
you are binding the key you want:
Set key C-z to command:...
And now the standard default is returned to by doing
M-x global-set-key M-del ...
backward-kill-word
But this is transient and must be done on each reload, any way to make this permanent?
Putting a command into the init.el is not overriding the other effect