none
How to make the path shorter in the prompt

    Question

  • I find the powershell prompt is too long if it displays the whole filepath, which can stretch across most of the screen.

    So I've edited my profile to

    function prompt
    {
    "$pwd
    PS>"
    }

    so that it inserts a newline after the filepath.

    But I don't really need to know the whole filepath every time I do something. Is there a way to just display the current folder name, or (even better) just the current folder and its parent? So if my path is C:\abc\def\ghi\klm, just display ghi\klm.

    I have all the POSIX commands available since I have Cygwin installed, and I've directed my classpath to Cygwin's bin folder, if that's any help (sed etc)...

    Tuesday, April 05, 2011 2:58 AM

Answers

  • It's been a while since I modified my PS prompt, but this thread has
    given me some interesting ideas. I'm now using
     
    function prompt {
      $host.ui.rawui.WindowTitle = "PS $pwd"
      $p = 'PS ' + ($pwd -split '\\')[0]+' '+$(($pwd -split '\\')[-1] -join '\') + '> '
      return $p
    }
     
     

    Tuesday, April 05, 2011 11:43 AM
  • Hi Luigi,

    For last two part use:

    Function Prompt
    {
     $tmp = $(Get-Location).Path.Split("\")
     $myPrompt = "PS: " + $tmp[($tmp.count-2)] + "\" + $tmp[($tmp.count-1)] + ">"
     Write-Host ($myPrompt) -NoNewLine
     Return " "
    }


    Or for last X chars use:

    function prompt
    {
     $m = 30 # maximum prompt length
     $myPath = $(Get-Location).Path
     if ($myPath.length -ge $m)
     {
     $myPath = "..." + $myPath.substring($myPath.length - $m + 4) + ">"
     } 
     Write-Host ($myPath) -NoNewLine
     Return " "
    }
    Tuesday, April 05, 2011 7:55 AM
  • $pwd.path -split '\\',2
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Tuesday, April 05, 2011 10:21 AM
    Moderator

All replies

  • Hi Luigi,

    For last two part use:

    Function Prompt
    {
     $tmp = $(Get-Location).Path.Split("\")
     $myPrompt = "PS: " + $tmp[($tmp.count-2)] + "\" + $tmp[($tmp.count-1)] + ">"
     Write-Host ($myPrompt) -NoNewLine
     Return " "
    }


    Or for last X chars use:

    function prompt
    {
     $m = 30 # maximum prompt length
     $myPath = $(Get-Location).Path
     if ($myPath.length -ge $m)
     {
     $myPath = "..." + $myPath.substring($myPath.length - $m + 4) + ">"
     } 
     Write-Host ($myPath) -NoNewLine
     Return " "
    }
    Tuesday, April 05, 2011 7:55 AM
  • $pwd.path -split '\\',2
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Tuesday, April 05, 2011 10:21 AM
    Moderator
  • It's been a while since I modified my PS prompt, but this thread has
    given me some interesting ideas. I'm now using
     
    function prompt {
      $host.ui.rawui.WindowTitle = "PS $pwd"
      $p = 'PS ' + ($pwd -split '\\')[0]+' '+$(($pwd -split '\\')[-1] -join '\') + '> '
      return $p
    }
     
     

    Tuesday, April 05, 2011 11:43 AM
  • I added one more line to get the full path in the window title
     
    function prompt {
      $host.ui.rawui.WindowTitle = "PS $pwd"
      $p = 'PS ' + ($pwd -split '\\')[0]+' '+$(($pwd -split '\\')[-1] -join '\') + '> '
      return $p
    }
     
     

    Tuesday, April 05, 2011 11:49 AM