Is there a way to alias tmux commands? - tmux

tmux has a command mode that can be accessed via C-b : and I'm wondering if there is a way to alias commands in my .tmux.conf file like split-window to something I use more often like vsp in vim.
I know I can bind keyboard shortcuts with bind but can I alias commands as well?

tmux 2.4 adds the command-alias array option which does this, so for example you can do
:set -s command-alias[10] vsp='split-window -h'
And then you can use :vsp in the command-prompt just like you'd expect.

This doesn't appear to be possible as of tmux 2.0.
One thing you can do, however, is send commands to the enclosing tmux session from the shell. This means that you can, for example, create a bash alias that can split windows:
alias vsp="tmux split-window -h"
You can then run vsp from your bash prompt to split the tmux window vertically. Depending on your specific use case, this might help.
It's also worth noting that, if minimising typing is the goal, tmux commands can be shortened to their shortest unambiguous prefix: sp does the same thing as split-window.

There's a mod allowing not only alias but also create new commands in tmux: http://ershov.github.io/tmux/
For example:
proc vsp {args} { split-window -h {*}$args }
No external shell involved, no new processes spawned.

You can use bind. Here is an example alias for turning mouse-mode on and off:
bind m \
set -g mode-mouse on \;\
set -g mouse-resize-pane on \;\
set -g mouse-select-pane on \;\
set -g mouse-select-window on \;\
display 'Mouse mode ON'
bind M \
set -g mode-mouse off \;\
set -g mouse-resize-pane off \;\
set -g mouse-select-pane off \;\
set -g mouse-select-window off \;\
display 'Mouse mode OFF'
Now you can easily use Ctrl+B m to turn it on, and Ctrl+B M to turn it off (assuming Ctrl+B is your prefix)

Yep. Using bind-key in you tmux.conf. For example to split tmux windows use:
bind-key v split-window -v #C-b v to split vertically
bind-key h split-window -h #C-b h horizontal
If you don't want to use prefix (C-b) just add -n param:
bind-key -n C-right next # C - → to move to next window.

Related

tmux - Force repeat prefix key for every command

Right now my tmux keybindings are set such that prefix + h/j/k/l (i.e vim-style bindings) are used to move focus of the panes left/down/up/right. However, sometimes when I switch to a pane running vim and I immediately start navigating with h/j/k/l, I get stuck in "tmux mode" where it will continue switching panes instead of navigating in vim. I have similar issues when switching to a normal terminal pane and listing files (i.e by using "l"). To avoid this, I would like to force tmux to require the prefix key for every pane switch I do.
Is there a way to do this?
Edit:
In case its needed, here is my .tmux.conf
# Bind CTRL+a to the prefix button
set -g prefix C-a
unbind C-b
bind C-a send-prefix
bind a send-prefix
# Remove the delay of escape key
set -s escape-time 0
# Bind PREFIX + r to reload the .conf file
unbind r
bind r source-file ~/.tmux.conf
# Quick pane cycling
unbind ^A
bind ^A select-pane -t :.+
set -g base-index 1
setw -g pane-base-index 1
set-option -g default-shell "/bin/bash"
# List of tmux plugins
set -g #plugin 'tmux-plugins/tmux-resurrect'
# Plugin manager
run '~/.tmux/plugins/tpm/tpm'
# Enable mouse support on tmux
set -g mouse on
# Rebind the pane switching to vim-like shortcuts
bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R
unbind Up
unbind Down
unbind Left
unbind Right
unbind C-Up
unbind C-Down
unbind C-Left
unbind C-Right
# Set the tmux colors to default
set -g default-terminal screen-256color
This is being caused by your use of -r when creating the key bindings for h/j/k/l.
From the entry for bind-key in the tmux man page:
The -r flag indicates this key may repeat, see the repeat-time option.
And about the repeat-time option:
Allow multiple commands to be entered without pressing the prefix-key again in the specified time milliseconds (the default is 500). Whether a key repeats may be set when it is bound using the -r flag to bind-key. Repeat is enabled for the default keys bound to the resize-pane command.
Just drop the -r from those four commands to have it require the prefix key every time.
I disagree with #filbranden's assertion that "Requiring the prefix for every pane switch is the normal behavior". This demonstrably false: on a fresh install, you can create a window with two panes, type the prefix (C-b), and then jump between the panes as many times as you like by pressing arrow keys.
This answer on StackExchange seems correct. The feature that results in the prefix not being required is controlled by setting the option repeat-time. It is set to 500 by default (which is why the default behaviour is that the prefix is not required).
To disable this behaviour, simply add this line to your config:
set-option -g repeat-time 0

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 automatically rename tmux windows to the current directory

