Emacs keybinding for "\C-' " - emacs

How do you set a function to \C-' ? when I try to do :
(global-set-key "\C-'" 'myfunct)
it gives me "Invalid modifier in string".

What is \C-'? Do you want something to happen when you type backslash+c+quote? Or do you mean C-' (control+quote)? If it's the latter (and I guess it is), then you should use
(global-set-key (kbd "C-'") 'myfunct)

For any key sequence you want to use, press C-h k and then press the key sequence you want to use. Then it tells you the name of the key. That name is what should be used (with the code in the accepted answer).

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)

In Spacemacs (or Emacs), is it possible to bind keys to other keys?

Here is one thing that has been hard to find out. In Spacemacs (or Emacs), is it possible to have a given key combination set to execute whatever another key combination is supposed to do?
In other words, is it possible, for example, to have the F5 key always execute whatever the combination C-c C-c is supposed to do? Something like:
(global-set-key [remap (kbd "F5")] (kbd "C-c C-c"))
Let me give a concrete example for clarity. The key combination C-c C-c is often used to send the current buffer's entire code to interpreter/console, be it to send the buffer's code to the Python interpreter in case the code resides in a .pyfile, or to send it to a R console with package ESS in a .R file.
Now, suppose that one wants to have the F5 key set to do that for these two languages and maybe even more. One obvious solution would be to simply implement key bindings that are different for each package. However, it would be much easier if it was possible to simply say: whenever F5 is pressed, it should result in whatever command associated to `C-c C-c being called.
Is it possible to have such a thing? I am particularly interested in doing that for Spacemacs, but of course a more generic answer for Emacs is acceptable.
Try
(define-key key-translation-map [f5] (kbd "C-c C-c"))
It should solve your problem.

emacs key binding command affects another

Previously I used C-SPC to activate/deactivate mark, now I use expand-region package and set key binding to C-# like this:
(global-set-key (kbd "C-#") 'er/expand-region)
But this affected the C-SPC keybinding also, so it is also bound to expand-region.
What I need is C-# bind to expand-region and C-SPC to bind to old activate/deactivate mark.
Suggest you refer to:
set-mark-command not working emacs with C-SPC
and
https://www.gnu.org/software/emacs/manual/html_node/emacs/Setting-Mark.html
Quoting from the latter:
"Footnotes [1] There is no C-<SPC> character in ASCII; usually, typing C-<SPC> on a text terminal gives the character C-#. This key is also bound to set-mark-command, so unless you are unlucky enough to have a text terminal that behaves differently, you might as well think of C-# as C-<SPC>."
I think you'll find that they are not separate keys; C-SPC sends a code that's the same as C-#. I think that means you'll have to find somewhere else to bind one of the functions, (even if you have to override expand-region)
Apologies for a second answer... I think the first was wrong because I have now been able to make separate definitions for C-SPC and C-#, as described below.
This works to define C-# and C-SPC separately:
(global-set-key [?\C-#] 'beginning-of-line)
(global-set-key (kbd "C-SPC") 'end-of-line)
To give credit, I derived the answer from here: Rebind C-space in Emacs
after googling "emacs control space"
(Regarding your question, "what key should I bind this expand-region to?", I ordinarily use C-h C-k and type some key I don't think I use. Then look at the function that that key is bound to by default. If it seems useful to me, I try another key and keep looking. If I feel like I will never use the default definition, I redefine it for my own purposes.)

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.

How to simulate return in emacs minibuffer

What I want to do is, when I find myself using a function that prompts me for a value, and this value is ALWAYS the right one, to be able to bind the function to a key and auto-accept the answer, simulating a RET in the minibuffer.
Take for instance "ido-switch-buffer". When entered, it prompts you for a buffer name, and proposes the last visited one.
(It can do a lot more, that's why I dont want to re-define it, I just want a wrapper around it)
Say I want to switch between the last visited two buffers, how would I say that in my .emacs ?
You can do this with:
(switch-to-buffer (other-buffer))
You may bind any keyboard macro, which is essentially a sequence of keys, to a key. For example,
(global-set-key (kbd "C-c b") (kbd "C-x b <return>"))