Configure mouse in tmux on OSX - macos

It seems that mouse support changed in one of the recent versions of tmux and every article I am finding on the subject uses outdated settings. All I want to be able to do is use my mouse to scroll when in scroll mode and use it to copy/paste with the left button like it usually does in a terminal. Unfortunately, if I set set -g mouse on the copy/paste doesn't work and if I set it to off, scroll doesn't work. I am on OSX 10.12 Sierra if that makes a difference and I am using the default Terminal app.

In my case fn button + mouse works fine for text selection.
After selection need to press cmd+c for copying and paste with cmd+v.

One quick way to get copy-paste functionality back using the mouse is to disable "Mouse Reporting"; see the menu item View > Allow Mouse Reporting in Terminal.app. The default keyboard shortcut is ⌘ Command+r, so a possible workflow is:
Hit ⌘ Command+r to toggle off mouse reporting.
Highlight text with mouse as usual, copy with ⌘ Command+c, or paste with middle-click.
Hit ⌘ Command+r to restore tmux's mouse functionality.
Alternatively, hold down the fn key to temporarily obtain the same functionality, as noted in the answer from #yaroslavpalamar.
Multi-pane solution
The comments pointed to an issue with this solution when using multiple panes. This requires a fix from within tmux, in order to simultaneously zoom the pane and allow copy/paste with the mouse. To do that, we can augment the solution pointed to by #MadWombat by adding the line below to the config.
In your ~/.tmux.conf file (or /etc/tmux.conf):
bind-key m set mouse \; resize-pane -Z
Then prefix+m is a toggle: hit it once to zoom the pane and allow copy/paste with the mouse, hit it again regain the former functionality. Note this works fine with a single pane as well -- the zoom part of the command has no effect in that case.

tmux changed mouse behaviour moving from v2.0 to v2.1.
You can find more information reading tmux's github repository issue 145 description.
I don't use this anymore, but this fixed mouse handling for me after moving to v2.1:
# Handling mouse
set -g mouse on
# Fix Mouse Scrolling: https://github.com/tmux/tmux/issues/145
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
With this settings you can select using the mouse and the Option (alt) key and then use Cmd-C to copy the selection.

You can hold shift when selecting text this way it won't invoke copy and paste mode.
https://awhan.wordpress.com/2012/04/18/tmux-copy-paste-with-mouse/
you will notice that you are not able to select text at all.
The solution is to use the shift key.
Hold down the shift key and then left click and drag across the target text. If you want to now paste the selected text back in to xterm, you must also hold down the shift key and then middle click in order to paste the text. This is not mentioned in the tmux man pages so i do not think this is a tmux feature. guess this has something to do with xterm, but i m not sure.
https://superuser.com/questions/300060/tmux-and-text-selection-with-mouse-through-putty

Using the set -g mouse on as specified above works to use the mouse for scrolling.
On Linux the shift button works to allow the normal mouse selection and copy/paste, but on Mac it seems to be the Alt button, give that a try (it works for me using tmux on iTerm2).

On OSX and tmux 3.0a with set -g mouse on in your tmux.conf, you can use ctrl-b [ then a mouse drag to select text. After which you should be able to paste with ctrl-b ].

I've tried many different solutions but the only working (scrolling with mouse and possibility to copy/paste using usual CMD+C/V) for me was the one stated over here:
i.e. put this inside ~/.tmux.conf:
# macOS only
set -g mouse on
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
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-vi C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down
# To copy, left click and drag to highlight text in yellow,
# once you release left click yellow text will disappear and will automatically be available in clibboard
# # Use vim keybindings in copy mode
setw -g mode-keys vi
# Update default binding of `Enter` to also use copy-pipe
unbind -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"
then run tmux command source-file ~/.tmux.conf (by typing CTRL+B, followed by :)

Related

Use tmux without two-step commands

