3

I'm trying to use iTerm2 profile triggers on my mac to change the prompt if I leave my home directory. This rule should only apply when the node I'm on has a hostname of headnode. The prompt will also include the machine's location in parenthesis after the hostname. I'm trying to build a regex that will match any directory unless it includes ~.

So it should match:

[jbutryn@headnode (us-west-a) /]$

but not:

[jbutryn@headnode (us-west-a) ~]$
or
[jbutryn@headnode (us-west-a) ~/tmp]$
etc.

Currently I'm building my regex like this:

\[.*\@headnode.*(?!\~).*].

But that matches all examples above. Does anyone know what I'm doing wrong? Thanks!

jesse_b
  • 35,934
  • 12
  • 91
  • 140
  • Unless you bring your prompt with you to any other host which you might log into, hostname detection won't work, as when you log into a new host, you get a new environment including a freshly-set `PS1`. – DopeGhoti Aug 09 '17 at 15:56
  • This is a feature of iterm2, it can literally just match regex patterns and take action based off them on your local machine. Maybe I'm using the word prompt incorrectly but essentially I just want iTerm to highlight it red when I'm not in the home directory. The highlight function definitely works I just can't get the match to work correctly. – jesse_b Aug 09 '17 at 15:57
  • The iterm trigger feature: https://www.iterm2.com/documentation-triggers.html – jesse_b Aug 09 '17 at 15:59
  • 1
    You can also do this within the shell. [Example](https://unix.stackexchange.com/questions/217270/change-ps1-color-when-connected-to-other-host-via-ssh/217275#217275). – Gilles 'SO- stop being evil' Aug 09 '17 at 22:55

2 Answers2

4
\[.*\@headnode.*(?!\~).*\]

Matches on

  1. [ followed by
  2. .*: a sequence of 0 or more characters followed by
  3. @headnode followed by
  4. .*: a sequence of 0 or more characters that is not followed by
  5. ~, followed by
  6. .*: a sequence of 0 or more characters followed by:
  7. ]

It does match on:

[jbutryn@headnode (us-west-a) ~/tmp]$

Because that's:

<[><jbutryn><@headnode>< (us-west-a) ~/tmp><><]>$
 1  2        3          4                  6  7

What follows 4 is not ~.

You'd need the negative look ahead to be at a position that matches something specific in the text, like after the first word after @headnode:

\[.*\@headnode \S+ (?!\~).*\]
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
0

Working off Stéphane's breakdown, I suggest a slightly different solution.  Since the signal to stop including stuff in his item 4 is only a single character wide, it's easy to construct a class that includes all characters EXCEPT the signal characters – or, in this case, single character.

In other words, replace the portion of the regular expression in his #4 with just this:

[^~]* 
  • 2
    Thank you for acknowledging that your answer is building on somebody else’s answer.  However, your answer would be better if you gave a complete, standalone solution. (For example, consider how useless your answer would become if Stéphane’s answer disappeared.) In particular, I believe that you would need to replace parts 4 through 6. – G-Man Says 'Reinstate Monica' Jan 19 '22 at 00:16