Tmux bind-key not working for split-window - tmux

I have following configuration in my tmux config file:
unbind C-b
set -g prefix C-s
bind-key - split-window -v
bind-key \ split-window -h
When I am in tmux, if I try
Ctrl+s : split-window -v
it works. If I try
Ctrl+s -
it does not work. It says No Buffer, and tmux does not do anything.
Any help on how can I debug this behaviour?

The - key is already bound to the delete-buffer command. You can check this by executing tmux list-keys. You must unbind it and then apply your binding:
unbind -
bind-key - split-window -v

Related

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.

How to exit tmux's prefix mode after switching pane

I switch panes often and when I press ctrl-a-right I move the the right. Then if I start typing "vim", the 'v' causes a ctrl-a-v (split vertically) to be issued, but I don't want this. How do I make tmux realize this?
In your .tmux.conf, add
bind Up select-pane -U
bind Down select-pane -D
bind Left select-pane -L
bind Right select-pane -R
or if you're using vim directions,
bind k select-pane -U
bind j select-pane -D
bind h select-pane -L
bind l select-pane -R
as detailed in this post: https://superuser.com/a/1273727
My solution was to remove -r in .tmux.conf on the split-window options. Removing -r causes repeats to be disabled.
from
bind -r v split-window -h -c "#{pane_current_path}"
to
bind v split-window -h -c "#{pane_current_path}"

Iterm2 with tmux intergration not using keybindings from .tmux.conf

I have been using tmux in iTerm2 for a while now but I have not been using the tmux integration that now comes with iTerm2.
I started looking at using the tmux intergration as it allows you to use the shell intergration inside tmux.
The problem i am encountering is that after i do tmux -CC none of the key bindings inside my .tmux.conf work... I cant even get any of the defaults to work. I would really like to use tmux integration but cant unless i can get the key bindings in my .tmux.conf to work.
Some of the bindings that are not working are;
Rebound the prefix key to C-s
bind-key \ split-window -v -c '#{pane_current_path}'
bind-key - split-window -h -c '#{pane_current_path}'
These are just a couple of examples but basically nothing seems to be working...
My .tmux.conf is
# improve colors
set-option -g default-terminal "screen-256color"
# set base Prefix key to ctrl-s
unbind C-b
set -g prefix C-s
bind-key -r C-s send-prefix
# reload the source config
bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded"
# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
set-option -g status-keys "emacs"
#change colour of status bar
set-option -g status-bg '#666666'
set-option -g status-fg '#aaaaaa'
# set window split
bind-key - split-window -v -c '#{pane_current_path}'
bind-key \ split-window -h -c '#{pane_current_path}'
# set text in status bar
set -g status-interval 1
set -g status-justify centre # center align window list
set -g status-left-length 50
set -g status-right-length 140
set -g status-left '#[fg=green]#H #[fg=black]• #[fg=green,bright]#(uname -r | cut -c 1-6)#[default]'
set -g status-right '#[fg=green,bg=default,bright]#(tmux-mem-cpu-load -i 1) #[fg=red,dim,bg=default]#(battery -at) #[fg=white,bg=default]%a%l:%M:%S %p#[default] #[fg=blue]%Y-%m-%d'
# Fine adjustment (1 or 2 cursor cells per bump)
bind -n S-Left resize-pane -L 2
bind -n S-Right resize-pane -R 2
bind -n S-Down resize-pane -D 1
bind -n S-Up resize-pane -U 1
# new window opens in same directory
bind c new-window -c "#{pane_current_path}"
# set window numbering to start from 1
set -g base-index 1
set -g renumber-windows on
# allow to take a pane and put it into new window
bind-key b break-pane -d
# allows ctrl-j to open tree of tmux sessions
bind-key C-j choose-tree
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"
# History
set -g history-limit 10000
# Mouse mode
set-option -g mouse on
# Rename Tab
set-option -g set-titles on
# Terminal notifier doesnt work with tmux
set -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL -l"
set-option -sg escape-time 10
Copying my answer at https://stackoverflow.com/a/47171067/4200039 over:
Someone reported a similar bug at iTerm2 Tmux Integration Default Keybindings not working in 2015. The response from the developer George Nachman suggests that keybindings are not supported:
One of the main goals of the tmux integration is that you use iTerm2's
keystrokes, not tmux's. If there's a specific workflow that you can't
accomplish natively, let me know--there is a lot of power in the
ability to remap keys in iTerm2.
This should be flagged as a duplicate of iTerm 2 not honoring key bindings declared in .tmux.conf but I can't flag it until it has an accepted or upvoted answer.

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