3

I've set the following key bindings in my .emacs file:

(global-set-key (kbd "C-S-M-w") 'windmove-up)
(global-set-key (kbd "C-S-M-s") 'windmove-down)
(global-set-key (kbd "C-S-M-d") 'windmove-right)
(global-set-key (kbd "C-S-M-a") 'windmove-left)

(global-set-key (kbd "C-S-a") 'shrink-window-horizontally)
(global-set-key (kbd "C-S-d") 'enlarge-window-horizontally)
(global-set-key (kbd "C-S-s") 'shrink-window)
(global-set-key (kbd "C-S-w") 'enlarge-window)

They work just fine when they're in their own window. However, if I run it in terminal (emacs -nw) the keybindings aren't loaded. Even after loading the .emacs file I still don't have the keybindings.

It's the same story when I use a emacs daemon and open in client vs in terminal. I'm on a linux machine if that matters.

Drew
  • 29,895
  • 7
  • 74
  • 104
roro
  • 931
  • 5
  • 21

3 Answers3

8

Problem is not with emacs, problem is combinations of modifier keys (Control, Shift and Alt) behave quite poorly in most terminal programs. Similar questions keep popping up here and in many other places, including superuser, for instance: emacs - [control shift up] doesn't work, and https://superuser.com/q/230852. You need to test it in your specific terminal - but checking Gnome terminal for instance shows that C-S- is indistinguishable from just C-, so most of your bindings aren't even making it to emacs properly

If you need to convince yourself use C-h k and then the combinations you are missing. You will see that when you run in a terminal those combinations are stripped of some modifiers.

I have been through similar experience and came to the conclusion that fighting with terminals isn't worth the effort. I suggest you remap the key combinations that need more than one modifier to something else, when you are in a terminal. (I ended up remapping windmove commands to F-keys, for instance.) Alternatively, I can recommend either using evil leader key (if you use evil), or God mode otherwise. That drastically reduces the need for multiple modifiers.

Community
  • 1
  • 1
Yuri Steinschreiber
  • 2,648
  • 2
  • 12
  • 19
1

xterm can be made to do this; other terminals cannot.

If you change the goal to use function keys, you can get further, because without changing the configuration, xterm sends distinct escape sequences for the various combinations of the modifiers shift, control, alt and meta when applied to function- and cursor-keys.

The likely suspect for "Terminal" would be one of the VTE-based terminal emulators such as gnome-terminal. That copies a fair-sized chunk of this part of xterm's behavior, so you could experiment with your configuration for function-keys, decide what makes sense and use those settings.

VTE's behavior is undocumented. But you can read the original in XTerm Control Sequences.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

Step one is to get your terminal to send escape codes that you can later assign a meaning to in emacs. Edit your .Xdefaults file to add as many of these as you need. Here's an example using xterm (typos possible, as I can't cut-and-paste from my work PC):

*VT100*translations: #override \n\
    ~Ctrl ~Shift <KeyPress> BackSpace: string(0x7F)\n\
    Ctrl ~Shift <KeyPress> BackSpace: string("\033[27;5;8~")\n\
    Ctrl Shift <KeyPress> BackSpace: string("\033[27;6;8~")\n

    Ctrl Shift ~Meta <KeyPress> A: string("\033[27;6;65~")\n\
    ...
    Ctrl Shift ~Meta <KeyPress> Z: string("\033[27;6;90~")\n\

    Ctrl Shift Meta <KeyPress> A: string("\033[27;8;65~")\n\
    ...
    Ctrl Shift Meta <KeyPress> Z: string("\033[27;8;90~")\n\

XTerm*vt100.modifyOtherKeys: 1
XTerm*vt100.formatOtherKeys: 0

The key sequences can be anything (and I've seen a lot of undocumented key sequences), but the closest thing to a "standard" can be found here: http://invisible-island.net/xterm/ctlseqs/ctlseqs.html.

Step two is to get emacs to assign these new escape sequences to key sequences it understands:

; xterm-specific options
(unless window-system
    (define-key key-translation-map "\C-[[27;6;65~" (kbd "C-S-a"))
    ...
    (define-key key-translation-map "\C-[[27;6;90~" (kbd "C-S-z"))

    (define-key key-translation-map "\C-[[27;8;65~" (kbd "C-M-S-a"))
    ...
    (define-key key-translation-map "\C-[[27;8;90~" (kbd "C-M-S-z"))

    ; other xterm-specific options here
)

Within the ..., increment the last number before the ~ by one, so A=65, B=66, ..., Z=90.

teadotjay
  • 1,395
  • 12
  • 15
  • Something like that. But it works only for xterm, and OP likely thinks that "terminal" is a different program. – Thomas Dickey Aug 03 '16 at 20:57
  • @ThomasDickey As you mention in your answer, many terminal emulators copy xterm's behavior, to some extent, though they often capture key combinations for their own purposes. It's likely the OP doesn't realize the limitations of the terminal and might want to try other terminals (like xterm) to see if they are a better fit. – teadotjay Aug 04 '16 at 00:31
  • That was why I answered (better than making comments). – Thomas Dickey Aug 04 '16 at 00:33