How to configure tmux so that it hehaves just like a normal terminal?

1,264 views
Skip to first unread message

Peng Yu

unread,
Jan 26, 2021, 11:51:48 AM1/26/21
to tmux-users
I have the following tmux installed on Mac OS X and Linux.

$ tmux -V
tmux 3.1c

I have the following .tmux.conf, which allows automatic copy of highlighted text in a terminal. But it doesn't copy anything in a terminal on Linux. I know that the problem is probably due to pbcopy.

I want to have an identical configure file so that it can allow use highlight text and type command-C (Mac) or ctrl-shift-C (Linux) for copying. Basically, I just want the normal behavior of a terminal. Could anybody let me know the tmux code to figure this?

$ cat ~/.tmux.conf
# Send prefix
set-option -g prefix C-a
set-option -g repeat-time 2000
bind-key c-R source-file ~/.tmux.conf \; display-message "~/.tmux.conf sourced"

bind-key v split-window -h -c "#{pane_current_path}" #\; switch-client -T prefix
bind-key s split-window -v -c "#{pane_current_path}" #\; switch-client -T prefix

bind-key -r h select-pane -L
bind-key -r j select-pane -D
bind-key -r k select-pane -U
bind-key -r l select-pane -R

bind-key -r - resize-pane -D
bind-key -r + resize-pane -U
bind-key -r < resize-pane -L
bind-key -r > resize-pane -R

bind-key c new-window -c "#{pane_current_path}"

bind-key -r n next-window
bind-key -r p previous-window

set-option -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-pipe pbcopy

bind-key -T prefix P paste-buffer

set-option -g history-limit 10000
set-option -g mouse on

bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe pbcopy


Nicholas Marriott

unread,
Jan 27, 2021, 5:08:56 AM1/27/21
to Peng Yu, tmux-users
You can install and use xsel instead of pbcopy on Linux. Or set up tmux to use OSC 52 if your terminal supports it, see https://github.com/tmux/tmux/wiki/Clipboard.

I don't know what it means to behave like a normal terminal, which terminal? You can make mouse selection stay around until you copy and bind C-c or M-c in tmux to copy by changing the key bindings.

Alternatively, most terminals let you hold down Shift to override tmux mouse and handle the mouse themselves, but that will only work for visible text, not the scrollback.



--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/tmux-users/6f6f9895-0478-4c3a-b21c-574cde0c1cd4n%40googlegroups.com.

Peng Yu

unread,
Jan 27, 2021, 9:46:11 AM1/27/21
to Nicholas Marriott, tmux-users
> I don't know what it means to behave like a normal terminal, which
> terminal? You can make mouse selection stay around until you copy and bind
> C-c or M-c in tmux to copy by changing the key bindings.

I mean in Mac Teminal, I just type command+C to copy (after
highlighting the text). In Linux qterminal, I just type ctrl-shift-C
to copy (after highlighting the text). However, I want to have the
same configuration instead of have one configuration file for Mac and
another configuration file for Linux.

--
Regards,
Peng

Nicholas Marriott

unread,
Jan 27, 2021, 11:59:44 AM1/27/21
to Peng Yu, tmux-users
tmux will not receive either of those keys from the terminal so you will need to pick a different one like M-c.

Sean Hammond

unread,
Jan 27, 2021, 4:13:32 PM1/27/21
to tmux-...@googlegroups.com
On Tue, 26 Jan 2021, at 4:51 PM, Peng Yu wrote:
> I want to have an identical configure file so that it can allow use highlight text and type command-C (Mac) or ctrl-shift-C (Linux) for copying. Basically, I just want the normal behavior of a terminal. Could anybody let me know the tmux code to figure this?

You might find this guide useful:

https://www.seanh.cc/2020/12/27/copy-and-paste-in-tmux/

It uses the tmux-yank plugin to achieve the cross-platform behaviour.

Also see my tmux config:

https://github.com/seanh/tmux

This doesn't bind command-c on mac. But this is the line that binds control-c on linux, you could add a similar line for mac but using command-c and pbcopy:

bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"

Peng Yu

unread,
Jan 27, 2021, 6:20:11 PM1/27/21
to Sean Hammond, tmux-...@googlegroups.com
On 1/27/21, Sean Hammond <tmux-...@snhmnd.fastmail.com> wrote:
> On Tue, 26 Jan 2021, at 4:51 PM, Peng Yu wrote:
>> I want to have an identical configure file so that it can allow use
>> highlight text and type command-C (Mac) or ctrl-shift-C (Linux) for
>> copying. Basically, I just want the normal behavior of a terminal. Could
>> anybody let me know the tmux code to figure this?
>
> You might find this guide useful:

I don't quite understand this. It seems that tmux makes it more
complicated than necessary by disable the default behavior of Mac
Terminal or Linux terminal Is it so? Why not just let them behave as
what they would do without tmux in the aspect of copying?

I don't think it is a good idea to make it so difficult to just get
back the default behavior of terminals. Any simpler solutions
available?

