24

I find even Ctrl+b to be very emacs like but I understand the point. I'm wondering if I could bind it to a single keypress of a key I don't other wise use? namely Super_L (also known as the left windows key. for why I say Super_L start xev in a terminal and press that key)

xenoterracide
  • 59,188
  • 74
  • 187
  • 252

4 Answers4

21

Super_L is an X keysym. Tmux runs in a terminal. It is up to your terminal emulator to transform a keysym into a character sequence. So you would have to configure both your terminal emulator and tmux.

Looking at the tmux documentation, the prefix can only been a known key name with an optional modifier. So you can set the tmux prefix to a key combination you don't use, say M-F12, and get your terminal to send the character sequence for M-F12 when you press Super_L. With a little more work, you could use a key that your keyboard probably doesn't have (tmux accepts F13 through F20 as key names, but they have to be declared in terminfo).

On the terminal emulator side, you would have to arrange for Super_L to generate the key sequence \e\e[24~ (for M-F12) or \e[34~ (for F20) (where \e is the escape character). How to do this depends on the terminal emulator (and some aren't configurable enough to do it). With xterm, it's done through X resources:

! Make Super_L act as Meta+F12
XTerm.VT100.translations:       #override \
    <Key>Super_L:  string("\033\033[24~")

You may hit a snag that Super_L is normally a modifier, and modifier keys don't always work when a non-modifier is required. If you don't want Super_L to be a modifier, you can take its modifier away, or (less confusingly) use a different keysym for the physical key. This can be done through xmodmap (old-fashioned and simple to understand), through xkb (the modern, poorly-documented, powerful and complex way), or perhaps through your desktop environment's GUI configuration tool.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252
  • 1
    would this only work in X then? e.g. if I shelled into my system remotely without X forwarding it wouldn't work. – xenoterracide Sep 11 '10 at 19:30
  • 3
    @xenoterracide: either I don't understand your question or you don't understand my answer. Super_L is an X keysym, so you presumably have an X server somewhere (if you were logging in from Windows, I suppose you'd call the key the left Windows key). Tmux runs in a terminal, and reads its input as bytes, with function keys translated into escape sequences. A remote login is transparent, ssh just transmits the bytes that make up the escape sequence. – Gilles 'SO- stop being evil' Sep 11 '10 at 21:04
  • 1
    Just tried this on a Mac and I might save others some time. Looks like iTerm2 does not support sending F13-F20. See http://code.google.com/p/iterm2/issues/detail?id=1630 – balu Jan 12 '12 at 13:01
  • @Gilles'SO-stopbeingevil' so how would one program this on the terminal emulator side without "hard-coding" it in .Xresources ? xmodmap -e "keysym Super_L = Meta+F12 does not work and substituting for ..... = string("\033\033[24~") does not work either. Can you tell I have a hard time understanding the https://invisible-island.net/xterm/xterm.faq.html documentation ??? :) – nass Nov 17 '21 at 11:42
  • @nass I don't understand what you're asking. What do you want to do? – Gilles 'SO- stop being evil' Nov 17 '21 at 12:02
  • @Gilles'SO-stopbeingevil' what you suggest: i.e. use an xmodmap to do the same that you describe that I could do in .Xresources. that is, send a "M-F12" when I press "Super_L". – nass Nov 17 '21 at 14:51
  • 1
    @nass That's not possible. You can only map a keycode to a keysym, with the possibility of changing which keysym each keycode maps to based on modifiers. You can't change which modifiers are considered pressed. – Gilles 'SO- stop being evil' Nov 17 '21 at 15:50
  • @Gilles'SO-stopbeingevil' OIC. I am afraid I still cannot get the above to work: 1) appended the above in .Xresources . 2) xrdb -load ~/.Xresources 3) add set prefix M-F12 in ~/.tmux.conf 4) start tmux. Pressing "Super_L" then still does not act as the default prefix combination (="C-b"). What step am I missing ? – nass Nov 17 '21 at 17:52
4

You can't. Binding a key will call the cmd_bind_key_parse function from cmd-bind-key.c which in turn will (eventually) call key_string_get_modifiers from key-string.c:

/* Find modifiers. */
105     int
106     key_string_get_modifiers(const char **string)
107     {
108         int modifiers;
109         
110         modifiers = 0;
111         while (((*string)[0] != '\0') && (*string)[1] == '-') {
112             switch ((*string)[0]) {
113                 case 'C':
114                 case 'c':
115                 modifiers |= KEYC_CTRL;
116                 break;
117                 case 'M':
118                 case 'm':
119                 modifiers |= KEYC_ESCAPE;
120                 break;
121                 case 'S':
122                 case 's':
123                 modifiers |= KEYC_SHIFT;
124                 break;
125             }
126             *string += 2;
127         }
128         return (modifiers);
129     } 

The tmux.c contains the modifier key #define declarations and from that file we have:

106     /* Key modifier bits. */
107     #define KEYC_ESCAPE 0x2000
108     #define KEYC_CTRL 0x4000
109     #define KEYC_SHIFT 0x8000
110     #define KEYC_PREFIX 0x10000 

On the surface though, it doesn't look too hard to modify; maybe a weekend (famous last words ;)) project?

gvkv
  • 2,738
  • 5
    you're missing a step here: tmux is a text mode program, it reads key sequences, not X keysyms. So it's actually possible, by configuring both the terminal emulator and tmux properly. – Gilles 'SO- stop being evil' Sep 06 '10 at 21:15
2

I have not been able to set a prefix to a custom modifier key, but I did manage to define tmux bindings in combination with a custom modifier key under Gnome in combination with Metacity. For example, to map Mod4+k and Mod4+j to move to current panel up and down respectively:

gconftool-2 --set /apps/metacity/keybinding_commands/command_1 --type string "tmux select-pane -D"
gconftool-2 --set /apps/metacity/keybinding_commands/command_2 --type string "tmux select-pane -U"
gconftool-2 --set /apps/metacity/global_keybindings/run_command_1 --type string "<Mod4>j"
gconftool-2 --set /apps/metacity/global_keybindings/run_command_2 --type string "<Mod4>k"

This allows for tmux bindings in combination with for example the Windows key. Something along those lines works for any window manager that allows to define global keyboard shortcuts (Compiz, KWin, etc.).

-1

Seems you want this: https://lists.gnu.org/archive/html/screen-users/2009-12/msg00144.html