Skip to content

Instantly share code, notes, and snippets.

@GhostofGoes
Last active November 18, 2018 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GhostofGoes/23e5d8f7011fe0164bd3fc005e5bb744 to your computer and use it in GitHub Desktop.
Save GhostofGoes/23e5d8f7011fe0164bd3fc005e5bb744 to your computer and use it in GitHub Desktop.
PowerShell Profile: "notepad $profile.CurrentUserAllHosts"
# Provides same functionality as the Unix "which" command
function which($commandName)
{
(Get-Command $commandName).Definition
}
# Shortens a filesystem path to singe-characters [used by prompt()].
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')
}
# Shortens the path displayed at each line of the prompt.
# If in the root of a git repo, the Git status will be appended to the prompt.
function prompt {
if(Test-Path .git) {
# retrieve branch name
$symbolicref = (git symbolic-ref HEAD)
$branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
# retrieve branch status
$differences = (git diff-index HEAD --name-status)
$git_mod_count = [regex]::matches($differences, 'M\s').count
$git_add_count = [regex]::matches($differences, 'A\s').count
$git_del_count = [regex]::matches($differences, 'D\s').count
$branchData = " +$git_add_count ~$git_mod_count -$git_del_count"
# write status string (-n : NoNewLine; -f : ForegroundColor)
write-host 'GIT' -n -f White
write-host ' {' -n -f Yellow
write-host (shorten-path (pwd).Path) -n -f White
write-host ' [' -n -f Yellow
write-host $branch -n -f Cyan
write-host $branchData -n -f Red
write-host ']' -n -f Yellow
write-host ">" -n -f Yellow
}
else {
# write status string
write-host 'PS' -n -f White
write-host ' {' -n -f Yellow
write-host (shorten-path (pwd).Path) -n -f White
write-host ">" -n -f Yellow
}
return " "
}
# Chocolatey profile
# $ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
# if (Test-Path($ChocolateyProfile)) {
# Import-Module "$ChocolateyProfile"
# }
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
$aliases = @("pipenv") + @(Get-Alias | where { $_.Definition -eq "pipenv" } | select -Exp Name)
$aliasPattern = "($($aliases -join '|'))"
if($lastBlock -match "^$aliasPattern ") {
$Env:_PIPENV_COMPLETE = "complete-powershell"
$Env:COMMANDLINE = "$lastBlock"
(pipenv) | ? {$_.trim() -ne "" }
Remove-Item Env:_PIPENV_COMPLETE
Remove-Item Env:COMMANDLINE
}
elseif (Test-Path Function:\pipenvTabExpansionBackup) {
# Fall back on existing tab expansion
pipenvTabExpansionBackup $line $lastWord
}
}
# Pipenv tab completion
if ((Test-Path Function:\TabExpansion) -and -not (Test-Path Function:\pipenvTabExpansionBackup)) {
Rename-Item Function:\TabExpansion pipenvTabExpansionBackup
}
# Change directory to Documents when a new prompt is opened
cd $env:USERPROFILE\Documents\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment