Hacker News new | ask | show | jobs
by nrclark 2308 days ago
Not sure whether or not this is a little-known feature. iTerm2 has _fantastic_ integration with tmux (software used for persisting a terminal session across multiple logins).

Tmux can be a pretty complex piece of software, but iTerm can basically wrap it all up into a nice package. You don't need to know anything at all about tmux to use iTerm's tmux integration.

If you're SSHed into a server that has tmux installed, try running `tmux -CC` on the server. It'll pop up a new window that looks and feels just like it's running natively (complete with tab support), except it's all tunneled over SSH.

And if you disconnect, you can just reconnect later and your windows will all come back in the same state as when you left them.

I use this one-liner to SSH into a server and reconnect (or start) a tmux session:

    ssh -XY -F user@hostname -Ct \
     'sh -l -c "exec tmux -CC -u new-session -AD -s remote"'
iTerm2's tmux integration makes SSH access to remote machines feel almost as native as using your machine locally. It's really a killer feature, and it made iTerm2 worth a donation for me.
9 comments

I haven’t really figured out Tmux, and I’m a little ashamed to admit it. I usually reach for screen when needed, and disconnect/reconnect/kill are the only commands of screen I know.

I’ll give the tmux -CC a go when I’m back at my machine, but what else does the integration do? I’m a heavy pane user - does the default split pane in tmux mode use tmux splitting for example?

Yes, splitting panes in iTerm will crate tmux panes on the remote. It's a pretty neat feature.
I mean, it uses it, but it no longer feels like tmux, and you don’t have nice tmux features like zoom-pane, right? iTerm2 is absolutely fantastic and I would miss it if I moved to a linux laptop, but I still haven’t seen the attraction of the tmux integration yet!
Very true that you don't really have access to zoom-pane type features. But they're also not really applicable. The integration is more about giving you a native "tabs and GUI windows" kind of feel, so that you can have multiple terminals open without needing to mess around with panes, tmux windows, etc. And of course, that gets provided in a persistent way (so that you can disconnect and reconnect at will) just like a normal tmux experience.
iTerm2 panes can be zoomed in.
As in maximise to take up the entire window? cmd+shift+enter to toggle, I love this feature.
Even better, it also maintains multiple windows!
I use iTerm2 with tmux every day (but I don’t ssh into machines often nowadays) and I feel oppositely — I don’t get the attraction of the tmux integration. But maybe I still haven’t understood it, so please feel free to tell me what I’m missing. For me, the purpose of using tmux on my laptop is session, window, and pane management. (Perhaps my favorite tmux feature is zoom-pane.) So I find that the iTerm2 tmux integration takes away everything I liked about tmux, and makes using tmux feel like using iTerm2 without tmux!
I love tmux just for letting me run long running sessions on remote machines. I don't like tmux's window management, I want to use my terminal, scroll back with mouse, that kind of thing. Tmux integration is therefore for those of us who love their GUI terminal :)
You can always `set -g mouse on` in your `tmux.conf`. But that has the nasty downside of stopping you from being able to right-click and copy text to your OS clipboard. It does let you pane select and scroll with a mouse though.
For someone that doesn't like or understand working with tmux windows and panes, it brings the benefit of running sessions in remote servers, being able to disconnect while still using their GUI.
Out of curiosity, why do you use iTerm2? As a heavy tmux user, I made the switch back to Terminal recently and never looked back.
- visor mode: I always have a fullscreen terminal one keypress away

- genuine fullscreen mode, not the crazy Apple thing where you try to fullscreen an app and it creates another "workspace" or "desktop" or something and you can no longer switch efficiently between fullscreened and non-fullscreened apps without seeing some sliding animation.

- Powerful keybinding support for simple and sophisticated cases. E.g. I use the "send raw bytes" feature so that I can switch panes in tmux with a single keypress, without needing to explicitly send the tmux prefix key. And all that sort of configuration can be version controlled in JSON (you certainly wouldn't want to get something like that working and then commit it to an app config dialog for safe keeping)

- settings for everything

- "dynamic profiles" You can version control your config as JSON

- configurable click behavior ("smart selection"). See other subthreads in this discussion.

- color themes and convenient color switching

Probably others.

With respect, I believe you're making a mistake. Check out some of the things I mentioned. (What does Terminal.app give you over iTerm2?)

> visor mode: I always have a fullscreen terminal one keypress away

Last time I tried this (which, admittedly was sometime back), I found that this was ugly as heck if one has multiple screens and them set to be above one another-ish (e.g. on laptop with desktop screen(s) offset above) - it turns out that iTerm was just sliding the window in from 'offscreen', which if one has windows as I do, means it appears on an upper window.

It's slick if your screens are in 'acceptable' positions for iTerm - otherwise it's an ugly mess. Which is a shame, because I quite like / want this feature.

(Not that Terminal offers this at all mind)

(Replying to sibling due to HN constraints)

@jimsmart Yes I can believe that might take a bit of tweaking to get right. The author (George Nachman) is very responsive though, so I'm sure he'd appreciate the report. It might be reported/fixed/discussed already?

https://gitlab.com/gnachman/iterm2/issues/8536

> What does Terminal.app give you over iTerm2?

Ease. I've been down many a custom-config-rabbit-hole but these days I find greater pleasure in paths of least resistance. I'll admit, however, that I love my hyper key and BetterTouchTool gestures.

I don't use tmux locally, but I find iTerm2+Tmux invaluable when it comes to managing my servers. for each of them I have an ssh command that automatically opens all the windows and panes for that particular server. It even persists colors I give various panes so I don't accidentally work in the wrong one.

Saves a bunch of time!

I have a function that I've aliased `ssh` to that does a similar thing, so my default is to have tmux running on the remote.

It checks if the remote has tmux first though:

    function ssht() {
      /usr/bin/ssh -t "$@" "which tmux 2>&1 > /dev/null && tmux -u -CC new -A -s default"
    }
    alias ssh=ssht
I really wish some Linux-based terminal emulator would copy this feature.
I'm not too sure if it has this built-in by default, but I've found kitty terminal to be the nearest thing to iterm for Linux. Lots of customizability and it has fairly good docs which is always a plus.
after years of seeing tmux fly by, and still not understanding its purpose I thought okay, lets give this one liner a try and see if I really don't need to know about tmux as you wrote. :)

why do I get: Can't open user config file <user@host-redacted>: No such file or directory

Sorry about that! Take off the -F.

I grabbed it out of a bigger script that also generates and uses an SSH config file. Looks like I pulled out the actual file from args, but not the -F.

Remove the `-F` flag right in front of that, works for me after that.
Correction for those playing along at home: remove the -F from above. That was a cut/paste error on my part. Corrected version should be:

    ssh user@hostname -Ct 'sh -l -c "exec tmux -CC -u new-session -AD -s remote"'
More of a normal tmux question, but is there a way to input the window number, or is there a fast way to open up ssh to 4 servers numbered sequentially?

pane 1: ssh <server>-1

pane 2: ssh <server>-2

pane 3: ssh <server>-3

pane 4: ssh <server>-4

When I need to do cluster ssh with tmux I do this or a variant of it:

  function cluster_ssh() { 
    # Start a pane for every server
    for server in $*; do
      tmux split-window ssh $server
    done;

    # Same input to all panes
    tmux setw synchronize-panes on;

    # Init pane to background, tile layout
    tmux break-pane -s 0 -d
    tmux select-layout tiled
  }

  bash# cluster_ssh server-{01..10}.example.com


EDIT: made it a bit more complete, but you only need split and sync panes IMHO.
So what exactly is the iTerm feature here? You are just describing regular tmux.
it pulls out windows into terminal tabs (in the UI), and handles panes like they were native iTerm panes (so same keybindings etc). Makes for a native-feeling experience while still getting the benefits of tmux.
So when Volkswagen releases a new car you say "what is new here? You are just describing regular Cugnot's steam wagon?"
iTerm2 will use tmux's scripting mode to give you a "tmux without tmux" kind of experience. Scrollback will work normally, and so will opening new terminal tabs and new windows.

It doesn't give you anything if you're already comfortable with tmux. But if you want your experience to "feel" more native, it's an awesome tool.

great tip thanks for sharing