Bindings with key sequences - tmux

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

Related

can I override next session binding <prefix>-) in tmux?

I want to bind this to something more convenient, like M-] without the prefix:
bind -n M-] send-prefix \; send-keys )
in my tmux.conf doesn't work.
What's my mistake?... I also tried C-] and some others. When I'm at a prompt, it just writes the ) character, so the prefix isn't being captured by tmux.
There doesn't seem to be a command for "next session", just the predefined binding.
I put these lines in my .tmux.conf:
bind j switch-client -n
bind k switch-client -p
As you can see the -n and -p arguments are next and previous. Enjoy!

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

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 can I preserve context while scrolling fast in tmux vi-copy mode?

I was wondering what the possible options are for tmux vi-copy bindings. I have the following in my .tmux.conf:
bind -t vi-copy e start-of-line
bind -t vi-copy r end-of-line
bind -t vi-copy v begin-selection
bind -t vi-copy V rectangle-toggle
bind -t vi-copy K page-up
bind -t vi-copy J page-down
bind -t vi-copy h cursor-left
bind -t vi-copy j cursor-down
bind -t vi-copy k cursor-up
bind -t vi-copy l cursor-right
bind -t vi-copy C-f cancel
Q1: I've had this in my config file for a while and have no idea where the options in the last column come from. A google search only showed me other forums that have code snippets like this. I can't find the documentation on these keywords. Any ideas? nope, not the manpage :)
Q2: If possible I would like to change K to (the tried and failed) half-page-up, or even to something like "go up 5 lines", to preserve context.
Tried and failed:
1) bind -t vi-copy K half-page-up
2) bind -t vi-copy K M-Up
3) bind -t vi-copy K C-u // already configured half page-up
Thx!
You can use tmux list-keys -t vi-copy to see a list of all the functions that are mapped in vi-copy mode. If you wish to see all possible commands you can look at the source code, specifically mode-key.c. I don't think there are any docs that list them all.
The mappings you are looking for are:
bind-key -t vi-copy 'K' halfpage-up
bind-key -t vi-copy 'J' halfpage-down
Unfortunately, this part is not documented well in tmux.
This is the full list of 'copy mode' commands in version 2.2 ripped out from the source code:
append-selection
back-to-indentation
begin-selection
bottom-line
cancel
clear-selection
copy-end-of-line
copy-line
copy-pipe
copy-selection
cursor-down
cursor-left
cursor-right
cursor-up
end-of-line
goto-line
halfpage-down
halfpage-up
history-bottom
history-top
jump-again
jump-backward
jump-forward
jump-reverse
jump-to-backward
jump-to-forward
middle-line
next-space
next-space-end
next-word
next-word-end
other-end
page-down
page-up
previous-space
previous-word
rectangle-toggle
scroll-down
scroll-up
search-again
search-backward
search-forward
search-reverse
select-line
start-named-buffer
start-number-prefix
start-of-line
top-line
All these commands apply to both vi-copy and emacs-copy modes but behavior might differ making it consistent with vi or emacs, though.
There are some shortcomings when tmux is 'in mode':
No sensible commands list or tab-completion for 'mode' commands
No way to combine commands in 'mode': you can only bind one action to a keypress and the set of actions is limited.
There's also a patch addressing these problems: http://ershov.github.io/tmux/
It adds full-fledged scripting support into tmux.
Using that, you can list all available commands:
info commands ::tmux::*
List all 'mode' commands:
info commands ::tmux::mode::*
List all 'copy mode' commands:
info commands ::tmux::mode::copy::*
Bind multiple actions in copy-mode:
bind-key -t vi-copy K tcl { scroll-up ; scroll-up }

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