tmux - Force repeat prefix key for every command - tmux

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

Related

How to disable mouse copy while keeping mouse scroll in tmux?

I'm running tmux via ssh on a linux (remote) machine from an iTerm2 on macOS.
I've configured .tmux.conf in order to get mouse wheel scrolling inside of tmux via the following:
set -g mouse on
This enables mouse wheel scrolling, copy to clipboard on mouse drag and others.
Can I keep mouse wheel scrolling but disable copy on mouse drag to clipboard at the same time?
Here is the best behaviour I was able to get from tmux so far
set -g mouse on
unbind-key MouseDown2Pane
unbind-key MouseDragEnd1Pane
bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o -sel primary)\"; tmux paste-buffer"
bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -sel primary -i"
set -g set-clipboard external
If you run tmux list-keys you should see lines like
bind-key -T root MouseDrag1Pane ...
depending on your version of tmux. You can add lines to your config file to unbind the ones you don't want, eg:
unbind-key -T root MouseDrag1Pane
When tmux has the mouse mode on (set -g mouse on), then in iTerm on macOS you can press the option key (= alt key) to temporary disable sending mouse events to the terminal application (and thus you can mark lines in the terminal as you are used to).
This also allows you to scroll back in your buffer to before you started tmux.

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+").

Is there a way to alias tmux commands?

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.

"tmux set -g mouse-mode on" not scrolling

