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!

4
  • 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 9, 2017 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 9, 2017 at 15:57
  • The iterm trigger feature: iterm2.com/documentation-triggers.html
    – jesse_b
    Aug 9, 2017 at 15:59
  • 1
    You can also do this within the shell. Example. Aug 9, 2017 at 22:55

2 Answers 2

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+ (?!\~).*\]
1
  • I don't understand the function of the \S+ here... is it whitespace?
    – Seamus
    Feb 25 at 23:06
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:

[^~]* 
1
  • 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. Jan 19, 2022 at 0:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .