can I override next session binding <prefix>-) in tmux? - 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!

Related

tmux: list all the tmux windows with last 5 lines of output

I am running a lot of tmux session like
Each tmux session is started as:
today=`date +%Y-%m-%d-%H_%M_%S_%N`
tmux new-session -d -s "$today" zsh /home/path/to/script.sh "with_params"
If i want to only view the list of tmux sessions. I can do by
tmux ls | awk '{print $1}';
Now what i want is to monitor their output using the while loop to show the session name and the recent output:
while true;
do;
echo "##########################################"
??? For list of tmux session; do
sleep 1;
??? session name
??? recent last 5 lines of output
done
echo "##########################################"
done;
??? : What commands should i use
You can use capture-pane to show the last five lines of output:
tmux capture-pane -p -S- -E-|sed '/^$/d'|tail -5
Add -t to specify the pane you want to see - if you just give a session it will use the active pane in the current window.
Add -e if you want colour sequences included.

binding the shift key in .tmux.conf doesn't work

This is my .tmux.conf file, placed in the home directory.
unbind C-b
set -g prefix S-a
bind S-a send-prefix
bind r source-file ~/.tmux.conf
I am properly loading it with tmux source-file ~/.tmux.conf. However, doing shift+a to initiate the prefix doesn't work.
However if I replace S-a with C-a, it'll work (ctrl+a).
What am I doing wrong?
You can't and it's not a good idea, the shift key is by no way meant for that. Take a look in the man tmux, section KEY BINDINGS for the list of available keys. More info are available here https://unix.stackexchange.com/a/140010
Solution 1
Run: (WARN: Save your work in all sessions first)
tmux kill-server
Solution 2
In your .zshrc/.bashrc file, add the following line:
[ -z "${TMUX}" ] || tmux YOUR_COMMAND
e.g. YOUR_COMMAND = set -g prefix S-a.

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 }

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

Settings to move to end of line using tmux

Annoying question to have to ask! In bash (or anywhere.. like when editing this question) I can do CTRL + left|right to move left left or right to different bits of whitespace. With tmux running.. this does work.. it doesn't do anything. How do you do the same thing but with tmux?
Thank you.
You can do it by binding C-Left and C-Right to "send-keys M-b" and "send-keys M-f" in your ~/.tmux.conf file like this:
bind-key -n C-Left send-keys M-b
bind-key -n C-Right send-keys M-f
You don't. bash, respectively readline, has an idea of what you typed and can therefore jump as many characters until the next "word boundary". tmux does not have this information. Also, in tmux and the ANSI code space, I would interpret "end of line" in fact as \r\e[xB (with x being the size of the window), though that did not match up with your expectation.

Resources