How to modify aquamacs keyboard shortcuts - emacs

I'd like to redefine some of the default aquamacs shortcuts.
I've tried putting this in my preferences.el file:
(global-unset-key (kbd "A-l"))
(global-set-key (kbd "A-l") 'forward char)
but it doesn't change the behavior of CMD-l

check the key descriptor as JF Sebastian said. But there is another typo in what you have written, it should be something like:
(global-set-key [(meta l)] 'forward-char)
(I am using meta, but by default aquamacs doesn't seem to have command has meta, not sure what it is)
You can check to what the key is mapped by typing:
c-h c
and then the key combination you want to check. It will tell you in the minibuffer. You can thus check if your binding worked fine without restarting etc.

Related

How to set new keyboard shortcuts in emacs?

I am new to emacs.
Is there a way set new key binding permanent for future use. That is I have a set of key bindings I would like to use in all of my future emacs sessions.
The following command is getting deleted (or forgotten) every time I quit emacs.
M-x global-set-key new key-binding command
How to save this key binding for future use?
Save the definitions into your .emacs file located in your home directory.
For example, I have the following there:
(global-set-key "\M-m" blink-matching-open)
This is covered comprehensively in the manual.
A number of answers above (and elsewhere on SO) didn't work for me. First, the meta key is referred to by something else in my emacs 24.3.1 with GTK+ version 3.10.7 (on Ubuntu 14.04).
A number of options pre-existing on my init.el didn't work.
There options on my init.el didn't work (and they include the one in here):
(a) (global-set-key (kbd "M-<up>") 'comment-region)
(b) (global-set-key (kbd "<M-up>") 'comment-region)
(c) (global-set-key [(meta up)] 'comment-region)
Then, I discovered that when I do a M-x describe-key and pressed metakey + up cursor arrow, the description said the key was <s-up>. I tried this:
(a) (global-set-key '<s-up>' 'comment-region)
Which didn't work either.
I then tried:
(a) (global-set-key [s-up] 'comment-region)
Which is the only one that worked after I exited the session.
I think a number of things need to be improved for both the emacs manual and the in-editor help with respect to keyboard shortcuts. As a nearly 20-year user of emacs, I can vouch for this. The OP's confusion is legitimate.

Emacs/elisp: global-set-key bindings not taking effect for Meta-<down> or -<up>?

I am trying to bind M-<up> and M-<down> to scroll-down-line and scroll-up-line respectively as indicated here: https://stackoverflow.com/a/16229080/562139.
This is what I have in my .emacs:
;; Key bindings
(global-set-key (kbd "M-g") 'goto-line)
;; Scroll line by line
(global-set-key (kbd "M-<down>") 'scroll-up-line)
(global-set-key (kbd "M-<up>") 'scroll-down-line)
Problem:
The scroll key bindings are not taking effect, while the one for goto-line does.
When I run M-x scroll-down-line however, emacs prompts me and says
"you can run the command with <M-down>"
Note:
When I run global-set-key (kbd "M-<down>") 'scroll-up-line) or (global-set-key (kbd "M-<up>") 'scroll-down-line) directly in the mini-buffer, the bindings take effect! However, I seem to have noticed through the corner of my eye when I do the latter, that pressing M-<up> actually sends something like ESC ESC-<up>.
I'm foxed. What gives?
Note: I am running emacs 24.3 in a terminal (via iTerm on OSX with Option key mapped to ESC+) over SSH to a RHEL5 virtual machine.)
Update
I followed the suggestion in this answer and found that pressing M-<up> results in something completely different:
ESC <up> (translated from ESC M-[ A) runs the command
scroll-down-line, which is an interactive compiled Lisp function.
It is bound to <M-up>, ESC <up>.
(scroll-down-line &optional ARG)
I'm going to try binding that key sequence to the function and check the result.
Try starting Emacs without your init file: emacs -Q, and see if you can reproduce the problem.
I do not see the problem, with Emacs 24.3 in terminal mode.
What you saw briefly was probably ESC <up>, which is equivalent to M-<up>.
Did you perhaps mean to type "When I run M-x scroll-up-line (instead of down)?
I suspect that you are in some mode that gives a local binding or a minor-mode binding to these keys, which overrides the global binding. To test that, try in a buffer that is in fundamental mode. If that is the case, then to override that overriding you will need to also bind the keys in that mode's keymap.
If you cannot repro the problem starting from emacs -Q then bisect your init file (~/.emacs) recursively until you find the culprit code.
Seems key got lost in translation.
Planted a forward-paragraph at openSuse that way:
(global-set-key [(meta down)] 'forward-paragraph)

Can't map Ctrl + minus in Emacs in Mac OS X

I'm trying to map Ctrl + minus ("C--") to undo in Emacs 24.3 (from http://emacsformacosx.com) in Mac OS X 10.8.4, but I can't get it to work. There seems to be some very global key binding for decreasing the font size, which I can't seem to override. Can anyone tell me what I'm doing wrong?
I have the following in my .emacs.
(global-set-key (kbd "C--") 'undo) ;; Doesn't work
(global-set-key (kbd "C-u") 'undo) ;; Just for testing, does work
When I press Ctrl+U, it triggers undo, but when I press Ctrl+minus, it decreases the font size. It might be simply that I should use something other than "C--", but it looks like it should work. I checked the key bindings (via C-h b) and there, C-u is bound to undo, but C-- is bound to text-scale-decrease. It would probably be possible to find where that key is bound and get some clue, but my Emacs-fu is too weak.
I'm using the graphical version of Emacs, not the terminal version.
With these type of problems I usually
try f1 k and right after the key combination that I'm having a problem with,
C-- in your case.
One of two things should happen:
Nothing happens - this means that the shortcut is being intercepted
at the level of the operating system.
It gives you a description of a function that's being called.
It's probable that it was set by either your major mode or one of the minor modes.
So you should investigate that as well, searching though the references
to this function, which is text-scale-decrease in you case.
After you find either global-set-key, or local-set-key or define-key
with this function, either comment it out, or better just
call the same function with same shortcut and nil in your ~/.emacs.
UPD: how to unset a key
When you find that some source that you're loading e.g. starter-kit is setting the key,
you just need to unset it later in the same way:
If it was set with (global-set-key (kbd "C--") 'text-scale-decrease),
you unset it with (global-set-key (kbd "C--") nil).
If it was set with (define-key markdown-mode-map (kbd "C--") 'text-scale-descrease),
you unset it with (define-key markdown-mode-map (kbd "C--") nil).
If it was set with
(add-hook 'markdown-mode-hook
(lambda()(local-set-key (kbd "C--") 'text-scale-descrease))
you unset with
(add-hook 'markdown-mode-hook
(lambda()(local-set-key (kbd "C--") nil))

Bind command to C-RET in Emacs

Say I have some interactive function in Emacs my-function, how can I bind it to Ctrl + RET?
I have tried with:
(global-set-key (kbd "C-RET") 'my-function)
and
(global-set-key (kbd "C-return") 'my-function)
but none of them seem to work. is this at all possible?
Always remember that kbd very conveniently accepts the exact same syntax that Emacs gives you when you ask it about a key sequence, so you never ever have to guess.
C-hkC-RET tells me:
<C-return>
therefore I would use (kbd "<C-return>")
OTOH, when running Emacs in my terminal, C-hkC-RET tells me:
C-j
because C-RET isn't a valid control character in a terminal, and therefore Emacs isn't receiving the same input that it gets in GUI mode (so I wouldn't be able to use that binding in my terminal).
This should work:
(global-set-key [(control return)] 'my-function)
It works for me, but may not in a terminal as per #phils's answer.

emacs-nox 'C-,' & 'C-.' keybindings not working

I've got a couple emacs keybindings as follows:
(global-set-key (kbd "C-,") 'beginning-of-buffer)
(global-set-key (kbd "C-.") 'end-of-buffer)
Recently, I realized these don't work in emacs-nox. Is it is possible to get them working, or is this just a limitation of emacs-nox?
By popular demand:
It's probably a limitation of your terminal.
Most terminals I've used don't send any keycodes at all for C-, and C-..
You can check this by executing M-x describe-key (usually bound to C-h k), and then typing C-, and/or C-.. If Emacs does nothing when you hit the keys, it's your terminal.
On my emacs(21), \C-. and \C-, don't register as keyed. I use terminal through PuTTY. The default keybindings \M-< and \M-> works fine for me for beginning-of-buffer and end-of-buffer respectively.
So either use the default keys or set some other keys if your emacs-nox does not register them as keyed in.