I've got a keyboard, where pressing M-C-S is uncomfortable (Meta and control are under the thumbs and Shift is on the far end of the keyboard). However, PageUp, PageDown, Home and End are in a very comfortable place. I never use any of those keys though, so I was thinking, if it was possible to remap, say, Home to act like M-C-S, that would make my life a lot easier. I would be even happier, if the solution wasn't Emacs-only, but on the system level (it is okay if it is Linux-only).

  • 1
    You want Home to be Meta-Control-Shift-? – abo-abo Dec 26 '14 at 14:25
  • @abo-abo Either that, or Control-Shift, it's right under Meta, so it would be ok to press them both at once. – wvxvw Dec 26 '14 at 14:56
  • @Malabarba, it's not a duplicate. The question is quite interesting and apparently can't be solved with xmodmap. – abo-abo Dec 26 '14 at 15:14
  • @abo-abo the answer to the other question was "it can't be done with emacs". I don't think the additional complexity of this question will lead to a different answer. Still, I'll err on the side of niceness right now, and leave it open. The community can still vote to close if they agree with me. – Malabarba Dec 26 '14 at 15:19
  • @Malabarba, but OP isn't asking for specifically an "in-emacs" answer. A system-specific setting would be acceptable. – abo-abo Dec 26 '14 at 15:25

A pure Emacs solution that doesn't do exactly what you're asking, but close, would be to make e.g. Home be a key that adds the Ctrl, Shift and Alt modifiers to the next key that you press. That is, rather than press Home+key, you would press Homethen key.

Emacs has a built-in feature to add a modifier to the next key, bound to the unwieldy C-x @ letter by default, e.g. if you type C-x @ m a it's interpreted as M-a. This is implemented via function-key-map. Bindings in function-key-map are overridden by key bindings in most maps, including the global keymap; if you want your binding to take precedence, define it in input-decode-map instead. I've written a bit about it here.

You can adapt this mechanism to bind home to event-apply-control-meta-shift-modifiers.

(defun event-symbol-add-modifiers (symbol prefixes)
  (let* ((name (symbol-name symbol))
         (parts (split-string name "-"))
         (old-modifiers nil))
    (while parts
      (if (= (length (car parts)) 1)
          (setq old-modifiers (cons (aref (car parts) 0) old-modifiers)))
      (setq parts (cdr parts)))
    (while prefixes
      (if (not (memq (car prefixes) old-modifiers))
          (setq name (concat (list (car prefixes) ?-) name)))
        (setq prefixes (cdr prefixes)))
    (intern name)))
(defun event-apply-modifiers (event mask prefixes)
   ((numberp event)
    (logior mask event))
   ((symbolp event)
    (event-symbol-add-modifiers event prefixes))
   ((consp event)
    (cons (event-symbol-add-modifiers (car event) prefixes) (cdr event)))
   (t event)))
(defun event-apply-control-meta-shift-modifiers (ignore-prompt)
  "\\<function-key-map>Add the Ctrl, Meta and Shift modifier to the following event.
For example, type \\[event-apply-control-meta-shift-modifiers] & to enter Ctrl-Meta-Shift-&."
  (vector (event-apply-modifiers (read-event) #xe000000 '(?C ?M ?S))))
(define-key input-decode-map [home] 'event-apply-control-meta-shift-modifiers)

(event-apply-modifiers is a highly simplified version of Emacs's event-apply-modifier and may be inferior in some circumstances. Minimally tested.)

  • hi, this doesn't work with shift modifiers. Do you have any idea how to improve this? (defun event-apply-control-shift-modifier (_ignore-prompt) (vector (event-apply-modifiers (read-event) #x6000000 '(?C ?S)))) it doesn't trigger my C-S-s key binding – Amos Aug 13 '17 at 2:03
  • @Amos I forgot to mention that if you're using a key that already has a global binding and you want to bypass that binding, you need to use input-decode-map instead of function-key-map. – Gilles 'SO- stop being evil' Aug 13 '17 at 20:37
  • well it still doesn't work. here is my config file la.wentropy.com/LbMM and i use urxvt to send \030@aS – Amos Aug 13 '17 at 20:47
  • 1
    @Amos In your question, you asked how to “remap, say, Home to act like M-C-S”. What are you trying to do exactly? If you're now trying to do something different, you should ask a new question. – Gilles 'SO- stop being evil' Aug 13 '17 at 23:14
  • 1
    @Amos Well that's a completely different question! Which has been answered before for xterm: emacs.stackexchange.com/questions/1020/… In urxvt, you need to manually configure different escape sequences for modifier combinations such as Ctrl+Shfit with the URxvt.keysym. resources. – Gilles 'SO- stop being evil' Aug 14 '17 at 1:13

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.