I would like to have tmux to automatically rename the window with the current working directory (cwd). As it is by default, it names the tab/window as the name of the current process, such as zsh or vim.
When I open a new window in tmux, the name is reattach-to-use-namespace and then it immediately switches to zsh.
I'm on OS X 10.10.2, I use zshell, and I have tmux 1.9a.
To be clear, I don't want the entire path in the name of the window, just the current directory, so for example, I want projectName, not /Users/username/Development/projectName.
If you want to see my current tmux.conf, here it is.
With tmux 2.3+, the b: format modifier shows the "basename" (or "tail") of a path.
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'
The FORMATS section of man tmux describes other modifiers, such as #{d:} and even #{s/foo/bar/:}.
With tmux 2.2 or older, the basename shell command can be used instead.
set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#(basename "#{pane_current_path}")'
Expanding on what Josef wrote, you can put the basename of the directory in the status using a shell snippet:
# be sure to see note* below
set -g window-status-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
set -g window-status-current-format '#I:#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F'
# status bar updates every 15s by default**, change to 1s here
# (this step is optional - a lower latency might have negative battery/cpu usage impacts)
set -g status-interval 1
*Note that what would be ${pwd##*/} is escaped to ${pwd####*/} since # has special meaning in the format string.
**See here for an example default tmux config.
Show the top N components
Showing just the basename generates too much ambiguity, but full paths are too much clutter, so I settled for:
the/last/path
instead of:
/a/very/long/the/last/path
or just:
path
.tmux.conf
set-window-option -g window-status-current-format '#[fg=white,bold]** #{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]**|'
set-window-option -g window-status-format '#[fg=white,bold]#{window_index} #[fg=green]#{pane_current_command} #[fg=blue]#(echo "#{pane_current_path}" | rev | cut -d'/' -f-3 | rev) #[fg=white]|'
Trick taken from: Remove part of path on Unix
If that still does not solve ambiguity, I go for:
bind-key -r w choose-window -F '#{window_index} | #{pane_current_command} | #{host} | #{pane_current_path}'
Tested on Tmux 2.1, Ubuntu 16.04.
To get the best of both worlds - window name is path when you're at a shell prompt, but name of executable when you're running something, try this:
set-option -g status-interval 1
set-option -g automatic-rename on
set-option -g automatic-rename-format "#{?#{==:#{pane_current_command},bash},#{b:pane_current_path},#{pane_current_command}}"
Replace "bash" with whatever shell you're using.
Adding this config to your ~/.tmux.conf file should work:
set-option -g window-status-current-format '#I:#{pane_current_path}#F'
set-option -g window-status-format '#I:#{pane_current_path}#F'
set-option -g status-interval 1
It depends however on your Tmux version. I wasn't able to make it work on 1.9a3 (in Cygwin) - but with Tmux 1.8 on Ubuntu (in Vagrant) it worked fine.
I use the following in ~/.tmux.conf to achieve this (working on OSX, zsh, tmux-2.3):
set -g automatic-rename-format '#{pane_current_path}'
set -g status-interval 5
You can set status-interval to 1 to make it respond faster to changing directories.
According to the changelog (https://raw.githubusercontent.com/tmux/tmux/master/CHANGES) this should work in tmux 1.9 and up.
Using ssh into a CentOS machine with tmux 2.3 the window name doesn't change until I press return in the new panel, not sure why that is happening.
Do something like this in a tmux session for zsh shell:
setopt PROMPT_SUBST
export PS1=$'\ek$(basename $(pwd))\e\\> '
If someone uses bash shell:
export PS1="\033k\$(basename \$(pwd))\033\\> "
You can add these commands in the shell initialization file on the condition the $TERM env variable is set to the value "screen"
I am using zsh hook for that
Add following in ~/.zshrc
precmd () {
if [ -n "$TMUX" ]; then
tmux set-window-option -q window-status-format "#[fg=cyan bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
tmux set-window-option -q window-status-current-format "#[fg=cyan, bg=cyan] | #[fg=white, bg=cyan] #I | ${PWD##/*/} #[fg=cyan, bg=cyan] | "
fi
}
This doesn't strictly answer your question--it doesn't automatically rename an existing tmux session to the current working directory.
Rather, when creating a new session, it names that session after the current working directory.
Here's what I did:
to
~/.aliases
add
alias tm='tmux new -s `basename $PWD`'
Open a new terminal window and type:
tm
This now creates a new tmux session which is named after the current working directory.
Note: This relies on basename which does not exist in Windows.
I am sure that you want use this:
set -g status-left '#{pane_current_path} '
To change what you see in the window list you can specify a format when you define the key-binding for the chose-window function like this:
bind-key '"' choose-window -F "#{session_name} | #{window_name} - #{b:pane_current_path} (#{pane_current_command})"

inconsistent tmux new-window behavior

I just installed tmux on my new MacBook Pro, and am running into the following issue.
When I create a new window via the command tmux new-window I get a new window with the same working directory as the session I'm creating from. This is the behavior I expect/desire.
When I create a new window using C-b C-c, I get a new window in my home directory.
Why are my shortcut keys giving me different behavior/how do I make them use the same directory?
Since Tmux 1.9 you should use the -c option in order to open a new working directory of the current pane.
tmux new-window -c '#{pane_current_path}'
Add this to your tmux.conf:
bind c new-window -c '#{pane_current_path}'
Note: It is also possible that you will have to exit all running instances of the TMUX server before any changes to the .tmux.conf file will be loaded.
If you also want split windows in the current directory, you can add this to tmux.conf:
bind "\"" split-window -c '#{pane_current_path}'
bind "\%" split-window -h -c '#{pane_current_path}'
The quotes should not be in tmux.conf; at least not for tmux 2.1:
#
# open split panes in the current directory
#
bind-key \" split-window -c '#{pane_current_path}'
bind-key \% split-window -h -c '#{pane_current_path}'

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"

Resources