0

I am new to the world of Emacs, so please forgive me if my question is too trivial.

I am trying to use standard console keybindings for Emacs too, by editing the configuration file. A few of them are in Emacs already, but I wanted to added some more.

I wanted to add keybindings for killing a part of the text.

My entries:

(global-set-key [C-h] 'backward-delete-char)
(global-set-key [C-w] 'backward-kill-word)
(global-set-key [C-u] 'backward-kill-sentence)

But I couldn't get these to work. I searched online and tried to use this, adding the following lines before the above lines:

(setq help-char nil)          ; To enable C-h for 'backward-delete-char
(setq kill-region nil)        ; To enable C-w for 'backward-kill-word
(setq universal-argument nil) ; To enable C-u for 'backward-kill-sentence

But that didn't help either. What am I doing wrong? How can I get it to work?

reza.safiyat
  • 184
  • 3
  • 14
  • 2
    FWIW, you really, **really** do *not* want to rebind `C-u` to anything. `C-u` in Emacs is basic, pretty much hardcoded, fairly low-level, and used everywhere. Pick another key - really. – Drew Sep 08 '15 at 13:28

1 Answers1

3

There are multiple ways to specify the key you want to bind. [C-u] doesn't work; if you're supplying a vector, you have to use it a little differently. From that link:

In the vector representation, each element of the vector represents an input event, in its Lisp form. See Input Events. For example, the vector [?\C-x ?l] represents the key sequence C-x l.

So this works:

(global-set-key [?\C-u] 'backward-kill-sentence)

Alternately, you can have a vector containing lists:

(global-set-key [(control ?u)] 'backward-kill-sentence)

But I find that using kbd makes things much easier to read. For example:

(global-set-key (kbd "C-u") 'backward-kill-sentence)
zck
  • 8,984
  • 2
  • 31
  • 65
  • That is seriously weird. Your solution works, but what is the problem with `[C-u]`? I used `[C-tab]` just a few lines before that and it works flawlessly. What might be the reason behind `[C-u]` not working? – reza.safiyat Sep 08 '15 at 05:48
  • I'm not sure of all the rules here, honestly. I'm having a problem finding a single location with all the rules for how to specify keys. It might be related to treating the `u` in `[C-u]` not as the character `u`, but that's just a guess. – zck Sep 08 '15 at 05:50
  • 1
    @reza.safiyat To prevent confusion, sticking to the `(kbd ..)` convention will be the most convenient for its consistency. – Kaushal Modi Sep 09 '15 at 13:01
  • zck what can i do for those who have minor-mode? – Rajanboy Apr 02 '22 at 15:35