2

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!

  • 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 '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 9 '17 at 15:57
  • The iterm trigger feature: iterm2.com/documentation-triggers.html – jesse_b Aug 9 '17 at 15:59
  • 1
    You can also do this within the shell. Example. – Gilles 'SO- stop being evil' Aug 9 '17 at 22:55
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 it 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+ (?!\~).*\]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.