> https://www.seanh.cc/2020/12/27/copy-and-paste-in-tmux/
>
> It uses the tmux-yank plugin to achieve the cross-platform behaviour.
>
> Also see my tmux config:
>
> https://github.com/seanh/tmux
>
> This doesn't bind command-c on mac. But this is the line that binds
> control-c on linux, you could add a similar line for mac but using command-c
> and pbcopy:
>
> bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"


--
Regards,
Peng

Scott Rochford

unread,
Jan 27, 2021, 9:50:48 PM1/27/21
to Peng Yu, Sean Hammond, tmux-users
Hi Peng,

I don't quite understand this. It seems that tmux makes it more
complicated than necessary by disable the default behavior of Mac
Terminal or Linux terminal Is it so? Why not just let them behave as
what they would do without tmux in the aspect of copying?

'tmux' is a terminal multiplexer, i.e. a way to give you access to multiple terminals within one terminal.  One problem with doing this is that a single terminal only supports one scrollback history, and is unaware of screen splits.  So the default behaviour of copy and paste doesn't work in many cases because when you try to copy text on the screen which includes some of the scrollback history, the history is mostly likely not part of the same output.  And if the screen is split in some way, especially vertically, it will copy text from multiple terminals.

That's the reason why 'tmux' has to provide it's own clipboard and copy/paste functionality, so it can correctly handle those situations.

Pressing 'shift' while selecting is the easy solution if you want to only copy a small piece of text which doesn't include any history or other split panes.  'shift' toggles the mouse between "terminal mode" and "application mode".  On many terminal clients such as 'putty' for example, you can reverse the 'shift' behaviour so that the default is reversed.  You may prefer this.

Basically with 'tmux' we are doing things with terminals that they were not originally designed for, so some imagination is required.  I hope this helps explain why it's more complicated than it probably should be.

Regards,

Scott

--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.

Chas. Owens

unread,
Jan 28, 2021, 12:56:05 PM1/28/21
to Peng Yu, Sean Hammond, tmux-users
Terminal emulators often have an option to avoid sending mouse events to the terminal app (usually you just have to hold the shift key). Some of them also have an option to change the default to not sending mouse events to the terminal app.  However, this is still a bad experience for most users as the scrollback window in the terminal emulator is not trustworthy when running tmux (or vi or any other terminal app that uses the alternate screen functionality of terminals) and if you use splits, the terminal emulator has no idea where the splits are and will not behave the way one would desire.

As for why this is complicated, terminals used to be printers attached to a serial port (teletypewriters). Then we got "glass teletypes" which used a CRT instead of a printer. Then came DEC's Video Terminals in the '70s with standards like VT-05, VT-50, VT-100, etc. which allowed command line programs to do amazing things like change a character at a specific x and y coordinate instead of only being able to add lines at the bottom of the screen.  All terminal emulators today are just software versions of those physical terminals.  Because they are software, they can do things that originals could not, like allow selecting of text without the program running in the terminal's help (which is how selecting text and hitting cmd-C works).  Because the terminal emulator is doing that, the application running in the terminal is completely ignorant of the actions you are taking.  Some terminal protocols do allow passing of mouse events to applications that have asked for them.  Tmux can ask for this with "set-option -g mouse on", at that point the mouse event is no longer processed by the terminal emulator, but passed to tmux.  Tmux as a terminal app can only be sure it can talk to the terminal emulator currently controlling the pty (which is short for pseudotty where tty is teletypewriter) it is attached to, so it can't tell the OS to add the text to the clipboard (because it could be running under ssh for instance).  X Windows and macOS however provide command line programs that can talk to the clipboard.  So the right answer is for the user (who knows how the system is set up) to configure tmux to "do the right thing" for the user in that situation.  There was an attempt to fix this nightmare by adding escape sequences (how an application talks to the terminal emulator) to tell it to update the clipboard (see OSC 52), however, support for those escape sequences is not 100% (for instance Terminal.app in macOS does not support them). Even terminals that do support them don't always do it the same way: https://sw.kovidgoyal.net/kitty/protocol-extensions.html#pasting-to-clipboard.  There are also security concerns as an user with root access on a box you ssh into could read/set your local operating system's clipboard contents.

tl;dr terminal emulators are a series of hacks for backwards compatibility dating back to the dawn of digital computers and it is amazing any of this works at all.


Peng Yu

unread,
Jan 28, 2021, 9:28:57 PM1/28/21
to Sean Hammond, tmux-users
Hi Sean,

> > I want to have an identical configure file so that it can allow use highlight text and type command-C (Mac) or ctrl-shift-C (Linux) for copying. Basically, I just want the normal behavior of a terminal. Could anybody let me know the tmux code to figure this?
>
> You might find this guide useful:
>
> https://www.seanh.cc/2020/12/27/copy-and-paste-in-tmux/

The "Tmux Plugin Manager" instruction mentioned in the above webpage
is confusing.

https://github.com/tmux-plugins/tpm

On the above page it says to add these to ~/.tmux.conf

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

But the clone was only about the first line. How can I add the second
line when it is not cloned?

$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

>
> It uses the tmux-yank plugin to achieve the cross-platform behaviour.
>
> Also see my tmux config:
>
> https://github.com/seanh/tmux

Which approach do you recommend? Your approach or the approach on
seanh.cc? What is their difference?

