6

I am using Windows Terminal(Preview) with shells like 1.PowerShell, 2.WSL in my Windows 10 machine. I installed the latest version of oh-my-posh and posh-git to customize the terminal. My current theme is Agnoster which gives a colorful custom prompt. But I want to get rid of the "username@host" from my prompt.

Eg:

Current => username@host D:\folder-name>

Needed => D:\folder-name>

I tried few things with $GitPromptSettings variable and also in GitPrompt.ps1 file which is inside the posh-git folder but with no use.

Also, since i have oh-my-posh and posh-git, does both have prompt customization properties or it is only from posh-git?

Any help is appreciated.

Saravana Kumar M
  • 460
  • 9
  • 19

4 Answers4

8

remove the segment which type is session from ~\Documents\WindowsPowerShell\Modules\oh-my-posh\3.101.23\themes\<themename>.omp.json

cocoa2135
  • 81
  • 1
  • 1
7

You have to set $DefaultUser before importing modules.

Example:

$global:DefaultUser = [System.Environment]::UserName
Import-Module posh-git 
Import-Module oh-my-posh
Set-Theme Paradox
Jigar
  • 544
  • 1
  • 8
  • 28
  • 1
    OMG. Awesome. Thanks. It worked. But can you please explain what we are doing and how it is working? Because, i was thinking that, i should do something on the ```GitPrompt``` variable which is returned by posh.git – Saravana Kumar M Mar 09 '20 at 04:47
  • This is no longer working in Oh My Posh 3, see the answer by @cocoa2135 below: https://stackoverflow.com/a/66740114/928483 – w5l Apr 15 '21 at 06:27
6

In Oh My Posh 3, the method for hiding the username@host part has changed because the whole theme configuration has changed.

The username@host portion of the powerline comes from the "session" segment. The configuration options for session can be found here: https://ohmyposh.dev/docs/session

Interesting options are:

  • display_user: boolean - display the user name or not - defaults to true
  • display_host: boolean - display the host name or not - defaults to true
  • default_user_name: string - name of the default user - defaults to empty
  • display_default: boolean - display the segment or not when the user matches default_user_name - defaults to true

And the note on environment variables:

POSH_SESSION_DEFAULT_USER - used to override the hardcoded default_user_name property

Solution

Based on this, I made the following changes:

  1. For the theme, I added the display_default: false property to the "session" segment:
    {
      "type": "session",
      // ....
      "properties": {
        "display_default": false
      }
    }```
    
  2. In my powershell profile, I set the new environment variable to my own username: $env:POSH_SESSION_DEFAULT_USER = [System.Environment]::UserName

This makes the user@host part of the prompt disappear as long as I'm using my default username.

w5l
  • 5,341
  • 1
  • 25
  • 43
  • 1
    It's not clear from your answer wether you need to do 1. and 2. or just either one of them. The default location of the theme files could also be mentioned. – Good Night Nerd Pride Sep 18 '21 at 15:20
2

You may check {theme's name}.psm1 in the Themes/ directory to understand how it works. The key is the following code.

e.g. Paradox.psm1

$user = $sl.CurrentUser
....
if (Test-NotDefaultUser($user)) {

        $prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor

}

if $DefaultUser is same to $user or is different $null, your powershell does not show $user@$computer.

  • Thanks. Actually I was going through the ```Agnoster.psm1``` file to understand the logic. Ii will be easy if i could debug the file. Seems there is a way to debug. Let me check. Thanks Again. – Saravana Kumar M Mar 10 '20 at 05:49