tmux send-keys syntax - tmux

On the tmux man page I found no reference to how it names keys.
For example, to send ctrl + r to tmux you would do:
tmux send-keys C-r
and to send the esc key you do
tmux send-keys Escape
Is there a list which maps keyboard keys to how tmux sendkeys expects you to name them? I have a feeling that I missed a memo that its using some-long-forgotten-program's syntax for convenience.
Note, this is nothing to do with key bindings.

The key names used by send-keys are the same ones that bind-key uses.
From the Key Bindings section of the tmux manpage:
When specifying keys, most represent themselves (for example ‘A’ to
‘Z’). Ctrl keys may be prefixed with ‘C-’ or ‘^’, and Alt (meta) with
‘M-’. In addition, the following special key names are accepted: Up,
Down, Left, Right, BSpace, BTab, DC (Delete), End, Enter, Escape, F1 to
F20, Home, IC (Insert), NPage/PageDown/PgDn, PPage/PageUp/PgUp, Space,
and Tab.
Although they are not listed in the man page, there are also special names for keypad-specific keys: KP0 through KP9, KP/, KP*, KP-, KP+, KP., and KPEnter.
Several of the more cryptic key names (BTab, IC, DC, NPage, PPage) probably come from the terminfo library.
Emacs shares the convention of using C- and M- prefixes to indicate modifiers (I would not be surprised if there were earlier uses of this convention).

Related

Is it possible to bindkey Shift+Enter in zsh?

