TMUX using HJKL to navigate panes - tmux

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.

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

Tmux commands doesn't work

When I type ctrl+b(keep them pressing) button and then hit c button nothing happens. No ctrl+b command combinations work. Only these two commands work:
tmux new-session -s {session-name}
tmux kill-session -t {session-name}
Also I am not able to create new nested session. How to create new session. Are there modes for using tmux like vim. For eg. hit esc for normal/command mode, hit i for insert mode and v for visual mode. I am asking this question because I doubt if I need to press some key before giving key commands like ctrl+b+n. They just get written as normal text in terminal.
Characters are inputted in terminal. See the screenshot below. I am using all my tmux, zsh, vim configurations from here
Please check my tmux.config file
set -g default-command "reattach-to-user-namespace -l zsh"
# tmux display things in 256 colors
set -g default-terminal "screen-256color"
set -g status-utf8 on
# automatically renumber tmux windows
set -g renumber-windows on
# unbind default prefix and set it to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# for nested tmux sessions
bind-key a send-prefix
# Activity Monitoring
setw -g monitor-activity off
set -g visual-activity off
# Rather than constraining window size to the maximum size of any client
# connected to the *session*, constrain window size to the maximum size of any
# client connected to *that window*. Much more reasonable.
setw -g aggressive-resize on
# make delay shorter
set -sg escape-time 0
# make window/pane index start with 1
set -g base-index 1
setw -g pane-base-index 1
######################
#### Key Bindings ####
######################
# reload config file
bind r source-file ~/.tmux.conf \; display "Config Reloaded!"
# split window and fix path for tmux 1.9
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# synchronize all panes in a window
bind y setw synchronize-panes
# pane movement shortcuts
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+
# Resize pane shortcuts
bind -r H resize-pane -L 10
bind -r J resize-pane -D 10
bind -r K resize-pane -U 10
bind -r L resize-pane -R 10
# enable mouse support for switching panes/windows
# NOTE: This breaks selecting/copying text on OSX
# To select text as expected, hold Option to disable it (iTerm2)
setw -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on
# set vi mode for copy mode
setw -g mode-keys vi
# more settings to make copy-mode more vim-like
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection
# Buffers to/from Mac clipboard, yay tmux book from pragprog
bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy"
bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste); tmux paste-buffer"
You have changed the default escape-sequence in your configuration: from Ctrl-B (tmux default) to Ctrl-A (just like the similar terminal multiplexer screen).
The relevant configuration lines are in the third paragraph:
# unbind default prefix and set it to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
If you want to use tmux default one just comment out (with a leading #) or remove the lines above in your tmux.conf.
I have also recently came across the same problem in Linux Mint, but this thread fixed it
For the vertical splitting: Instead of (Ctrl+B) + % => (Ctrl+B) + (Ctrl+%).
For the horizontal splitting: Instead of (Ctrl+B) + " => (Ctrl+B) + (Ctrl+")
Also, do not forget to release (Ctrl+B) before pressing the (Ctrl+%) or (Ctrl+").

How to send a command to all panes in tmux?

I like to call :clear-history on panes with a huge scrollback. However, I want to script a way to send this command to all the panes in the various windows.
I know how to send a command to all the windows, courtesy of this question, but how do I send a command to all the panes of which window as well?
send-keys and synchronize-panes from the tmux manpage come to mind, but I'm not sure how to marry them together. But maybe there is a simpler way to do this.
Extra Observations:
Thinking about this a little bit, tmux list-panes -a seems to list all the panes in the current session. Pretty useful to start off with. Where do I go from here?
Have you tried following in tmux window with multiple panes
Ctrl-B :
setw synchronize-panes on
clear history
A bit late to the party but I didn't want to set and unset synchronize-panes just to send one command so I created a wrapper function around tmux and added a custom function called send-keys-all-panes.
_tmux_send_keys_all_panes_ () {
for _pane in $(tmux list-panes -F '#P'); do
tmux send-keys -t ${_pane} "$#"
done
}
I also create a wrapper around the tmux command to simplify calling this function (for convenience). The wrapper and the above code are all here.
This allows me to run tmux send-keys-all-panes <command> or tmux skap <command to send <command> to all panes.
Note that tmux is aliased to my wrapper function tmux_pp.
Update June 2019
Quick illustration on how to configure your own binding for synchronize panes.
Added the following into my tmux.conf (the comments certainly apply to my overall configuration):
# synchronize all panes in a window
# don't use control S, too easily confused
# with navigation key sequences in tmux (show sessions)
unbind C-S
bind C-Y set-window-option synchronize-panes
Now, I can toggle the ability to synchronize commands across multiple panes with <C-a><C-y>.
(Yes, I remapped the bind key to Ctrl a).
my tmux version is 1.9a, and this works for me, one key is enough for both on and off
bind-key X set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized,on,off}"
None of the above answers worked for me (tmux v2.3), but this did, from the bash command line:
for _pane in $(tmux list-panes -a -F '#{pane_id}'); do \
tmux clear-history -t ${_pane} ; done
A more generalized script, for tmux commands other than 'clear-history' would just replace that element with a parameter, eg. $1. Do be careful if you intend to write a script to handle a series of tmux commands, as "-t ${_pane}" will need to be applied to each.
Note that the -a parameter to tmux list-panes is required to cover all panes in all windows in all sessions. Without that, only panes in your current tmux window will be affected. If you have more than one tmux session open and only want to apply the command to panes within the current session, replace -a with -s (It's all in the tmux man page).
I haven't the mod points to comment directly on each of the above answers, so here's why they weren't working for me:
The problem that I had with #shailesh-garg 's answer was that the sync affected only commands issued within the panes, not tmux commands issued using Ctrl-B : which are outside the panes.
The three problems that I had with #kshenoy 's answer were that:
it sends keystrokes to within a pane, not to the tmux operation
of that pane, so for instance, if one had a bash shell running in
the pane and one used the script to send "clear-history", those
would be the keystrokes that would appear in the bash command-line.
A work-around would be to send "tmux clear-history" or to pre-pend
"tmux " to "$#", but I haven't edited the answer because of my other
problems with the answer;
I couldn't figure out how to send a
new-line character without literally breaking the line;
Even when I did that, sending "tmux clear-history" had no effect.
If you want to send your command to every pane in every window in every session, add this to your .bashrc:
send_command_to_every_pane() {
for session in `tmux list-sessions -F '#S'`; do
for window in `tmux list-windows -t $session -F '#P' | sort`; do
for pane in `tmux list-panes -t $session:$window -F '#P' | sort`; do
tmux send-keys -t "$session:$window.$pane" "$*" C-m
done
done
done
}
You can then use it like this:
send_command_to_every_pane source ~/.bash_profile
Change "$*" to "$#" if you want that behavior, but in my experience this is what you want.
tmux send-keys -t <session id> <command> C-m
Replace the "session id" and "command" accordingly.
This is my utility function to do it, only executing the command when there there is nothing running in the pane.
#!/bin/bash
_send_bash_command_to_session() {
if [[ $# -eq 0 || "$1" = "--help" ]] ; then
echo 'Usage: _send_bash_command_to_session $session_name what ever command you want: '
return
fi
input_session="$1"
input_command="${#:2}"
for _pane in $(tmux list-panes -s -t ${input_session} -F '#{window_index}.#{pane_index}'); do
# only apply the command in bash or zsh panes.
_current_command=$(tmux display-message -p -t ${input_session}:${_pane} '#{pane_current_command}')
if [ ${_current_command} = zsh ] || [ ${_current_command} = bash ] ; then
tmux send-keys -t ${_pane} "${input_command}" Enter
fi
done
}
tmux_set_venv() {
_current_session=$(tmux display-message -p '#{session_name}')
_send_bash_command_to_session ${_current_session} workon $1
}
Example targeting a session called dev, enabling a python virtualenv in all panes that are in bash or zsh, avoiding executing the command in panes with vim or any other executable:
_send_bash_command_to_session dev workon myvirtualenv
or easier to remember: to do it in the current session:
tmux_set_venv myvirtualenv
Find my configuration file with this function.
You can combine synchronize-panes and send-keys in a single shortcut to send commands to all the panes:
Predefined tmux command clear-history:
bind-key C set-option -w synchronize-panes on\; clear-history \; set-option -w synchronize-panes off
Prompt an arbitrary tmux command:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; %% ; set-option -w -u synchronize-panes"
Prompt an arbitrary shell command:
bind-key p command-prompt -p "Panes command: " "set-option -w synchronize-panes on; send-keys %%\\n ; set-option -w -u synchronize-panes"
By default, byobu uses tmux as backend. It's a wrapper that make things much easier:
Shift+F9:
Ctrl+F9:
Shift+F1
Admittedly only semi-related, I found I could make the status background red when I toggle synchronize-panes so it's obvious when I switch back to a window with an unknown synchronize-panes state:
bind-key C-x setw synchronize-panes on \; set-window-option status-bg red \; display-message "pane sync on"
bind-key M-x setw synchronize-panes off \; set-window-option status-bg default \; display-message "pane sync off"

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