Help in understanding how to setup profile triggering

510 views
Skip to first unread message

Vladimir S

unread,
Apr 7, 2017, 4:52:57 AM4/7/17
to iterm2-discuss
Hi,

I've spent the last 4 hours trying to understand the "Triggers" section of the Shell Integration page and I need help. 

The example talks about turning
geo...@example.com:/home/george%
into a regular expression like this (which I assume goes in the Triggers windows under "Regular Expressions"
^(\w+)@([\w.]+):.+%
Then put these parameters in the  "Parameters"
\1@\2

What condition triggers the trigger?

Maybe it will be clearer if we take my case as an example. I have a shell like this:
[08:45:32][user@landscape:nodeName ~] $

I'd like to trigger a different profile when I switch to root:
[08:45:32][root@landscape:nodeName ~] $

Where from the above example can I specify that I want only root@ to trigger the profile switch?

I already tried putting only "root@" in the parameters section and I've set the action to "Report User & Host" but nothing happened.

I've completed the shell integration setup only on iterm2 for my local machine.

What am I missing?

Regards,
Vladimir


Tom Feist-Hassan

unread,
Apr 7, 2017, 9:46:14 AM4/7/17
to iterm2-...@googlegroups.com
Vladimir,

A trigger in your default profile(s) something like:

'^\[[0-9:]+\]\[(root)@(\w+):' with action "report user & host", parameter: "\1@\2"

should match what you want. You may need to set the 'instant' flag as well since the prompt won't have a newline.

(You could simplify it further, to '^\[[0-9:]+\]\[root@' and param 'root@' I think, but untested)

Then in the profile you want to switch to as root, in 'profiles > advanced > automatic profile switching, add a rule of 'root@'.

I tested briefly with `echo '[08:45:32][root@landscape:nodeName ~] $' && sleep 5' and it seems to be working ok, including switching profiles back again when my local shell-integration picks up my actual prompt and resets to my normal user.

I think if you can install shell integration on the remote hosts/users as well, you wouldn't need the triggers at all, just the auto-switching rule, but that maybe isn't practical if you have a lot of machines or don't control their user configs.


hope that helps,

--Tom

Vladimir S

unread,
Apr 7, 2017, 11:32:40 AM4/7/17
to iterm2-discuss
Hi Tom,

Even your clear guide didn't work so I'll go step by step explaining what I did because it seems I'm missing something trivial:

1. Create a new profile with contrasting colors to the default profile
2. In new profile, edit the triggers window with
Regular expression: ^\[[0-9:]+\]\[(root)@(\w+):
Report User & Host
Parameters: root@
Instant: ticked
3. Edit Automatic profile switching by adding root@ entry.
4. Close window and restart iTerm2
5. Do: echo '[08:45:32][root@landscape:nodeName ~] $' && sleep 5 . No change in profile
6. Login to remote host, do sudo -i so that the shell changes to contain "root". No change in profile
7. Bang head on keyboard

My work involves logging into different machines including production machines where I shouldn't install any software so shell integration on remote hosts isn't an option. What am I missing here?

Regards,
Vladimir

Tom Feist-Hassan

unread,
Apr 7, 2017, 11:53:39 AM4/7/17
to iterm2-...@googlegroups.com
I'm not sure, but it sounds like it might be that you're defining the trigger in the profile you want to switch _to_, rather than _from_. But the 'auto profile switch' rule goes into the profile to switch _to_.

So whatever default profile you use when opening a new session, ensure the trigger is defined there. You could test that part is working by changing the trigger action to 'highlight' (which will just mark the whole string that matches) or 'send notification' with param: "matched \1 @ \2" or something).

If that works, you can set it back to 'set user & host', and in the same profile, set the profile badge to something like '\(session.username)@\(session.hostname)'. That should work as expected on localhost (where you have shell integration installed), and the 'echo ... && sleep' line from before should make it briefly change to 'root@whatever'.

That's all still in your default profile.

If that all works as expected, in your special root-only profile check the auto-switch rule is 'root@', and see if it gets activated then.

Thinking about it some more, you probably want a trigger more like

'^\[[0-9:]+\]\[(\w+?)@(\w+):' / set user & host / param: '\1@\2'

so it matches & updates your non-root prompt on the remote host as well, although the impact of not doing that would be having your root profile stay active for too long, rather than too little.

--T

Vladimir S

unread,
Apr 7, 2017, 12:11:46 PM4/7/17
to iterm2-discuss
You're exactly right about me defining it in the wrong place. I automatically assumed everything would go in one place so I didn't pay attention to the to/from specifics.

Unfortunately I won't be able to try it until Monday but I will post an update then. Thanks for the help and have a nice weekend!

Regards,
Vladimir

Vladimir S

unread,
Apr 10, 2017, 6:37:50 AM4/10/17
to iterm2-discuss
Hi Tom,

I set the configuration correctly and everything works as it should. Thanks for the help.

Regards,
Vladimir
Reply all
Reply to author
Forward
0 new messages