
Using tmux properly - cmmn_nighthawk
http://danielallendeutsch.com/blog/16-using-tmux-properly.html
======
rnhmjoj
Yesterday I was helping a friend setting a box with no graphical session: I
fired up tmux in the linux virtual terminal and then proceeded to realize
without my tmux.conf I couldn't do a thing because I have always been using
entirely different keybindings from the default. This is the problem with too
much customization. Learning to use the default configuration may be difficult
but it's probably worth the effort, for vim it has definitely been in my
experience.

~~~
znpy
I 120% agree.

I once lost my emacs dotfile and suddenly i was almost unable to use Emacs on
my own pc.

Since then, i've decided to apply as little customisation as possible to
software I use, and read the documentation and learn the standard
configuration instead.

For some reasons nowadays it's trendy to add colors, bells and whistles to
things but I've found out that learning the standard/default configuration
lets me be instantly capable of intervention on whatever machine i put my
hands on. Worst case scenario, i can tell programs to ignore configuration
files.

~~~
MaikuMori
That's why I have public dotfiles. Also this situation happens rarely. The
whole beauty of software is that you can customize it and make it your own.

The only downside I see with customization is that it can be huge timesink.

I guess to each his own, but I think you're really missing out.

P. S. Backups.

~~~
save_ferris
Regardless of the frequency of such a situation, it can really bite you when
something big breaks and you need to ssh into a vanilla server in the middle
of the night under pressure. Trying to fix a production fire under the gaze of
an angry CEO is stressful enough, but not being able to take care of business
on a box because of a missing personal config is truly one of the worst
experiences I've ever had in this business.

It's only happened to me once, but that one experience made me take a really
hard look at my workflow, and years later, I have no regrets about scaling
back my configs. I guess it really just depends on your job and on-
call/firefighting responsibilities.

~~~
myrandomcomment
Keep your dot files in github. Grab them. No internet, I bet you have them on
your laptop that you are ssh'd to the server from. SCP them over. Heck, cut
and paste them if needed. If things are so bad nether of these work you have
bigger issues and need to consider a design with some redundancy. Clos fabrics
and VM/containers and if you have to have bare metal then you should have more
then one (cluster or behind a SLB).

There is no excuse for a single point of failure in 2017. Not trying to sound
like an ass but....

You did say it was years ago, so what I said seems to be true for today, no
judgment on your situation years ago. My first major shit, got to fix it was
on Solaris 2.51 so that shows my age.

I bind bash and tmux to vim keymaps. If that is not on the box it is the first
thing I fix. I will be fast to resolve the issue if my fingers just work as I
have trained them :)