I'm starting to use zsh on macOS Sierra. I would like to have the following key mappings:
Enter => accept-line
Shift-Enter => accept-and-hold
However, I can't seem to differentiate between the two. I'm only able to get Enter, and Esc-Enter, but not Shift-Enter:
bindkey "^M" accept-line # Enter
bindkey "^[^M" accept-and-hold # Esc-Enter
bindkey "????" accept-and-hold # Shift-Enter
Is it possible to detect and handle Shift-Enter?
zsh (as well as other shells) do not act on key bindings but rather on key sequences received from the terminal. Converting key presses and combinations into key sequences is the responsibility of the terminal. You can retrieve the key sequence for a key combination by pressing Ctr+v followed by the key combination, e.g. Shift+Enter.
By default Enter and Shift+Enter (as well as Ctrl+v and Ctrl+Shift+m) all generate the identical key sequence ^M (at least in most common terminal emulators).
Fortunately, some terminal emulators allow to configure the key sequences sent. For example iTerm2 allows you to set customized key bindings that send escape sequences (in Profile > Keys), you should be able to define a sequence for Shift+Enter there, e.g. [[SE and can then make the appropriate settings in zsh: bindkey '^[[[SE' 'accept-and-hold'. (Unfortunately I do not have access to a Mac at the moment, so I could not test this).
This might answer your problem (can't put it into comment, don't have 50 rep). You might try # showkey --scancodes which gives you key codes and take a look into the manual-pages e.g. man zshzle and search for "code". I have tried to map the shift key without success. Might be it's not possible. Also have a look into bindkey -l wich gives you the keymaps and bindkey -M emacs for emacs keymap

tmux changed keybinding (resizep) not working as expected

I use vim, and so I wanted to change a couple of tmux's default bindings. In particular I wanted to change the resizing commands so that e.g. ctrl-b ctrl-k resize the split up by one position. I entered the following into my .tmux.conf:
bind-key C-k resizep -U
and it works, except that it only allows me to resize by one unit at a time before I have to hit ctrl again. In other words, I can't hold down ctrl and press b followed by k a bunch of times (while still holding down ctrl), whereas I can hold down ctrl, press b, and then press the up arrow key a bunch of times.
Does anyone know exactly why this is, or how I might replicate my desired behavior?
You need to specify the -r parameter in your command:
bind-key -r C-k resizep -U
As explained in tmux man page:
bind-key [-cnr] [-t mode-table] key command [arguments]
(alias: bind)
Bind key key to command. By default (without -t) the primary
key bindings are modified (those normally activated with the
prefix key); in this case, if -n is specified, it is not neces‐
sary to use the prefix key, command is bound to key alone. The
-r flag indicates this key may repeat, see the repeat-time
option.

How to use Ctrl-semicolon for prefix in tmux?

I want to use Ctrl-semicolon for tmux's prefix. But my conf doesn't work.
unbind-key C-b
set-option -g prefix C-\;
I found a similar article. But it's not for the prefix.
tmux bind semicolon
Terminal can't register a Ctrl-; keystroke. It's just not a valid character. If you look at the control characters in the below ascii table, you'll see Ctrl-; is not on the list.
I'm on OS X and when I type Ctrl - ; in the (terminal and in a "desktop" program) I get a bell sound indicating the character is not recognized or something.
As for the "favorite" prefix key: from what I saw reading other people's .tmux.conf files, Ctrl-a is the most popular choice. This makes sense because:
Ctrl-a was the default for GNU Screen, tmux predecessor
it's much easier to type than the default Ctrl-b especially when you remap caps lock to ctrl.
The downside to using Ctrl-a is that you can't use the same key in bash or vim, but that's easily solved by having the following binding in .tmux.conf:
bind-key 'C-a' send-prefix
With that, pressing the Ctrl-a twice will send the same character to the underlying program (eg bash or vim).
As others said, you can't bind to Ctrl-; because it's not a valid character.
I like that prefix because it's really easy to press when CapsLock is remapped to Ctrl.
My workaround, for Linux, was to remap Ctrl-; to Ctrl-B at the xkb level.
Xkb is the Xorg subsystem which handles keyboard layouts.
I'm using the us layout, so I modified the /usr/share/X11/xkb/symbols/us at line 42:
key <AC10> { [ semicolon, colon ] };
to
// key <AC10> { [ semicolon, colon ] };
key <AC10> {
type="BABEL_CONTROL_LEVEL3",
symbols[Group1]= [ semicolon, colon, b ]
};
This tells Xkb to generate for AC10 (the 10-th button in the C row) semicolon at level 1 (no modifiers), colon at level 2 (shift modifier) and b at level 3 (Ctrl modifier).
Level 3 in Xkb is not activated by Ctrl generally, for this reason I created a new key type, which I called BABEL_CONTROL_LEVEL3. You need to put its definition in /usr/share/X11/xkb/types/pc:
type "BABEL_CONTROL_LEVEL3" {
modifiers = Shift+Control;
map[Shift] = Level2;
map[Control] = Level3;
level_name[Level1] = "Base";
level_name[Level2] = "Shift";
level_name[Level3] = "Control";
};
You'll need to restart X or reboot.
List of resources which helped me with this:
https://unix.stackexchange.com/questions/205226/xkb-make-ctrlbackspace-behave-as-delete
https://help.ubuntu.com/community/Custom%20keyboard%20layout%20definitions
https://wiki.archlinux.org/index.php/X_KeyBoard_extension
If you want to use control-semicolon, you can try AutoHotkey.
This is my tmux & autohotkey settings.
Tmux:
set-option -g prefix 'C-\'
AutoHotkey:
^;::
Send ^{\}
return

Bindings with key sequences

Does Tmux supports key-bindings with key sequences like Vim does (e.g. bind-key ab kill-pane)? Or how can i emulate that?
I'm using tmux 2.3.
You can emulate key sequences by defining your own key tables and chaining them together.
For example, if I want <C-q>x to do something, I put the binding for 'x' into a key table "my-keys", then bind the key that activates that key table with switch-client (C-q):
bind-key -Tmy-keys x send-keys "my binding"
# Multi-key prefix for custom bindings
bind-key -Troot C-q switch-client -Tmy-keys
NOTE: I started with C-q, because it seems to conflict the least with the command line and Vim.
So, now you have every key at your disposal with a C-q prefix.
If you want more keys in your sequence, add another level of indirection:
bind-key -Tmy-keys x send-keys "my binding"
# Pane (i.e. 'W'indow commands like Vim with C-w)
bind-key -Tmy-keys-window-ctl s swap-pane
bind-key -Tmy-keys C-w switch-client -T my-keys-window-ctl
# Multi-key prefix for custom bindings
bind-key -Troot C-q switch-client -Tmy-keys
So, now I have swap-pane bound to <C-q><C-w>s.
This works because
<C-q> activates "my-keys" key table,
which has the binding <C-w>,
which activates "my-keys-window-ctl" key table
which has the binding s to call swap-pane
Tmux supports only single character key bindings (unfortunately).
So, only this:
bind-key a kill-pane
or this:
bind-key b kill-pane
Please note this is different from for example C-a (Ctrl-a) or M-a (Alt-a).
Even though we users write those with multiple characters and even have to press 2 keys to invoke them, both Ctrl-a and Alt-a are actually a single character for tmux (and in general to my knowledge).
Alternative
...might not be what you expect, but here it is:
# in .tmux.conf
bind a command-prompt -p "pressed a" "run '~/my_script %%'"
And the example my_script file:
#!/bin/bash
case "$1" in
b)
tmux kill-pane
;;
c)
tmux kill-window
;;
esac
Now after you reload your tmux.conf and press prefix + a you'll get a tmux prompt saying 'pressed a'.
Go ahead and press b and Enter. tmux kill-pane from the script will execute.
Similarly if you press prefix + a + c and Enter you'll execute another option from the script.
This kind-of mimics what you want with the addition of Enter key at the end.
Also, the provided script is extendable so you can add more "bindings" to get prefix + a + d + Enter etc..

Tmux special character support

I'm trying to remap the movement keys (between panes) in tmux to a Vim like style.
I don't like 'hjkl', and therefore I prefer to use the 'jkl;' keys. On my keyboard, the ';' character is 'ç' instead. I'm doing this:
bind j select-pane -L
bind k select-pane -D
bind l select-pane -U
bind ç select-pane -R
But it seems that tmux doesn't recognizes the 'ç' key. How can I solve this problem?
Currently, tmux does not really support binding keys whose core characters have multibyte representations. The internal key representation is mostly “8-bit plus modifiers”. Most terminals use UTF-8 these days, so your c-with-cedilla (U+00E7) is probably two bytes: C3 A7.
It should work if your terminal is using an 8-bit encoding. You would need to make sure your terminal is configured correctly, and also that your tmux client does not use UTF-8 (do not use the -u option, make sure LC_ALL, LC_CTYPE, and LANG all do not include UTF-8 or UTF8), do not enable the utf8 window option, and make sure your configuration file is saved with the same encoding that your terminal uses.
When parsing key strings from (e.g.) the bind-key argument, tmux treats any multibyte sequence (after stripping leading modifiers) as a named key (e.g. Up, PageUp, F1, KP0, etc.); see key_string_lookup_string in key-string.c. This is what causes the unknown key error message.
I have also previously written about this at Super User: tmux: trying to bind utf8 key.

Resources