Is there a quick way to unbind keys in Emacs? - emacs

I did a ctrl h b to view all my bindings in emacs. Now I want to unbind a lot of keys, simply because I never use those functions of Emacs and I don't want to perform them when I accidently press the bound keys! This also frees up a lot of keys for other tasks (for use with Cedet for example). So apart from global-unset-key, is there any method to remove bindings in bulk?
C-a move-beginning-of-line
C-b backward-char
C-c mode-specific-command-prefix
C-d delete-char
C-e move-end-of-line
C-f forward-char
C-g keyboard-quit
C-h help-command
C-k kill-line
C-l recenter-top-bottom
C-n next-line
C-o open-line
C-p previous-line
C-q quoted-insert
C-t transpose-chars
C-u universal-argument
C-v scroll-up
C-x Control-X-prefix
C-z suspend-frame
ESC ESC-prefix
I want to remove most of these bindings which are absolutely useless for me.

There's no built-in way to unset a lot of keys, because it's easy to do it yourself:
(Edited for strict correctness:)
(dolist (key '("\C-a" "\C-b" "\C-c" "\C-d" "\C-e" "\C-f" "\C-g"
"\C-h" "\C-k" "\C-l" "\C-n" "\C-o" "\C-p" "\C-q"
"\C-t" "\C-u" "\C-v" "\C-x" "\C-z" "\e"))
(global-unset-key key))
Although I have to say that most of the commands you call "useless" I would call "essential."
(Edited to add:)
As for freeing up keys for other tasks, there's plenty of unused key real estate:
Key sequences consisting of C-c followed by a letter are by convention reserved for users.
If you have an extra modifier available, like Option on the Mac or the Windows key on a PC, you can associate it with an Emacs modifier like super. I have super-b bound to browse-url-at-point, for example.
If you're not on a plain terminal, the shift key becomes available to distinguish key sequences. For example, I have shift-meta-b bound to bury-buffer.
For commands that are useful but not run often enough to warrant a dedicated key sequence, you can use defalias to provide a shorter name. In my .emacs file, I have (defalias 'ru 'rename-uniquely) and (defalias 'c 'calendar) (among many others).

global-unset-key and local-unset-key are useful, but it's worth having an answer to this question that points out that the general way to unbind a key (for any keymap) is to define a binding of nil:
(define-key KEYMAP KEY nil)
If you follow the code for either of those other functions, you'll notice that this is exactly what they do.

Related

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.)

redefined keys doesn't work correctly

