Cross compatible tmux configuration for vi mode - tmux

I have the following in my ~/.tmux.conf file
# improved (vi) copy paste
#
# vi mode in tmux
setw -g mode-keys vi
bind-key -t vi-copy y copy-selection
# select entire line
bind-key -t vi-copy v select line
Up till today this has always worked perfectly, problem is I've updated to the latest version :(.
tmux -V shows me that I'm currently at version 2.4
The vi-copy bindings are not working anymore. It seems that vi-copy mode is broken. If I execute the following:
CTRL+<leader> :list-keys -t vi-copy
The output is:
Unknown key-table vi-copy
According to this, it is a known issue
And you now have to do the following (extracted comment from the above link):
Basically you now need to bind your key in the copy-mode-vi table now,
look at how the default key bindings are done with "tmux lsk". For
your example: bind -Tcopy-mode-vi v send -X begin-selection
I'm sharing my tmux configuration across varous PC's, which are running different tmux versions, I hope somebody can explain how a cross compatible tmux configuration can be created, in which I can have the same keybindings and vi mode behaviour.

I have the following snippet in my tmux.conf:
if-shell "tmux -V | awk '{exit($2<2.4?0:1)}'" \
"bind-key -t vi-copy 'v' begin-selection; bind-key -t vi-copy 'y' copy-selection" \
"bind-key -T copy-mode-vi 'v' send -X begin-selection; bind-key -T copy-mode-vi 'y' send -X copy-selection"
Not the prettiest code I wrote but it seems to work.

Related

Byobu-tmux vi-copy doesn't work after system upgrade (KDE Neon)

I have in my ~/.byobu/.tmux.conf
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
so normally I could enter in copy mode by:
Prefix + [
then I could use vi commands to navigate and enter vi select mode by 'v'
yank the selection by 'y'
after that I could put/paste in the terminal by:
Prefix + ]
Now after a system upgrade of KDE Neon (v - begin-selection) doesn't work anymore.
Any ideas?
Because of newer Tmux version I had to change my config:
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection \; send-keys -X cancel
Thanks to 'rushiagr' (http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting-ubuntu)

Tmux: disallow switching into a particular pane

I would like to remove a pane from being selected using ctrl+b arrow-keys. This is because I have a pane displaying the time using tmux clock-mode -t 2 and I do not ever want to switch into that pane.
This solution is similar to a problem I answered on sister site unix.stackexchange, and is not perfect.
tmux has a flag for each pane saying whether it is in a mode. For example, display -p #{pane_in_mode} for a pane in clock-mode prints 1.
Unfortunately, you cannot distinguish between, say, copy-mode and clock-mode. However, if you are unlikely to have many panes in a mode at a time, you can write a small shell script to intercept the appropriate bindings, and test if the resulting movement ended up in a pane in a mode. If so, the script repeats the movement, probably to the next window.
Create the following file mytmux in your PATH and make it executable (chmod +x mytmux):
#!/bin/bash
# https://stackoverflow.com/a/51232832/5008284
noclock(){
tmux "$#"
inmode=$(tmux display -p '#{pane_in_mode}')
[ "$inmode" = 1 ] && tmux "$#"
exit 0
}
case $1 in
-noclock)shift
: ${1?select-pane cmd and args}
noclock "$#" ;;
esac
then setup the following bindings in your ~/.tmux.conf:
bind-key -T prefix o run-shell 'mytmux -noclock select-pane -t :.+'
bind-key -T prefix l run-shell 'mytmux -noclock select-pane -l'
bind-key -r -T prefix Up run-shell 'mytmux -noclock select-pane -U'
bind-key -r -T prefix Down run-shell 'mytmux -noclock select-pane -D'
bind-key -r -T prefix Left run-shell 'mytmux -noclock select-pane -L'
bind-key -r -T prefix Right run-shell 'mytmux -noclock select-pane -R'
You will need to extend this if you want to handle multiple sessions, for example. I put select-pane -l in the bindings, but this is not useful, as if it repeats it will just go back to where you started from.

TMUX using HJKL to navigate panes

