Reporting Username and Hostname for Custom Badge and Automatic Profile Switching

19 views
Skip to first unread message

DanF

unread,
May 14, 2026, 5:44:07 PM (10 days ago) May 14
to iterm2-discuss
I am working in an HPC environment where I am very frequently SSH'ing and SU'ing between servers.  Many of these systems have inconsistent prompt formatting etc.  I notice that it is possible "report username and profile" from a trigger, and I can also set user owned variables with escape codes. Is there any way to directly set/report username/hostname other than the trigger mechanism?  

For example, something I'd like to do, if I have my Badge set to "username@hostname" rather than detect these from a regex trigger, is when I issue an SSH command

ssh foo@bar

have in my ~/.ssh/config file something like

host bar
  hostname hpc.bar.org
  permitlocalcommand yes
  localcommand 'iterm2_report_username %s; iterm2_report_hostname %r'

or equivalently using an escape sequence.  Does anything like this exist?  Is it possible to trigger triggers without regex matching?

George Nachman

unread,
May 14, 2026, 5:56:13 PM (10 days ago) May 14
to iterm2-...@googlegroups.com
Yes, there are a couple of direct paths that don't require triggers.

Option 1: OSC 1337 escape codes

iTerm2 understands a proprietary escape sequence for this:

ESC ] 1337 ; RemoteHost=user@host BEL
ESC ] 1337 ; CurrentDir=/some/path BEL

These set the same user.username / user.hostname / session.path variables that the "Report Username and Hostname" trigger sets, so your Badge will pick them up automatically. They also drop a host mark, which feeds the Hosts toolbelt, per-host command history, semantic history, and so on.

The username and host are joined with @, and parsing scans backward for the last @ (so Windows UPN-style usernames with embedded @ work). You can also send just host with no @, or user@ with no host, to update only one side.

For your ~/.ssh/config example, you'd want something like this run on the remote after login:

printf '\033]1337;RemoteHost=%s@%s\007' "$USER" "$(hostname)"
printf '\033]1337;CurrentDir=%s\007' "$PWD"

LocalCommand runs on the local side before the SSH session starts, so it can't see the remote's real $USER/hostname, but you can hardcode values there or use a wrapper script around ssh that emits the sequence before invoking ssh.

Option 2: iTerm2's SSH integration

If Python 3.7+ is available on the remote hosts, iTerm2 can inject shell integration over the SSH session without touching any dotfiles or shared environment. You get RemoteHost/CurrentDir updates on every prompt (plus command marks, history, file transfer, etc.) automatically, on every hop, including after su/sudo transitions if you set it up that way. Since you're frequently bouncing between machines with inconsistent setups, this avoids having to retrofit anything per-host.


On "triggering triggers without regex": there isn't a mechanism for that. Triggers always fire from a regex match on terminal output. But the two options above give you the direct, non-regex path you're looking for.


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/iterm2-discuss/866c2d97-079a-42b2-ad66-711699dba4ffn%40googlegroups.com.

Daniel Fulton

unread,
May 15, 2026, 4:35:38 PM (9 days ago) May 15
to iterm2-...@googlegroups.com
Perfect, this is really helpful.  LocalCommands passed via SSH config can take advantage of SSH token variables, which include remote user and remote host, so those can be fed into the printf statement for cases where regex matching is a little more dicey.  I'm curious to explore SSH integration as well.

Cheers!

You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/t8UUbB9j8E8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/iterm2-discuss/CAB5RqomW%3DS17O%2BKjGnBt8sBjpKomHWtZgY5%2BLf2919QPaXzPnA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages