
Tmux Tutorial - keyboardman
https://leimao.github.io/blog/Tmux-Tutorial/
======
Townley
One tip I didn't see mentioned (though it's pretty common in tmux) is to use
the default window layouts for organizing multiple panes. You do this with
Ctrl + b followed by esc + <num>.

esc + 1: All windows take up equal space horizontally, and stretch the full
vertical width

esc + 2: All windows take up equal space vertically, and stretch the full
vertical width

esc + 3: The first window takes up half of the screen vertically, and all
remaining windows divide up the space equally

esc + 4: The first window takes up half of the screen horizontally, and all
remaining windows divide up the space equally

esc + 5: Windows are divided into two equal columns horizontally, and split
remaining space vertically.

Even if I have to alter the sizes a bit usually, the pre-defined layouts are
super useful for positioning different windows.

~~~
lytedev
Just to nitpick a tiny bit, but you mean "panes" and not "windows". Tmux's
terms can be slightly confusing and the difference is important in usage!

~~~
Townley
Good point, thanks for that. Fixing now!

------
ridiculous_fish
If you're on a Mac, try tmux -CC with iTerm2. You get tmux goodness like
multiple panes, persistent sessions, etc. But you also get native scrolling,
native copy and paste, etc. It's what makes tmux usable for me.

~~~
AceJohnny2
Absolutely! TMux is pretty great by itself, but it doing its own internal
scrolling and copy-pasting, though essential, is a usability frustration.

Tmux + iTerm2 is a fantastic combo that takes all of Tmux's pwer and removes
all the downsides.

I searched if anything else used Tmux's Control Channel on Linux to replicate
iTerm2's features, and I'm sad that I found nothing.

(Frankly, it's kind of stunning to me that one terminal app on macOS has more
useful features than the plethora of me-too terminals on Linux)

~~~
calcifer
> Frankly, it's kind of stunning to me that one terminal app on macOS has more
> useful features than the plethora of me-too terminals on Linux

If by "useful features" you mean intercepting tmux output and having it
integrate with the terminal, then no. But more generally, I think the closest
thing to iTerm2 on Linux is Tilix [1].

