How to set new keyboard shortcuts in emacs? - 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.

Related

Emacs, org-mode, evil-mode - TAB key not working

I've been working with VIM for decades, and I've become quite proficient in it. I was however sort-of... seduced by Emacs's org-mode, and in order to try it, I installed Emacs and Evil.
Evil satisfies most of my VIM-related muscle memory, so I proceeded with my testing of org-mode - and met my first issue: when I spawn Emacs in its own window (i.e. emacs plan.org) then the TAB key works, opening and closing my plan's sections just fine. However, TAB does nothing when I use Emacs in text mode (i.e inside my XTerms, via "emacs -nw plan.org"). And that's the state that I am mostly interested in, since I usually work from inside screen/tmux over SSH connections.
If it's a conflict with Evil-mode, I don't understand why - I am unaware of any TAB functionality in VIM's normal mode (which is what we're in when opening/closing org-mode sections).
Any Emacs-guru out there with a suggestion on why this happens?
Try
(setq evil-want-C-i-jump nil)
in your ~/.emacs before
(require 'evil)
Evil has, in evil-maps.el
(when evil-want-C-i-jump
(define-key evil-motion-state-map (kbd "C-i") 'evil-jump-forward))
That should give you org-mode Tab functionality back
I have almost no experience with terminals. However, I know that TAB is equivalent to C-i. Maybe that one would go through the terminal? If that works, you could add some key bindings for every TAB operation?
Try maybe C-h k TAB as well to see if TAB if sent on the wire.
(define-key evil-normal-state-map (kbd "M-i") 'evil-jump-forward)
(define-key evil-normal-state-map (kbd "M-o") 'evil-jump-backward)
I bind the function to other keys, so it's also work.

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)

Define key-bindings in emacs

I'd like to map a command in emacs to a key-binding. I want the command Control-l to have the same effect as the command Alt-x goto-line followed by a return (since that command first needs a return to be invoked and then a line number).
I modified the init file as follows:
(define-key (M-x goto-line) '\C-l)
but that didn't work. The error was that define-key was being given more than 1 arguments.
Does anyone know how to reset key-bindings in emacs?
Thanks!
M-g g is the default shortcut for goto-line. You might want to try that.
To redefine C-l use:
(global-set-key (kbd "C-l") 'goto-line)
Easiest way to customize lots of keybindings is to install John Wiegley's bind-key module, which is a part of use-package Lisp package. Solution in your init.el:
(require 'bind-key)
(bind-key "C-l" 'goto-line)
Minor modes keys usually override global keys, so if you don't want such behavior, use function bind-key* instead. The package is on MELPA, if you don't know what is it, quickly learn about Emacs package management (should take you 2 minutes to set up MELPA as your repository).
The main problem with keybindings in Emacs is that minor modes keys often override your custom ones. In vanilla Emacs people workaround by creating a minor mode for your own keybindings. If you really wanna understand how Emacs keys work, read Key Bindings # Emacs Manual and Keymaps # Elisp Manual carefully.
I have set as (global-set-key (kbd "C-x g") 'goto-line). You can use that or (global-set-key (kbd "C-l") 'goto-line). I would personally do not touch the C-l key from its default behavior.
If you must use M-x define-key, use
(define-key global-map (kbd "C-l") 'goto-line). The 1st argument to define-key is a KEYMAP.

How to modify aquamacs keyboard shortcuts

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.

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.