To allow scrolling a tmux pane with a mouse, I put the following in my ~/.tmux.conf file:
set -g mouse-mode on
However, nothing changes. When I scroll, it still scrolls outside of tmux. Why is this?
So this option has been renamed in version 2.1 (18 October 2015)
From the changelog:
Mouse-mode has been rewritten. There's now no longer options for:
- mouse-resize-pane
- mouse-select-pane
- mouse-select-window
- mode-mouse
Instead there is just one option: 'mouse' which turns on mouse support
So this is what I'm using now in my .tmux.conf file
set -g mouse on
As #Graham42 noted, mouse option has changed in version 2.1. Scrolling now requires for you to enter copy mode first. To enable scrolling almost identical to how it was before 2.1 add following to your .tmux.conf.
set-option -g mouse on
# make scrolling with wheels work
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
This will enable scrolling on hover over a pane and you will be able to scroll that pane line by line.
Source: https://groups.google.com/d/msg/tmux-users/TRwPgEOVqho/Ck_oth_SDgAJ
Just a quick heads-up to anyone else who is losing their mind right now:
https://github.com/tmux/tmux/blob/310f0a960ca64fa3809545badc629c0c166c6cd2/CHANGES#L12
So that's just
:setw -g mouse
this should work:
setw -g mode-mouse on
then resource then config file
tmux source-file ~/.tmux.conf
or kill the server
Paste here in ~/.tmux.conf
set -g mouse on
and run on terminal
tmux source-file ~/.tmux.conf
As #Graham42 said, from version 2.1 mouse options has been renamed but you can use the mouse with any version of tmux adding this to your ~/.tmux.conf:
Bash shells:
is_pre_2_1="[[ $(tmux -V | cut -d' ' -f2) < 2.1 ]] && echo true || echo false"
if-shell "$is_pre_2_1" "setw -g mode-mouse on; set -g mouse-resize-pane on;\
set -g mouse-select-pane on; set -g mouse-select-window on" "set -g mouse on"
Sh (Bourne shell) shells:
is_pre_2_1="tmux -V | cut -d' ' -f2 | awk '{print ($0 < 2.1) ? "true" : "false"}'"
if-shell "$is_pre_2_1" "setw -g mode-mouse on; set -g mouse-resize-pane on;\
set -g mouse-select-pane on; set -g mouse-select-window on" "set -g mouse on"
Hope this helps
You can still using the devil logic of setting options depending on your current Tmux version: see my previous answer.
But since Tmux v1.7, set-option adds "-q" to silence errors and not print out anything (see changelog).
I recommend to use this feature, it's more readable and easily expandable.
Add this to your ~/.tmux.conf:
# from v2.1
set -gq mouse on
# before v2.1
set -gq mode-mouse on
set -gq mouse-resize-pane on
set -gq mouse-select-pane on
set -gq mouse-select-window on
Restar tmux or source-file your new .tmux.conf
Side note: I'm open to remove my old answer if people prefer this one
This line:
set -g mouse-mode on
in your ~/.tmux.conf file won't work.
For one thing, the proper setting is not mouse-mode, but mode-mouse.
Second, it only works on older versions of tmux (pre-version 2; you can run tmux -V to see what version you have, but you can also run man tmux to see if mode-mouse is a supported option).
And third, technically man tmux says to use the mode-mouse option with setw (set-window-option) and not with set (set-option), although I've seen it work with set (as well as setw).
These lines should work:
If your version of tmux is recent enough (that is, if tmux -V shows version 2 or newer), you can put this line in your ~/.tmux.conf file:
set-option -g mouse on
But if you use an older version of tmux, put these lines in your ~/.tmux.conf file:
set-window-option -g mode-mouse on
set-option -g mouse-resize-pane on
set-option -g mouse-select-pane on
set-option -g mouse-select-window on
Again, if you're not absolutely sure which line(s) to put in your ~/.tmux.conf file, run man tmux and search for mouse-mode, mouse-resize-pane, mouse-select-pane, and/or mouse-select-window. If you find them, then use those options (the ones for the older version of tmux).
Putting these lines in will allow you to use the mouse scroll wheel, it will allow you click on various panes to activate them, and it will allow you to resize panes just by clicking-and-dragging on their splitter separator.
You can also copy-and-paste using your mouse. However, you may notice that copying-and-pasting with your mouse may not work like it normally does on your console. To work around this, I've noticed that holding down the SHIFT key while using your mouse's copy/paste abilities makes them work normally again. (On MacOS, fn seems to work for me better than SHIFT.) Or you can learn tmux's approach to copy-and-pasting (which I do recommend).
A note on using tmux's native ability to copy/select/paste:
Personally, I feel that the vi keys make more sense to me than the default emacs keys (for example, it feels more natural to exit selection mode with vi mode's ENTER than with emacs mode's CTRL+w or ALT+w), so I have this in my ~/.tmux.conf file:
# For vi-like keys in copy/paste/selection mode:
set-window-option -g mode-keys vi
However, I've noticed that on older versions on tmux, this line won't work, unless I've also included the line:
set-window-option -g mode-mouse on
Good to know: Occasionally, when you're using the mouse's scroll wheel, you'll discover that your pane/screen appears to be frozen or locked up (it won't respond to keyboard input), and it's not clear what to do to unlock it.
It's not locked up. You're likely in tmux's selection mode. To get out of it, try one of these:
Hit ENTER. (Will likely work in vi mode.)
Hit ALT+w. (Will likely work in emacs mode.)
Hit CTRL+w. (Will likely work in emacs mode.)
Hit META+w. (Will likely work in emacs mode.)
Hit Esc. (Will likely work in emacs mode.)
Hit q. (Will likely work in vi mode, and may work in emacs mode as well.)
Ultimately, you can try hitting:
CTRL+w     ENTER
or:
Esc     q
Chances are, that will put you back in the normal mode where your keyboard is responsive again.
Using the mouse with vim:
I've discovered that putting this line in my ~/.vimrc file:
:set mouse=a
seems to work well with tmux, better than this line:
:set mouse=r
I can't really explain the difference; if you're a vim user, try them out and see which setting works best for you.
If you need to test out these tmux options, you may find yourself editing your ~/.tmux.conf file, exiting tmux, and then restarting tmux. This can become a tedious process, so if you want some work-arounds, here are some options:
Option1: After editing (and saving) your ~/.tmux.conf file, run this command (while in tmux):
CTRL+B :source-file ~/.tmux.conf ENTER
This will immediately apply the new settings in your ~/.tmux.conf file into your current tmux session, without the need to restart tmux.
Option 2: If you're in a tmux session, you can turn on/off options temporarily just for that session by running commands like these at the Unix shell prompt:
tmux set-option -g mouse on
tmux set-window-option -g mode-keys vi
Option 3: You can also temporarily turn on/off options in a tmux session with:
CTRL+B :set-option -g mouse on ENTER
CTRL+B :set-window-option -g mode-keys vi ENTER
Good luck, and have fun using tmux !
Try this. It works on my computer.
set -g mouse on
Ctrl + B and then set mouse. -g is not needed anymore.

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/

Resources