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?

1
  • 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 8 '15 at 13:28
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)
3
  • 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? Sep 8 '15 at 5: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 8 '15 at 5:50
  • 1
    @reza.safiyat To prevent confusion, sticking to the (kbd ..) convention will be the most convenient for its consistency. Sep 9 '15 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.