Emacs: Use a function key (F19) as meta - emacs

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.

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 unbound key bindings

How do I clear a binding or edit a binding an emacs package provide?
For instance I had keybindings M-c to capitalize a word.
After I install some 3rd party emacs package, it is changed to calc-dispatch.
I'd like to use M-c as capitalize as before, and set calc-dispatch to something else.
How can I do this in general?
The keybind maps are loaded by order. The keybind map which loaded later will have higher priority. This is why the local key map will override the global keymap, because the global key map is loaded before the local key map(the mode key map). Something is wrong here. Look phils's comment.
What I solve this problem is add a hook to that specify mode to disable that key bind and rebind it to other key in that key map.
First, you need to find the key-map name which defines the M-c bind to calc-dispatch.
It is usually the combination of mode name and mode-map.
For example, the name of python mode key map is py-mode-map.
Second, remove the M-c bind in that mode and rebind to other key using hook.
For example, in python mode, I want to remove the bind C-j (py-newline-and-indent). And rebind it to C-i. Because globally I bind C-j to linum-ace-jump. This is the similar case with yours.
(add-hook 'python-mode-hook
#'(lambda ()
(define-key py-mode-map "\C-j" nil)
(define-key py-mode-map "\C-i" 'py-newline-and-indent)))
What you ask for is:
(global-set-key (kbd "M-c") 'capitalize-word)
This is in general the way to set words globally.
Maybe if you want to substite the two, you can try this:
(substitute-key-definition
'capitalize-word 'calc-dispatch (current-global-map))
(define-key KEYMAPNAME (kbd "KEYCOMBO") 'FUNCNAME)
Is for specific mode. For example: (define-key emacs-lisp-mode (kbd "M-c) 'capitalize-word).
(global-set-key (kbd "M-c") nil)
Is to generally unbind a key (globally).
You can easily find more on this by just googling.

Cannot get to bind Enter to 'newline-and-indent in Emacs !!! Very annoying

Cannot get to bind Enter to newline-and-indent in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter], (kbd "enter"), (read-kbd-macro "enter"), (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp
function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If use-hard-newlines' is non-nil, the newline is marked with the
text-propertyhard'.
With ARG, insert that many newlines.
Call auto-fill-function' if the current column number is greater
than the value offill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global
or local binding that gets in the way. trying to remap C-j
also doesnt work.
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode, so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that.
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent.
If you open a new file, activate ruby-mode and try the previous
command you will see why it doesn't work. Because ruby-mode doesn't
have newline-and-indent but rather
reindent-then-newline-and-indent. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode is not the only mode to do so, like paredit-mode which
uses paredit-newline.

Shift selection and page up/down in emacs 23

I’m trying to replace pc-selection-mode with the new shift-select-mode with emacs 23. It works generally well, only the shift+pgup/down keys don’t create a selected region and I can’t find a confguration setting where I could tell emacs I want these keys too shift translated. Are the supported keys hardcoded? It would be so unlike emacs, so there must be some setting somewhere which I overlook. Any ideas?
I removed PC-select mode and this just worked. For the record, here's what I get for the Page-Down key from describe-key:
<next> (translated from <S-next>) runs the command scroll-up, which is
an interactive built-in function in `C source code'.
It is bound to <next>, C-v.
(scroll-up &optional ARG)
...
With pc-select-mode enabled, Shift-PageUp is bound to scroll-down-mark and Shift-PageDown scroll-up-mark. scroll-down-mark is from pc-select.el so you have to require it before binding the keys (you don't have to enable pc-select).
Binding keys as mentioned can be done evaluating:
(require 'pc-select)
(global-set-key (kbd "S-<prior>") 'scroll-down-mark)
(global-set-key (kbd "S-<next>") 'scroll-up-mark)