[1] [https://gnunn1.github.io/tilix-web/](https://gnunn1.github.io/tilix-web/)

------
yason
The combo of mosh+tmux (and earlier, screen) really is something powerful that
most non-command-line people can't ever begin to fathom. I've been mostly
working through a terminal multiplexer for a couple of decades for work and
pleasure and the benefits are enormous.

I of course use graphical applications for browsing, image editing, etc. but
nearly everything else I do is from the command line and almost all of that
happens over tmux, either locally or remotely. I prefer using a new Tmux
window instead of a new graphical window for a temporary shell so I usually
just end up having one terminal emulator running on my screen.

The value here is state: tmux keeps my state forever which gives me one less
thing to worry about or remember. If I can't finish something right away, I
know I'll just go back to the tmux window when I have time. Laptop running out
of battery? No problems, I'll just reconnect when I find a charger and reboot
my laptop.

For example, I have the exact cursor-to-cursor same view for what I do at work
whether I'm at my desktop workstation in the office or using my laptop from
the home couch. I can literally leave my cursor on a certain line, leave the
office, go home, and continue exactly from where I was when I left, the same
exact line. Especially using mosh makes it even more transparent: I just open
my laptop at home and my work tmux session is already waiting there on my
screen, and will update as soon as I reconnect the work vpn.

I'd much prefer to have something like this for browsing too. I'd like to run
a browser muxer server that keeps all my tabs online with all the runtime
state, making it available to me wherever I happen to connect from. Browsing
something interesting on the train but then I had to hop off? No problem, I'd
return to that session on the big screen as soon as I get home and open my
laptop. Tab and bookmark syncing doesn't go far enough: I'd like to share one
live browser process and access that from everywhere.

I could imagine I could rig up something like that using VNC and Xnest/Xephyr
but a custom, hand-designed protocol for remote rendering natively in the
browser might just work more seamlessly. I would like the browser in my phone
to look like a mobile browser even if the content and state came from the
browser server. And I definitely would not want to operate a desktop browser
over VNC using my phone.

------
jdcaron
I have been using screen for almost 10 years now, is it worth switching? I
know it's not much learning but why switching when screen is not broken? It
feels like there's more important stuff I can learn.

~~~
sam_lowry_
Tried tmux, then went back to screen. Tmux is for hipsters.

~~~
petee
Meaning what? It's different?

~~~
sam_lowry_
I feel like tmux is styled more towards Mac users, with rich UI and verbose
options.

screen is more akin to a unix program that borrows the styling of options from
getopts.

------
photonios
Oh man, I love tmux.

I guess I am not a very advanced user but some of its features really appeal
to me.

We use Macbooks at work and initially I used iTerm2. I've always been a heavy
user of the terminal and split views. (Neo)Vim is my main editor. However, I
experienced difficulties switching back and forth between my work laptop and
my personal Ubuntu system at home. I switched to tmux to be able to have the
exact same set up on both OS's.

Although that was my initial reason for switching; I have come to love tmux.
Just the fact that I can quickly SSH into my home machine, attach to my tmux
instance and continue working from anywhere is amazing.

------
Improvotter
I tried to use tmux as a terminal multiplexer on sway just so I could make it
remember a setup of terminals for specific projects. That way I could simply
do `tmux-command start project-name` and have it automatically open a setup of
windows and panes. Though I found it unfortunate that I had to learn new
shortcuts that are different for shortcuts elsewhere. A simple example but
which was a deal breaker was in order to work with its clipboard system, you
had to use different commands. Does someone with more knowledge of tmux know
whether it's possible to get rid of the prefix command somehow? I know about
the i3 version of tmuxinator, but it doesn't support sway unfortunately. I
might make something to automatically open a list of windows on a specific
workspace for sway.

~~~
airstrike
Just use `bind-key -n` for a no-prefix bind

See e.g. [https://unix.stackexchange.com/questions/450184/in-tmux-
how-...](https://unix.stackexchange.com/questions/450184/in-tmux-how-to-set-
shortcut-for-copy-mode)

------
theSage
We use tmux for remote peer programming in a pinch.

If Alice has a session on a remote machine you have access to. You can run
`tmux new-session -s bob -t Alice`

With this both share he same view but can move cursors independently.

~~~
u801e
> With this both share he same view but can move cursors independently.

Does this mean they have 2 separate cursors, or that that can control the same
cursor? Your can sort of do the same thing in screen by using a nested screen
session and sharing the outer session with the other user.

------
Legogris
For those who customize their configs and, like me, SSH into all kinds of
different short- and long-lived servers regularly: How do you stay sane?

I don't want to have to scp over my tmux conf to every single server I jump
on, but OTOH difference in pretty much anything but the prefix gets really
frustrating with habits. Particularly remembering all the emacs mode-keys when
I really want the vi ones.

~~~
mmahemoff
I have a Github dotfiles repo so I can set it up on any server in a couple of
minutes, just by cloning the repo and running a script within it. If you're
jumping between different servers, I would highly recommend having a quick way
to setup config like this. It's a short investment that pays dividends for
many years - not just for tmux config, but setup for bash, vim, and any other
utils you rely on.

I use one tmux prefix for the local machine and another one for ssh sessions
(setup in bash_profile using something like `if [[ -n "$SSH_CLIENT" ]] ; then
tmux set-prefix ... etc`. You need different prefixes when using tmux
recursively like this, so you can navigate both the inner and outer session.

I also use a different color theme for ssh sessions, whch helps to know which
prefix to use until it becomes somewhat subconscious.

~~~
Legogris
Used to have that actually, it's just too much of a hassle in today's cloud
environment (I can easily SSH into dozens of fresh instances over a day and
there's no way I'm baking my personal config into an image). I was considering
to revisit and streamline that, but I imagine the cognitive overhead of
different remote hosts having different configs is worse than what I have now
(local=my config, remote=vanilla).

I just bind a different prefix and moved the bar to the top locally (I use
urxvt+tmux with Ctrl+arrow keys etc to navigate tmux tabs instead of a tabbed
terminal client) to get around the nesting issue.

------
Abishek_Muthian
I used PM2[1] as process manager for Node.js, Python services in the cloud
servers. It did its job well, apart from ensuring that process is always up,
it provided monitoring capabilities(incl. emails), capturing logs from the
stdout among other things.

Recently I wanted to manage a Golang service, found that PM2 is not stable for
it yet and I didn't want to install Node.js just for managing the Golang
service; so used Tmux for it. It keeps the process up, we can get the basic
process stats as in any terminal and I use external monitoring service for
alerts when the service is down[2].

[1]:[http://pm2.keymetrics.io/](http://pm2.keymetrics.io/)

[2]:[https://uptimerobot.com/](https://uptimerobot.com/)

~~~
nimrody
Tmux won't restart the service when it fails. It won't start the service when
the machine reboots (or even if someone deliberately reboots the machine and
forgets about the service).

Assuming you're on a modern Linux distribution, it's pretty easy creating a
systemd service file which will make it easier to manage the service.

~~~
Abishek_Muthian
You're correct, I should have mentioned the purpose. I'm currently testing the
service, want it stop when it fails and just not when I exit the SSH; hence
the tmux.

For restarting the service, systemd is the way to go.

------
mjbrownie
I use tmux in conjunction with tmuxp and highly recommend the pairing (or
alternately use tmuxinator). a bash shortcut I use every day to
attach/create/restore dozens of sessions is.

    
    
      pc:bt2$ cat ~/bin/t
      #!/bin/bash
      if [[ $# == 0 ]]; then
          echo tmux list-sessions
          echo
          tmux list-sessions
          echo
          echo tmuxp sessions
          echo
          ls ~/.tmuxp
    
      else
          tmuxp load $1 || tmux attach -t $1 || tmux new-session -s $1
      fi
    

usage "t session_name"

------
naikrovek
One of the downsides listed when you don't use tmux is that you have to type
your password for each terminal you open to the remote machine. Does no one
use ssh-agent?

~~~
Xeago
This somewhat painfully breaks when reattaching as the socket from the shells
environment could be defunct. Whereas the shell session outside has a
different, totally fine agent in its environment. This was somewhat painful to
explain to some engineers and I suck at explaining so I’ll gladly receive a
good explanation for it (or a nice workaround?) :D

~~~
gylterud
Are you talking about running ssh-agent on the server side? To further connect
to some other machine?

For that I use a file to save the name of the socket and PID, and restore this
in my .profile.

------
msadowski
I love tmux and use it extensively in my workflow. One thing that I found very
useful was binding "synchronize panes" to ctrl+b+e combination, this allows me
to quickly modify the configurations.

tmuxp is also a great tool for creating predefined layouts. Personally I store
a config for every client and by typing "tmuxp load client_config" my session
is immediatelly available and I can get productive within seconds.

------
Zaheer
Pro-Tip: Use it with autossh for automatically reconnecting during computer
standby / internet drops. Use iTerm for a 'native' feel to tmux (ex.
copy/paste, tabs, history, etc). I used to use Mosh + TMux for super durable
connections and that worked beautifully. Unfortunately Mosh doesn't play well
with ITerm's native integration so you can achieve a poor man's Mosh by using
[http://www.harding.motd.ca/autossh/](http://www.harding.motd.ca/autossh/).

All-together it looks something like this: Open iTerm. autossh -t myRemoteHost
"tmux -CC -A"

------
js8
Cool, a coworker showed me tmux yesterday.

One question we had was about scrolling in the tmux window and keeping the
terminal output (as in Linux where I can scroll back with Shift+PageUp). Does
anybody knows how that works with tmux?

~~~
goldemerald
You can enter scroll mode with ctrl+b+[ then you can mouse wheel, PageUp, etc.

To exit just press q or ctrl+c

~~~
mjbrownie
ctrl+b, pageup is enough to start scroll back so you can skip [

------
pw6hv
Looks like some of the shortcuts are really RSI prone, i.e. ctrl + b + &
(where & is shift + 7). Are there any possibilities to have more wrist-
friendly shortcuts?

~~~
tjoff
A very common change is to change the prefix to ctrl + a, like this in your
tmux.conf:

    
    
      # remap prefix from 'C-b' to 'C-a
      unbind C-b
      set-option -g prefix C-a
      bind-key C-a send-prefix
    

I've heard that he reason for ctrl + b is because screen uses ctrl + a, which
just doesn't make any sense to me. You can nest both tmux and screen and to
get to an inner layer you press (if ctrl + a has been mapped) ctrl + a + a -
and now your command is sent to the inner tmux/screen.

Defaults matter. It is not feasible to configure this on every machine you
touch so one have to remember both the default one and the sane one...

~~~
JerwuQu
I think another reason is because some people use Ctrl+a in bash for jumping
to the start of the line.

~~~
tjoff
Thanks, that is a good point!

------
spraak
One of the biggest improvements in my tmux productivity came when I changed my
prefix (default Ctrl + b) to backtick (i.e. to `) I am a heavy user of tmux
and spend a lot of time switching panes, switching windows, entering visual
mode, detatching etc. so this really feels faster.

    
    
        # change prefix to backtick
        unbind-key C-b
        set-option -g prefix `
        bind-key   ` send-prefix

~~~
yason
Backtick is a bit far away, isn't it? I literally have to stretch my left ring
finger to hit it, so doing this for each tmux key command does add up I
suppose?

I've been using C-z for the prefix for ages. It's conveniently nearly under
the default position where my fingers lay and it also masks the mostly useless
suspend signal which I seemed to accidentally hit every now and then. (If I
_really_ want to suspend my program 'C-z z' will do just fine.)

Switching around using, for example, C-z C-n/C-p is pretty fast to key in. You
can keep ctrl down for support and then it reduces down to typing 'zn' / 'zp'.
YMMV.

~~~
spraak
Ah you're right, I forgot that the keyboard I use isn't common/standard.
Backtick is right above Q for me.

------
xhgdvjky
OT:

How can I use my Mac's mouse to switch the active window in tmux? I have tried
Googling this and the .conf setting everyone says to use doesn't work.

~~~
lytedev
Depends on your version so check your docs and man pages, but this ought to
work:

    
    
        set-option -g mouse on

------
olalonde
Pro-tip: if you are using iTerm2, you can set this as your "Send text as
start" and always be in a tmux session:

    
    
        tmux attach || tmux new -s main
    

I also recommend remapping ctrl-b to ctrl-space (set -g prefix C-Space).
Easier to hit and as a bonus, ctrl-b will work as expected when connected to a
remote tmux session.

~~~
yjftsjthsd-h
If you ever use more than one session, I think you'll probably need to change
that to `tmux attach -t main`

------
genericacct
Paging mileac: the tmux cockpit (*) is down and my productivity is suffering

[http://cockpit.27ae60.com/](http://cockpit.27ae60.com/)
[https://news.ycombinator.com/item?id=15432986](https://news.ycombinator.com/item?id=15432986)

------
geospeck
Another useful source for learning Tmux that helped me a lot is the free
videos from Upcase[1]

They even show how to use Tmux with Vim which is my favourite setup

[1] [https://thoughtbot.com/upcase/tmux](https://thoughtbot.com/upcase/tmux)

------
sys_64738
Another alternative is emacs server.

~~~
nine_k
I run both.

Emacs's terminal emulator is almost correct but last few times I tried to use
it I had trouble with certain curses-based programs.

Also, disconnecting tmux UI (and even restarting X) does not affect the
session, and you can reconnect. Unfortunately, I was not able to reproduce it
with Emacs for some reason, though it should be possible.

------
ses1984
What's a good way to handle ssh and tmux, if I'm already in a tmux session in
my desktop?

I'm in a tmux session in my desktop. Then I ssh into a server from within
tmux. I want to start tmux to be able multiplex and detach from some tasks.
What do?

~~~
umbs
Use ctrl+b once to send commands to local Tmux server, twice to send commands
to Tmux running on remote machine you SSH'ed in to. I think this can go on, in
theory, but I did not test.

------
kiranp
This one is a gem of a thing.

------
mkor
What about byobu built on top of tmux? Isn't it more straightforward?

~~~
fullstop
I've not used byobu in about a decade, but it has crashed on me in the past.
I've never had tmux crash.

The other thing I didn't care w/byobu was that it had a clock in the bottom
row. Once a minute my terminal window would light up telling me that something
on that tab had changed.

~~~
dustinkirkland
Author/maintainer of Byobu here... Tell me more about your "byobu crash". Can
you point me to any bug reports in either bugs.launchpad.net/byobu or
github.com/dustinkirkland/byobu ? Thanks! @DustinKirkland

~~~
fullstop
Hi Dustin, thanks for the response. Unfortunately this was nearly a decade ago
on Ubuntu server 10.04, and I'm sure that a lot has changed since then.

If I have the time, though, I'll fire up byobu and see if I can make it happen
again. I tend to leave sessions open for many months and it wasn't something
which happened on a regular basis, and I never experienced it with bare tmux.

------
jeliotj
This is an acceptable tutorial but it could use a bit of editing to make the
English more idiomatic. Also, except for the bit on plugins, I don't think it
adds to or simplifies the man page.

------
chris_wot
Does anyone have a good setup for debugging? There is voltron, but without
tmux it won't work well, and I'd love to see a good layout that I could easily
use.

------
auiya
What's the fastest way to scroll back through terminal history? Native
scrolling doesn't appear to work and you have to annoyingly enter copy mode
first?

~~~
Liskni_si
With `set -g mouse on` in ~/.tmux.conf, scrolling via mouse/touchpad works, at
least in my setup.

------
emilfihlman
Never understood why tmux had to be different and use ctrl+b when ctrl+a is
already standard and much, much better.

~~~
tomcooks
isn't ctrl+a the BASH default for 'go to the beginning of the line'?

~~~
u801e
It is. If you're using screen, you can press ctrl-a a to send ctrl-a to
readline to go to the beginning of the line. ctrl-b will actually move back a
single character. Presumably, you could do something like ctrl-b b in tmux to
move back one character, but I haven't tried it, so I don't know if that would
work.

------
qatanah
Am I the only one using screen? Couldn't switch to tmux since most of what i
need is in screen.

~~~
chrisperkins
Can we do vertical splits in screen without having to install additional
plugins? How?

~~~
teddyh
Yes.
[https://www.gnu.org/software/screen/manual/screen.html#Split](https://www.gnu.org/software/screen/manual/screen.html#Split)

------
bananamerica
I happiky use about 1% of Tmuz and and only truly understand half of that.
Still super useful.

------
arbhassan
For someone using i3 WM does Tmux offer any benefits?

~~~
hr2016
Yes, leaving a session running in the background/remotely though you might get
disconnected or what ever. Same as screen.

