My PowerShell Prompt

Link. August 14, 2008. Comments [5]. Posted in: PowerShell

Neil Houghton asked a couple of days ago if I could share my PowerShell prompt() function. Here's what my prompt looks like:

ps-prompt

I tried to keep my prompt relatively short while still on a single line. There are two things I care about in my prompt: The machine name I'm working on (useful when I have VMs opened) and the current path, in abbreviated form.

Thus, my prompt() function looks like this:

function prompt {
   # our theme
   $cdelim = [ConsoleColor]::DarkCyan
   $chost = [ConsoleColor]::Green
   $cloc = [ConsoleColor]::Cyan

   write-host "$([char]0x0A7) " -n -f $cloc
   write-host ([net.dns]::GetHostName()) -n -f $chost
   write-host ' {' -n -f $cdelim
   write-host (shorten-path (pwd).Path) -n -f $cloc
   write-host '}' -n -f $cdelim
   return ' '
}

The abbreviation of the current directory is partially inspired by Unix (use ~ if it's under the $HOME) and partially by how GVim shortens paths for its tab captions:

vim-tabs

Here's the function that takes care of this:

function shorten-path([string] $path) {
   $loc = $path.Replace($HOME, '~')
   # remove prefix for UNC paths
   $loc = $loc -replace '^[^:]+::', ''
   # make path shorter like tabs in Vim,
   # handle paths starting with \\ and . correctly
   return ($loc -replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2')
}

One thing to keep in mind regarding shorten-path: I do a very simple replace of $HOME by ~. The reason it works correctly is that I ensure in my profile script that the $HOME variable has a fully qualified path and "completed" using the resolve-path command. I also modify what the home directory is under PowerShell by using a trick I've described previously.



Thursday, August 14, 2008 9:51:23 AM (SA Pacific Standard Time, UTC-05:00)
And if you want more than one letter of the parent folders in the prompt, Tomas gave this to me to use:

return ($loc -replace '\\(\.?)([^\\]{3})[^\\]*(?=\\)','\$1$2')

This replaces the similar line in the shorten-path function.
Thursday, August 14, 2008 10:03:00 AM (SA Pacific Standard Time, UTC-05:00)
Nice, thanks Tomas!
Thursday, August 14, 2008 12:19:02 PM (SA Pacific Standard Time, UTC-05:00)
Very nice. Thank you. I just recently found your site... and it rocks!!.... You Color Schemes are very cool. I am recently porting my Web Highlighter profile to your color scheme. And after that, I will add it to my emacs script.
Thursday, August 14, 2008 2:19:24 PM (SA Pacific Standard Time, UTC-05:00)
Glad you guys liked it :)
Friday, August 15, 2008 2:20:57 AM (SA Pacific Standard Time, UTC-05:00)
To get your computer name try the $env:COMPUTERNAME enevironment variable, it is much more fatser to get and less expnensive than [net.dns]::GetHostName().
Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1041
This Year: 111
This Month: 1
This Week: 0
Comments: 819

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In