I've redefined key bindings for some basic movement functions in my init.el file:
(global-set-key "\C-j" 'backward-char)
(global-set-key "\C-k" 'next-line)
(global-set-key "\C-l" 'forward-char)
(keyboard-translate ?\C-i ?\H-i)
(global-set-key [?\H-i] 'previous-line)
(global-set-key "\M-j" 'backward-word)
(global-set-key "\M-l" 'forward-word)
And in general (text editing) it perfectly works, but in some modes it executes multiple commands, e.g. in Buffer mode when I press C-k aside from moving the cursor down Emacs marks the listed buffer for deletion. Also, when I call helm-prelude with C-c p h and press one of these key bindings Emacs either doesn't react at all or, in case of C-k, clears the search bar. I thought that the purpose of global-set-key was to bind commands to specific keys everywhere, am I wrong?
Local (e.g., major-mode) keymap bindings trump global keymap (global-map) bindings. And minor-mode keymap bindings trump both of these.
There is a hierarchy of several keymap types that determines which maps take precedence. See the Elisp manual, node Controlling Active Maps (and nearby nodes about keymaps). The full hierarchy is a bit complicated, but most of the time what you need to be aware of is what I stated in the preceding paragraph.
Yes, the global keymap is only used when there is no binding for the key being pressed in a local keymap. For example, the buffer menu mode uses Buffer-menu-mode-map, where C-k is bound to Buffer-menu-delete.
You may have better luck using keyboard-translate to translate these keys to the "normal" Emacs bindings for those commands, i.e. C-p, C-n etc.

emacs get rid of C-x

Is there a way I can stop needing C-X before any shortcut and, for example, just use C-c to leave?
Also, if possible, what disadvantages could this have?
I'm looking for a simpler way of using GNU Emacs, but not sure if I can find one.
(this is too long for a comment)
Not really an answer but I know that some people hate the "C-x anything" in Emacs not just because you have to "type a lot of keys" but also because from a touch-typing point of view C-x doesn't make that much sense.
But of course in Emacs everything is configurable. I'm using "C-," instead of C-x and I honestly find it much easier this way.
I use my left pinky to hit CTRL, which is the key physically located at the left of 'a' on a QWERTY keyboard: i.e. the key that used to be CTRL but that now often is labelled CAPS-Lock [and you can remap it to CTRL].
I then touch-type ',' with my right hand.
To this end I added this to my config:
(define-key global-map [(control ,)] ctl-x-map)
You won't have less keys to type to do, say, a C-x C-c (which you can now do both by doing C-x C-c or by doing C-, C-c), but at least you won't be distorting your fingers as much ; )
Also note that as explained here (see user "scottfrazer"'s +40+ upvoted and accepted answer) it may be better to create a minor mode for all your key mappings instead of directly define a global mapping:
Globally override key binding in Emacs
My recommendation is to first learn the standard Emacs key bindings, then, after a while change whichever ones you think will do you the most good to change.
See also CUA mode, in the Emacs docs -- I don't use or recommend CUA mode, but a lot of people (esp. newbies?) use it to keep their cut, copy, paste key habits.
After looking at the source code of boon.el, I found out about ctl-x-map. C-x is a prefix key, prefix keys often have their own maps.
To use M-m for C-x shortcuts and unbind C-x, use this
(global-set-key (kbd "M-m") 'ctl-x-map)
(global-set-key (kbd "C-x") nil)
I think it is a very bad idea, because the C-x prefix can handle such situations like C-x s vs C-x C-s. And: you are really f*cked when you read emacs tutorials and have your own keybindings
If you want some other keybindings read ErgoEmacs Keybinding or How to Set Emacs's User Interface to Modern Conventions

How to invoke the buffer list in Emacs

I usually type M-x buffer-menu to switch buffers in Emacs. How can I do this with a shorter command? Its quite a long string to type.
Thanks!
You can use C-x b to change buffers. You have to enter the first few letters of the buffer name, and of course you can use completion. If you press TAB (the most useful key in Emacs), a list of (matching) buffers appears. You can click in this list to switch to a buffer.
You can bind buffer-menu to a key. Pick a key that's not used for another command — let's say f12 — and add the following line to the file ~/.emacs:
(global-set-key (kbd "<f12>") 'buffer-menu)
There are many other interfaces to changing buffers in Emacs, and they can be significantly more efficient than C-x b and C-x C-b. Since this tends to be a very personal choice, I recommend you experiment with a few and keep the one(s) you feel most comfortable with.
C-x C-b
As stated here
I'd highly recommend switching to a mode designed for efficient buffer switching.
If your version of Emacs is recent enough (22+):
M-x ido-mode
and then:
C-x b
to switch buffers, with incremental substring matching, C-s and C-r rotate forward and backwards through the matches.
If you have an older version of Emacs, it should have:
M-x iswitchb-mode
and then, as with ido-mode:
C-x b
opens up the minibuffer to let you choose the buffer to switch to.
Bind C-x C-b to buffer-menu. There is no sense leaving it bound to list-buffers. list-buffers is just a eunuch version of buffer-menu. ;-)
And you might want to try this: http://www.emacswiki.org/emacs/BufferMenuPlus
Try bs-show (in my opinion a way better than C-x C-b). You can bind it to F9 by adding this to .emacs:
(global-set-key (kbd "<f9>") 'bs-show)

Rebinding C-c to C-c

I'm using Viper, and I want to change its C-c and C-g to the original emacs functions. I can rebind C-g with (define-key viper-vi-global-user-map "C-g" 'keyboard-quit), but how can I rebind C-c, since it's a prefix key?
Thanks!
It may make sense for you to run M-x viper-set-expert-level with an argument of 2 ("Master"). As the viper-mode documentation explains:
2 -- MASTER: C-c now has its standard
Emacs meaning in Vi command state, so
most Emacs commands can be used when
Viper is in Vi state.
As you master viper-mode, you're meant to increase your expert-level setting gradually over time, making more Emacs features available to you (or, as the Viper documentation puts it, "To use Emacs productively, you must reach level 3 or higher").
The original binding for C-c can be set with the following:
(define-key viper-vi-global-user-map (kbd "C-c") 'mode-specific-command-prefix)
The info page for this is Prefix Keys.