~~~
Lxr
This works fine if your configuration contains nothing too custom. Typically
after copying my dotfiles I have to edit them quite substantially to make them
actually work - mainly because they rely on outside sources (in .tmux.conf,
stuff like `set-option -g default-command "reattach-to-user-namespace -l
bash"`, in .vimrc it's usually colours, clipboard hacks etc that break). Maybe
I should have a second, trimmed down version with no outside dependencies.

~~~
pyre
That's why I break out things that are platform-dependent. E.g.:

    
    
       if-shell "[ $(uname) = 'Darwin' -a -f ~/.tmux.macos.conf ]" "source-file ~/.tmux.macos.conf"
    

Technically `reattach-to-user-namespace` is a macOS-specific hack to get tmux
working, so there's no need to jam it into your standard config.

------
git-pull
I am author of _The Tao of tmux_ , a book I just finished a few weeks ago. You
can read it publicly at [https://leanpub.com/the-tao-of-
tmux/read](https://leanpub.com/the-tao-of-tmux/read).

One of the things I stress in my book is that there is no "correct" way to use
tmux. The approach I take to teaching tmux is separating it into its objects:
The server, sessions, windows and panes. Then where they stand in relation to
one another, how configurations work, and then leave some examples of usage
available to the user so they can pick out work flows they may like.

I'm sort of against the approach of just throwing a config / toolkit at
someone a la oh-my-zsh and spf13-vim (even though I think they're ok after
you've learned to customize yourself). The gift of allowing someone to wrap
their brain around something will let the rest come to them.

~~~
whsheet
Out of curiosity: Why do you write a book and let it be read publicly if you
sell it as well? Why should somebody buy a book when all the content is
available for free? Is this done to create some awareness for the book and
thus to improve SEO (e.g. generating good backlinks, etc.). And later you
restrict the public available content once SEO backlink power is generated?

Our of curiosity 2: Are you happy with the sales, how many books could you
sell already (if you don't mind to tell us)? Would you write again a book
about a console-based app?

Out of curiosity 3: Are you happy with Leanpub?

Thanks!

------
CaptSpify
I basically use tmux as my window manager. I only typically open two programs:
a browser, and a terminal. Both are always in full-screen mode.

It makes things nice and simple. No matter if I'm working on a laptop or a
desktop, all my terminal windows end up in the same place and have the same
settings. It's great!

~~~
hyperhopper
If you are already going that far, why not just switch to a tiling window
manager?

~~~
colordrops
They are not mutually exclusive. I use xmonad, tmux, and vim together. Tmux
adds some niceties on top of xmonad like shared keybinds with vim making
moving across vim panes and terminals seamless. Also, I can jump on my macbook
and login to my desktop using mosh and re-attach to the tmux session and
continue working where I left off.

------
KaiserPro
So this may mark me out as different, but apart from persistent remote
connections, I've never really felt the need for screen/tmux.

I've been trying to figure out what the fuss is about, and I still really
don't understand (apart from persistence and screen sharing.)

When I want multiple terminals, I just fire up another terminal window and hey
presto, jobs a goodun. I have a fairly high res screen and small fonts, so I
can fit many terminals on one screen. If I run out, I hop over to a new
virtual screen.

Perhaps this is a by product of working in linux/unix so long? I can imagine
that if I was forced to use putty, where connection setup is costly and screen
space scarce, I'd want tmux to make things quicker.

What am I missing?

~~~
nehcsivart
The problem with opening multiple terminals is that you will eventually be
slowed down in your workflow. Say you want to switch to a particular terminal,
and you have more than 10 terminals running. You either have to press CTRL+Tab
a few times (5 times on average for 10 terminals), or switch using the mouse,
both of which are slow.

Another problem is working remotely. If you connect to a remote server, and
you need another terminal for that remote server, you must establish another
separate connection. With a terminal multiplexer, you do not need that.

~~~
gooseyard
I had this problem, although I found after I started using a tiling window
manager that I gravitated back from screen (which I used obsessively) to
workspaces with a fairly small (usually two, sometimes three) of xterms
running. I might have a total of six running, but they're on three workspaces,
and the workspaces are conceptually discreet.

Tab cycling through more than 3 terminals got to be very tedious, but it also
often got to be more difficult with the multiplexer to remember the absolute
ids of terminals (in cases where I was opening a bunch of them to debug a
problem) than it was to visually spot the one I wanted when I had them grouped
into workspaces whose ids have meaning to me.

The remote thing you mention definitely gets to be a nuisance; usually when
I'm debugging a remote machine, I open an xmonad workspace and on it a couple
of terminals. It feels chintzy to ssh twice, but it's also weird to go from
one set of terminal selection commands (xmonad's) to another (tmux's).

Anyway this probably sounds like I'm disagreeing, I totally agree but the
tiling WM's threw a wrench into my thinking about being able to absolutely
select specific terminals.

------
3JPLW
I use tmux constantly but I barely know three tmux commands. Get all the
benefit with none of the cognitive overhead with `tmux -CC` in a supported
terminal (like iTerm). It allows the GUI program to take charge of the
windowing and paneling in its own native way. It's lovely.

------
williamdclt
It still sounds like a very shallow (naive?) use of tmux to me, I'm in no way
a real power user of tmux and I use a lot more feature than that.

You could talk about binds without prefix, sessions, customized status bar,
pane swapping, bind keys to script launching, plugins, pane highlighting...

And the best feature of all for me: maximized pane toggle. I use that ALL the
time.

I never could find all features I needed from an emulator (plus, I want a
drop-down terminal), Tmux have them all and more and I'm not dependent of a
specific emulator.

~~~
hyperhopper
Do you have any better recommendations on what to read in this case? If you
do, I would love to read them! If not, you could do a great thing and use your
knowledge to help the rest of us out.

------
skratlo
Most people use tmux locally, because their terminal emulator doesn't do what
they need it to do, or the window manager doesn't do it. Using tmux locally
merely supplements the window manager. I stopped using tmux locally after
switching to i3 (and I believe any other tiling / proper wm would do). Now my
only use case for tmux is on the server where I'm running weechat. When
sysadmining, it's eaiser for me launch few terminals in my window manager and
ssh to the same server, then running tmux remotely. It's simply a question of
ergonomics, how many sets of keyboard shortcuts do you need to remember to
handle a multiplex... eh, window manager running inside a window manager?

------
arca_vorago
echo "alias tmux='screen'" >> ~/.bashrc && source ~/.bashrc

But in all seriousness, I do sometimes find myself using tmux, but I try hard
not to fragment myself over tools too much, especially with screen finally
having vert split, and tmux being BSD, this is one of those cases where I
think screen while it has some quirks and may have a bit of a learning curve
is worth the effort.

edit: Since I didn't provide much value and my comment was a bit snarky, here
is a truly useful tmux shortcuts and cheatsheet link:

[https://gist.github.com/mischapeters/462c6f383bfd9b2f9eea3fb...](https://gist.github.com/mischapeters/462c6f383bfd9b2f9eea3fb7e6e5b79d)

------
Morantron
You are not using tmux properly if you don't have tmux-fingers (
[https://github.com/morantron/tmux-fingers](https://github.com/morantron/tmux-
fingers) ) installed B)

</shameless-plug>

~~~
GrinningFool
Thanks for the shameless plug, I was getting ready to write something like
this myself.

~~~
Morantron
Awesome! I'll publish a roadmap on GitHub on pending things that can be
implemented, so maybe you can contribute :B

I wonder how I could improve the visibility of the plugin, since it's a little
bit difficult to find/explain. What kind of keywords where you using?

Thanks!

------
elpocko
There is one thing I want from my terminal multiplexer: working scroll
functionality with Shift+PgUp/PgDn, even in split screens, just like in a
plain console, without messing everything up. Is that possible?

~~~
rhinoceraptor
You do one better - tmux's mouse mode allows you to scroll with your mouse
wheel.

Just add 'set -g mouse on' to your ~/.tmux.conf. You can configure the history
size limit by setting 'set-option -g history-limit <lines>'. When you scroll
the mouse wheel, tmux will enter copy mode and scroll.

~~~
ninkendo
The problem is, my terminal already has a concept of scrolling, and its own
concept of a buffer, with its own concept of keeping unlimited contents based
on available memory, etc.

I've never seen a tmux setup that doesn't fail horribly when these two
concepts collide (I can't cmd-f search the buffer for interesting things, I
don't get a scroll bar on the right that shows me how far up the buffer I am,
scroll acceleration doesn't work, etc.)

Terminal.app already does split panes, tabs, scroll acceleration, search, etc.
I don't get persistence, but I don't really understand the workflow where
you'd want it. I typically open new tabs exactly because I want a fresh
workspace, and I close them exactly because I want to clear my workspace. I
can just manually call tmux when I know the thing I'm running should outlast a
window (and actually, 99% of the time that's on a server I'm SSH'd into, not
on my local machine.)

Can anyone convince me why I need to be all-tmux all-the-time?

~~~
coldtea
> _The problem is, my terminal already has a concept of scrolling, and its own
> concept of a buffer, with its own concept of keeping unlimited contents
> based on available memory, etc._

Perhaps try iTerm2 (for Mac) native tmux integration. You can have tmux panes
and split panes that are (and behave like) regular terminal panes and tabs.

~~~
pmoriarty
_" You can have tmux panes and split panes that are (and behave like) regular
terminal panes and tabs."_

What's the advantage of that exactly?

~~~
coldtea
Proper fucking scrolling?

Less terminal behavior that needs to be multiplexed by tmux?

More integration with other iTerm features (instead of tmux panes being
agnostic to them)?

------
mcbuilder
I used to be an extensive user of tmux, basically I'd have a sprawling set of
sessions, all backed up with the continuum and resurrection plugins. It
allowed me to armory seemlessly work remote on my main dev box or through SSH.
Of course the biggest pain was consolidating the X11 clipboard and tmux's
internal one.

Since I switched to Emacs though I've gradually peeled away the tmux layer. I
still run without X11 version of Emacs so I can remote in, but Emacs itself
does an equal or better job at pane management and session persistence,
especially using daemon mode. ANSI term inside Emacs, something that has
really improved in the past few years, has cinched the deal for me. Tmux had
become bloat at this point for me.

~~~
zls
Funny, I'm going the other way. I used ansi-term (well, multiterm) with a
daemon emacs for years, but in the end, a few things ended up being too much:

* some programs didn't render properly in ansi-term

* opening really large log files was dubious

* hanging a buffer by, say, accidentally running a long macro would wipe out all my terminal sessions and open files

* lingering suspicion that modal editing makes a lot of sense

These days I'm picking up tmux and kakoune and loving it. I guess everyone
likes to change it up now and then. :)

------
Zaheer
After experimenting with mosh, tmux, screen, etc I've finally found my ideal
ssh setup. At work I do development on my Mac and run builds on a cloud
desktop. My Setup:

1\. Unison for file-sync
([https://www.cis.upenn.edu/~bcpierce/unison/](https://www.cis.upenn.edu/~bcpierce/unison/))

2\. iTerm on Mac

3\. AutoSSH for more durable SSH sessions (ex. close laptop & reopen at home)
- [http://www.harding.motd.ca/autossh/](http://www.harding.motd.ca/autossh/)

4\. tmux -CC command to attach to tmux session with iTerm Tmux integration.
This is the killer feature, it treats a tmux session as a native iTerm window.
This means all of the 'learning' you previously had to do with Tmux is nearly
non-existent (copy / paste, switching tabs, etc). My biggest annoyance with
TMux was the copy / paste special handling. Huge productivity loss.

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

Instantly have a _fairly_ durable terminal session on my remote desktop.

~~~
schemathings
Ironically, you got me interested in trying out Unison, but the download links
are broken :(

------
trengrj
I'd argue that using a terminal multiplexer like tmux or screen on a
production server (as this article implied) often means you are doing things
wrong.

It is better to set up a service (systemd or other) and configure it to start
correctly rather than relying on a tmux session to manage long running
processes. By going this route you get logging, auto restarting, starting on
boot, monitoring etc for free.

~~~
njharman
There's difference to daemons and Kong running process. Long process might be
tar up all these files, or copy all these. Things that takes 10 s of minutes
to hours. Things you don't want to bomb out just cause network died. Things
that are one off and variable enough it doesn't make sense to write a server
for them.

And of course any you don't want to be interiors part way. Like upgrade or
deployment

~~~
enobrev
I just now went through this, which makes this post perfectly apt for my
morning. 4-day running process. SSH disconnects on day 3.5.

Reconnected; Started tmux; Started the process; 4 days to go...

I generally know better. Some lessons need to be relearned.

------
adventist
I love Tmux, I just can't seem to wrap my head on how to COPY and PASTE
something lol. Anyone have any step, by step tutorial for how to copy and
paste like VIM? I would like to be able to see what is being copied just like
in VIM's visual mode. Like If I can copy some code in VIM and then go to paste
in another REPL pane in TMUX then that would be my ideal goal!

~~~
merlincorey
By default it's:

    
    
        C-b [   Start copy mode
    

C stands for CTRL, by the way.

This allows you to scroll up in the window and place the cursor where you want
to start copying, then:

    
    
        C-SPACE Set mark
    

This sets the mark for copying, then you can move around and what will be
copied will be hilighted with a background color.

    
    
        M-w     Copy selection
    

This copies the selection. Incidentally, M stands for "meta" and is nearly
always usable with ESC, but often ALT will work in many terminals (some need a
setting to enable it).

To paste, go to the buffer you want to paste into and then:

    
    
        C-b ]    Paste!

------
philippeback
In my config, some better mouse support, 256color, history saving, config
reload, and emoji-weather (temp + icon) in the status bar.

[https://github.com/philippeback/dotfiles/blob/master/.tmux.c...](https://github.com/philippeback/dotfiles/blob/master/.tmux.conf)

Emoji-weather is to be taken from [https://github.com/justincampbell/emoji-
weather](https://github.com/justincampbell/emoji-weather)

Got this from Justin Campbell on LiveCodingTV. He has pretty good dotfiles
[https://github.com/justincampbell/.dotfiles](https://github.com/justincampbell/.dotfiles)

His Vim one pretty much takes the cake:
[https://github.com/justincampbell/.dotfiles/blob/master/.vim...](https://github.com/justincampbell/.dotfiles/blob/master/.vimrc)

and this tmux-pomodoro is sweet. [https://github.com/justincampbell/tmux-
pomodoro](https://github.com/justincampbell/tmux-pomodoro)

definitely more stuff than in mine
[https://github.com/philippeback/dotfiles/blob/master/.vimrc](https://github.com/philippeback/dotfiles/blob/master/.vimrc)

------
babygetoboy
The big problem I always have with tmux is that is messes up all of the colors
in my Vim.

~~~
colordrops
This is easily fixed by setting the appropriate terminal color settings in
your bashrc. A quick google search will provide the answer.

------
blfr
_I use ... Screen on production servers. ... Screen has the lightest
footprint_

dtach, which is what I use on production servers even though it usually
doesn't come standard, probably has the lightest footprint if you just want to
return to some screen later. It doesn't maintain history, however, the screen
is redrawn, if possible, on reattach.

Otherwise, yes. Both tmux and the book are great (if you don't mind getting
drawn into wasting time on customizing the status bar and such).

------
jph
Tmux dot files with annotations-- may be helpful for people here.

[https://news.ycombinator.com/item?id=13570010](https://news.ycombinator.com/item?id=13570010)

Direct link to the tmux dot files repo on GitHub:

[https://github.com/SixArm/sixarm_tmux_dotfiles](https://github.com/SixArm/sixarm_tmux_dotfiles)

------
rhinoceraptor
I have my tmux/vim set up so that C-(h,j,k,l) navigates vim splits and tmux
panes seamlessly. The only downside was that I had to rebind C-l to C-o to
clear my shell. But it makes it a lot more productive when using multiple
shell panes along with my vim session in one terminal window.

------
dllthomas
A big thing I've done is use my multiplexer segregate contexts. When I spin up
screen (tmux would work the same, just never switched) I set an environment
variable noting the particular context I'm working in, and I adjust my PATH,
HISTFILE, and more based on this variable.

~~~
greyskull
I create new sessions and name them appropriately. My one wish is to be able
to do "PREFIX ls" and then just hit enter on the session to switch to. I may
be missing something, but I always have to do "PREFIX a -t [session name]".

~~~
jamestomasino
You don't use PREFIX s ? Just select by name and hit enter.

------
owenww
Great read - prompted me to actually learn tmux, has been worth it! Also
thanks for the link to the book, I ended up buying
[https://pragprog.com/book/bhtmux2/tmux-2](https://pragprog.com/book/bhtmux2/tmux-2)

------
autoreleasepool
What's the advantage of tmux over something like terminator with pane
splitting?

~~~
paulddraper
I can SSH to a machine and connect to a tmux session.

Tmux's competition is not terminator, but screen.

~~~
dozzie
screen + dvtm, as I hear tmux has sensible way to manage split windows, while
screen's way is atrocious.

~~~
paulddraper
Hm, true.

------
oconnor663
My favorite tip: add bindings with the control key. So if you use "Ctrl-b n"
to go to the next pane, add a binding for "Ctrl-b Ctrl-n". It's easier to keep
the Ctrl key pressed than to lift it.

~~~
rocqua
Here I've been letting go and re-pressing the Ctrl key. Thanks for the tip :D

------
gbrown_
So "properly" means customizing to user preference?

~~~
a3n
If that means in contrast to the default configuration, then probably yes.
tmux default is not bad, but each person would probably do better with a few
common adjustments, and maybe a bit more for their local needs and focus.

------
pkaye
Does mouse copy and paste work correctly with tmux? I find that it selects
spans across multiple panes. Is there a way to fix this or Windows or Linux?

~~~
chris_st
I use the "PREFIX z" command to "maximize" the current pane, that is, it hides
all the other panes and makes the current one the only visible pane, filling
the whole terminal. Then I copy, and "PREFIX z" again to go back.

~~~
bostand
Clever! You just solved my main issue with tmux!

------
ausjke
I used the default all the time and it worked fine for me.

by the way what is PREFIX meaning? ctrl-b?

~~~
teach
Yes, CTRL-b is the default prefix. The default prefix in screen is CTRL-a, so
a LOT of tmux users remap the prefix to match.

Also, CTRL-a is much easier to press, since B is a long way away.

tmux only uses CTRL-b so it won't conflict with screen's shortcut in case you
find yourself in a screen session inside a tmux session.

~~~
base698
I never understood that since ctrl+a jumps to beginning of line. Ctrl-o is
what I use, but probably in the minority.

------
vlunkr
My biggest question is why are they still using screen when sshing?

------
drieddust
bind | split-window -h bind - split-window -v

Binding vertical bar to horizontal and visa versa seems weird. Shouldn't it be
otherway around.

------
arc_of_descent
Wow! PREFIX t displays the time!

------
gcb0
> uses gnu screen on all servers.

> uses tmux on dev box

> advocates tmux but do not mention a single feature not on gnu screen.

why on earth would someone be that impractical?! just get a single gnu
screenrc file that does all that and call it a day on every single environment
you work on.

------
vegabook
Honestly, tmux is not hard to use. It has some contorted key bindings, sure,
but its benefits are so immediately huge that the motivation to learn its
quirks is huge too. And, unlike say, vim, it only has 3 things you really need
to know: panes, windows, detach. Most people who use tmux, even casually, will
inevitably be using it 'properly', within a week, and fluidly within a month.
So this blog post is quite redundant.

------
zump
can someone eli5 how to copy to the clipboard using this thing without
spending 100 calories in my upper torso

