tmux changed keybinding (resizep) not working as expected - tmux

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.

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

OS X: How do I use the tmux prefix correctly?

I am following a tmux tutorial which states (Control + b) + % should open a new pane. When I try to do this from tmux though, the Control + b keypress just gets converted into a character which displays on the command line.
Various tmux tutorials seem to treat Control + b as a special keypress, but it always just appears as a character on my command line. How do I use the tmux prefix correctly?
That tutorial you linked to in your question has you overriding the default Control-b with Control-a
try (Control-a) + %
this is a popular override. I use it. My ~/.tmux.conf has:
set -g prefix C-a
unbind C-b
bind C-a send-prefix
this is nice when you also remap your caps lock key to be control, the caps lock key and the 'a' key are right next to each other

Make Alt + h switch to the left pane in Tmux

I'm trying to make Alt-h switch to the left pane in Tmux. This does not work:
bind -n M-h select-pane -L
Pressing Alt-h in cat generates the following output in my terminal:
$ cat
^[h
That is, it looks like Alt generates ^[ in my terminal. So, in my Tmux configuration, I have also tried this binding:
bind -n ^[h select-pane -L
I have made "^[" both a literal two character string (i.e., "^" + "[") as well as the special "^[" character you can access in Vim. Neither work.
How to solve this problem?
EDIT: I'm primarily using xfce4-terminal as my terminal emulator, but I've tried using gnome-terminal as well as xterm, and the problem persists.
Actually the secret is how you enter ^[ ;-)
I assume you want to edit your .vimrc
vim ~/.vimrc
navigate to the correct position
press i
press ctrl-v
press ctrl-[ (on a german keyboard I have to press ctrl-altgr-8 because altgr-8 is [)
After that you should have the escape sequence generated required to write the binding.

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 send-keys syntax

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).

Resources