On my local machine I can use the shortcut CTRL+b and then d to detach from a tmux window.
Google Cloud Platform offers to connect via SSH into virtual machines using a terminal in a browser window, see screenshot below.
In this terminal, I cannot use the shortcut CTRL+b and then d to detach from a tmux window.
None of the two-step commands I tried work.
By two-step commands I mean commands that are supposed to be triggered by pressing a combination of buttons, releasing this combination of buttons, and then pressing another combination of buttons.
How can I use tmux without having to use two-step commands?
In your .tmux.conf file, you can set keybinds that do not use a prefix with -n
# would activate with <prefix> R
bind-key R source-file ~/.tmux.conf \; display-message "Config reloaded..."
# would activate with just R
bind-key -n R source-file ~/.tmux.conf \; display-message "Config reloaded..."
Of course, these binds will activate whenever you hit that button, so if you used the above example, it would be very difficult to type a literal 'R'.

Rebinding tmux prefix to alt key

I'd like to change the tmux prefix from ctrl+b to a single key, specifically alt. Tried set-option -g prefix M, but it said bad key.
Anyone know how to accomplish this without having to create a macro on my entire system making alt trigger ctrl+b?
I'd settle for a macro that only triggers when inside of a tmux session.
A possible workaround is to use tmux's root table instead of the prefix key table (see bind-key in man page). To do this, you need to pass the -n argument to the bind-key command. This does imply that you will have to re-bind all the commands in your .tmux.conf that you want to support.
An example of how to create a window using alt+c is:
bind -n M-c new-window

history-limit is not working after C-l in tmux

My environment is Fedora20 (32-bit) + Yakuake + fish + tmux.
Months ago, I started to use tmux, it is a great terminal multiplexer, but the scrollback (Use prefix then PageUp to scroll back) is not working as expected, the history limit is always under 2000 (around 1980 on the right above corner) even if I already set the scrollback limit to "unlimited scrollback" in Yakuake Setting.
Finally I could take it anymore and started to goole it for solution, found out that I could put set -g history-limit 30000 int my tmux.conf file, then check again, it worked, but I realized that every time I typed C-l to clear the screen(and I use it a lot), the scrollback history will be only under/around 30.
Then I found out at How do i clear tmux screen while tailing logs? that I could put bind l send-keys -R into .tmux.conf and use prefix-l to clear the terminal history buffer temporarily and still be able to scroll back, it worked just as I wanted. But What I really want is I use only C-l to do the trick without the prefix key, replacing the shell built-in C-l to do it...
Update:
Short ask, after setting a bind in .tmux.conf, in tmux, you have to type the prefix key before typing that key, then how to bind a key without the need to add the prefix key by default??
In order to bind a key without the need to add the prefix, use the -n flag for the tmux bind command.
Here's the example that should be put in tmux.conf:
bind -n C-l display-message 'foo'
The example binding is triggered when Ctrl-l is pressed (no prefix) and it displays message 'foo' in the tmux prompt.
All that said, be careful & conservative with setting these "direct" key bindings because the key will become unusable for any other terminal program.

tmux key bind C-Tab (Ctrl + Tab) to switch/cycle to the next pane

I'm trying to bind Ctrl + Tab to cycle to the next tab.
I've added this to my ~/.tmux.conf file:
bind -n C-Tab select-pane -t :.+
But it doesn't work,
however it i change it to Ctrl + a it works fine:
bind -n C-a select-pane -t :.+
Any ideas?
My first thought is my terminal maybe hijacking it but i cant see anywhere, and nothing happends with Ctrl + Tab normally for me in the terminal, just a tab space is inserted.
I'm using xfce4-terminal if that helps, else maybe im doing something wrong in my tmux conf.
Terminal wont sent key. You have the same issues for vim, and so on. The seams to work because the terminal grabs its, but it wont send to your current job inside it. I strongly recommend to use other mappings, ( i know, sounds stupid .... ).
take a look at this post, might help:
vim cant map <C-Tab> to :tabnext

Overriding tmux shortcuts

Is it possible to change tmux shortcuts such that:
ctrl+t opens new window
ctrl+w closes a window
ctrl+tab cycles through a list of windows
other similar shortcuts for splitting horizontally / vertically
I really like its features but am always of the opinion that the less shortcut I have to learn the better.
It seems like you want to do this without the command prefix key. If that's correct, add the following commands to your ~/.tmux.conf file:
bind-key -n C-t new-window
bind-key -n C-w kill-window
bind-key -n C-Tab next-window
If you want to use the prefix key, then drop the -n from the above commands.

Resources