> This doesn't bind command-c on mac. But this is the line that binds control-c on linux, you could add a similar line for mac but using command-c and pbcopy:
>
> bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"

So then this file will not be same anymore for both mac and Linux.
Therefore, this solution does not address my original question?

--
Regards,
Peng

Sean Hammond

unread,
Jan 29, 2021, 7:18:24 AM1/29/21
to tmux-...@googlegroups.com
On Fri, 29 Jan 2021, at 2:28 AM, Peng Yu wrote:
> The "Tmux Plugin Manager" instruction mentioned in the above webpage
> is confusing.
>
> https://github.com/tmux-plugins/tpm
>
> On the above page it says to add these to ~/.tmux.conf
>
> set -g @plugin 'tmux-plugins/tpm'
> set -g @plugin 'tmux-plugins/tmux-sensible'
>
> But the clone was only about the first line. How can I add the second
> line when it is not cloned?

You only need to clone Tmux Plugin Manager (tpm) itself. After that, Tmux Plugin Manager will clone the plugins (tmux-sensible, tmux-yank, etc) for you when you run `~/.tmux/plugins/tpm/bin/install_plugins`.

> $ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
>
> >
> > It uses the tmux-yank plugin to achieve the cross-platform behaviour.
> >
> > Also see my tmux config:
> >
> > https://github.com/seanh/tmux
>
> Which approach do you recommend? Your approach or the approach on
> seanh.cc? What is their difference?

They're the same. Or nearly identical anyway. github.com/seanh/tmux is my actual current tmux config. The blog posts on seanh.cc are tutorials explaining how parts of my config work.

> > This doesn't bind command-c on mac. But this is the line that binds control-c on linux, you could add a similar line for mac but using command-c and pbcopy:
> >
> > bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"
>
> So then this file will not be same anymore for both mac and Linux.
> Therefore, this solution does not address my original question?

A single config file could bind two different shortcuts, one for mac and one for linux. For example (you'd have to fill out the pbcopy command for mac, I don't have a mac so I can't test it):

# Make Ctrl+c copy into the clipboard. This will only work on Linux.
bind -T copy-mode C-c send -X copy-pipe-no-clear "xsel -i --clipboard"
bind -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"

# Make Alt+c copy into the clipboard. This will only work on mac.
bind -T copy-mode M-c send -X copy-pipe-no-clear "pbcopy ..."
bind -T copy-mode-vi M-c send -X copy-pipe-no-clear "pbcopy ..."

I don't have an example for how to make a single keyboard shortcut copy into the clipboard on both mac and linux. But I think it'd be easy enough to write a shell script that tries to call xsel and if that fails tries pbcopy. Then have a key binding in your tmux.conf call that script.

The tmux-yank plugin already does this: it uses "y" as the keyboard shortcut to copy into the clipboard and works on both mac and linux: https://github.com/tmux-plugins/tmux-yank

Note that you cannot bind the macOS command key in tmux. That's not possible. Tmux only supports Ctrl, Alt and Shift modifiers. I don't think this is tmux's fault--I think it's just a limitation of terminal apps.

I have a tutorial on binding keys in tmux you might find helpful: https://www.seanh.cc/2020/12/28/binding-keys-in-tmux/

Sean Hammond

unread,
Jan 29, 2021, 7:23:55 AM1/29/21
to tmux-...@googlegroups.com
On Fri, 29 Jan 2021, at 12:18 PM, Sean Hammond wrote:
> I don't have an example for how to make a single keyboard shortcut copy
> into the clipboard on both mac and linux. But I think it'd be easy
> enough to write a shell script that tries to call xsel and if that
> fails tries pbcopy. Then have a key binding in your tmux.conf call that
> script.

Or I guess even without a shell script you could have a key binding in tmux.conf that runs both xsel and pbcopy. Each time you hit it one of the commands will fail and one of them will succeed, depending on which OS you're on. Might work :) See:

https://www.seanh.cc/2020/12/28/binding-keys-in-tmux/#binding-a-key-to-run-multiple-commands-at-once

Chas. Owens

unread,
Jan 29, 2021, 11:28:59 AM1/29/21
to Peng Yu, Sean Hammond, tmux-users
That is what if-shell exists for:

if-shell "[[ $(uname) == 'Darwin' ]]" {
  #macOS specific options

  bind-key -T copy-mode-vi y send-keys -X copy-pipe pbcopy
} {
   #linux specific options
  bind-key -T copy-mode-vi C-c send -X copy-pipe-no-clear "xsel -i --clipboard"
}

--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.

Chas. Owens

unread,
Jan 29, 2021, 11:33:09 AM1/29/21
to Peng Yu, Sean Hammond, tmux-users
Whoops, I didn't notice that the commands were so different, this is a better example

if-shell "[[ $(uname) == 'Darwin' ]]" {
    # macOS specific options

    bind-key -T copy-mode-vi y send-keys -X copy-pipe pbcopy
} {
    # non-macOS specific options
    bind-key -T copy-mode-vi y send-keys -X copy-pipe "xsel -i --clipboard"
}
Reply all
Reply to author
Forward
0 new messages