Standard TMUX is set to use ctrl-b + [up, down, left, right] when navigating between panes.
I would like to make it so that I can use ctrl-b (or the prefix of my choice) + [h,j,k,l].
I thought I had done this with the following vi key in my ~/.tmux.conf settings:
set -g status-keys vi
setw -g mode-keys vi
Yet this didn't seem to change anything (at least not what I was looking for). How can I get this to work. And yes my .tmux.conf is working properly. I can provide more info if needed.
Update:
Here is my full .tmux.conf after trying to get it to work:
set -g status-keys vi
setw -g mode-keys vi
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# smart pane switching with awareness of vim splits
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
Alternatively, I have tried using this w/ vim-tmux-navigator Vim plugin:
# smart pane switching with awareness of vim splits
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-j) || tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-k) || tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-l) || tmux select-pane -R"
bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys 'C-\\') || tmux select-pane -l"
source
Which also doesn't work either. I am a bit stumped.
You can do this as follows:
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
Note that mode-keys refers to using vi-like navigation within a buffer and status-keys refers to using vi-like editing within the status bar, but neither refers to switching between panes.
Did you remember to source your ~/.tmux.conf file? After making any changes in this file you need to enter the following command to see any of the changes take place
tmux source-file ~/.tmux.conf
The Micah Smith's answer seems to work. But it doesn't have quite the same behaviour that we have with the arrow keys. With the arrows, if you are fast enough, you can hit prefix + arrow key multiple times and you are able to navigate multiple panes, using the same prefix hiy. Main difference:
With the arrows:
// to move 3 panes to the right
prefix + -> -> ->
With this hack:
// to move 3 panes to the right
(prefix + l) 3x
Still, to make this change, you need to update your ~/.tmux.conf file and then restart tmux sessions.
To be sure you don't have any tmux sessions you can run
$ tmux list-sessions
If you have some sessions running, run $ killall tmux and you should be good to go.
This☝️ was tested in a macbook, it should be the same for linux.
If you are looking for a modal mode for tmux (e.g. like in Vim text editor), there is a plugin tmux-modal that can be used to execute complex commands with just a few keystrokes. For example:
w h to select the left pane
w j to select the pane below
w k to select the pane above
w l to select the right pane
There is also a sticky mode (w w) that enables h, j, k, l to select the panes as you want to, without a prefix key. Please see the repository for more information.

Tmux copy mode: how to create your own command?

I love Tmux and its copy mode with Vi commands, but I'm really annoyed by the fact that this mode is very far from being as efficient as real Vim.
For example, there is no keybinding to just copy a word (yw), I must always "go to the beginning of a word" "begin selection", "go to the end of the word" then "finish selection". A lot of operations when I just need to do yw in vim.
I searched a way to create my own "yw" command in Tmux copy mode. Chaining all the operations needed is a good idea, but a simple bind with commands separated by ; just doesn't work (similar thing works in non-copy mode). Is there something I miss? Or is the copy mode of Tmux just limited and not as scriptable as I need it to be?
I have this in my tmux conf:
# vi-style controls in copy mode
set-option -g status-keys vi
set-window-option -g mode-keys vi
# v and y like vi in copy-mode
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
Now after going copy-mode i can easily select words by:
vw
And copy with
y
In tmux you have to select something to copy. There is nothing like copying in normal mode as you know from usual vi/vim commands. Unfortunately you can only use one key (like v or y) for every tmux argument.
You can find more about tmux's vi movement commands here: https://superuser.com/a/197272/57890
This appears to be a bug in the bind-key command when called with the -t option. I have filed a bug report at https://sourceforge.net/tracker/?func=detail&aid=3533562&group_id=200378&atid=973262.
On upstream (2.4+) tmux version this got changed, in order to create a bindings for begin selection you need to use -T and send-keys with -X.
More info in tmux changelog.
Here my bindings for vi copy mode as an example:
# Bind `v` to trigger selection
bind-key -T copy-mode-vi v send-keys -X begin-selection
# Bind `y` to yank current selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# Rebind `mouse click + drag button release` to not jump away from context
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-selection
If you are using emacs copy mode, replacing copy-mode-vi with copy-mode should be enough.
There's a patch for tmux allowing to create procedures and bind any number of actions for 'mode' keystrokes: http://ershov.github.io/tmux/

tmux -- disable echo of last run command?

I started using tmux recently and things are good except for the fact that tmux prints out my last run command before output, e.g.:
~ $ pwd
pwd/Users/me
You can see that it put "pwd" before the directory there, which is annoying.
My shell(zsh) doesn't do this when I run commands outside of tmux.
show-environment -g doesn't reveal any weird options being passed to zsh or anything: SHELL=/bin/zsh
I read through the manpage and Googled around but I can't find anything.
Thanks for any help!
Figured it out -- needed to change my ~/.tmux.conf to have a different TERM(xterm instead of screen-256color):
# act like vim
setw -g mode-keys vi
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind-key -r C-h select-window -t :-
bind-key -r C-l select-window -t :+
# act like GNU screen
unbind C-b
set -g prefix C-a
# look good
#set -g default-terminal "screen-256color"
set -g default-terminal "xterm"
set -g status "off"

Resources