Skip to content

Instantly share code, notes, and snippets.

@petrsnd
Last active August 9, 2016 21:17
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 petrsnd/77eebce987473c0debc1 to your computer and use it in GitHub Desktop.
Save petrsnd/77eebce987473c0debc1 to your computer and use it in GitHub Desktop.
My Powershell Profile
<#
Prerequisites:
- Sublime Text 3
- VIM 7.4
- GitHub for Windows
I highly recommend ConEmu for use with Powershell.
I install ConEmu using Chocolatey and then change my start screen links to point to
ConEmu, Target="C:\Program Files\ConEmu\ConEmu64.exe" /font "Consolas" /size 16 /bufferheight 9999 /cmd powershell
#>
## Sublime Text 3
$sublimepath = "C:\Program Files\Sublime Text 3\sublime_text.exe"
function sublime {
param([string] $file = $null)
if ($file) {
& $sublimepath $file
}
else {
& $sublimepath (Get-Location)
}
}
## VIM 7.4
$vimdir = "C:\Program Files (x86)\Vim\vim74"
$vimpath = (Join-Path $vimdir "vim.exe")
$gvimpath = (Join-Path $vimdir "gvim.exe")
Set-Alias vi $vimpath
Set-Alias vim $vimpath
Set-Alias gvim $gvimpath
function Edit-Vimrc { vim (Join-Path $home "_vimrc") }
## Profile
$env:EDITOR = "vim"
. (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
$onlineProfile = "https://gist.githubusercontent.com/petrsnd/77eebce987473c0debc1/raw"
function prompt {
$private:fg = $Host.UI.RawUI.ForegroundColor
$private:pwd = $((Get-Location).Path.Replace($HOME, '~'))
$private:gitbranch = $(git rev-parse --abbrev-ref HEAD 2> $null)
$Host.UI.RawUI.ForegroundColor = 'Yellow'
Write-Host "PS " -NoNewLine
Write-Host "${private:pwd}" -NoNewLine
if (-Not [string]::IsNullOrEmpty([string]$gitbranch)) {
$private:gitstatus = $(git status -s)
if (-Not [string]::IsNullOrEmpty([string]$gitstatus)) {
$Host.UI.RawUI.ForegroundColor = 'Red'
}
else {
$Host.UI.RawUI.ForegroundColor = 'Green'
}
Write-Host " [${private:gitbranch}]" -NoNewLine
}
$Host.UI.RawUI.ForegroundColor = $private:fg
Write-Host ">" -NoNewLine
return " "
}
function Edit-Profile { vim $profile }
function Get-OnlineProfile {
Get-Url $onlineProfile $profile
}
## Git aliases
function Invoke-GitStatus {
Invoke-Expression "git status"
}
function Invoke-GitCheckoutBranch {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $name
)
try {
Invoke-Expression "git checkout $name"
}
catch {
try {
Write-Host "Not found in local repo, checking origin..."
Invoke-Expression "git checkout -b $name origin/$name"
}
catch {
Write-Host "Not found in origin, checking upstream..."
Invoke-Expression "git checkout -b $name upstream/$name"
}
}
Invoke-GitResetOrigin $name
}
function Invoke-GitResetOrigin {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $name
)
if (-not $(git branch -a | Select-String "remotes/origin/$name")) {
Write-Host "Pushing $name to origin..."
Invoke-Expression "git push origin $name"
}
if ($(git branch -vv | Select-String $name | Select-String "origin")) {
Write-Host "$name branch already tracking origin"
}
else {
Write-Host "Resetting tracking branch to origin..."
Invoke-Expression "git branch $name --set-upstream-to origin/$name"
}
}
function Invoke-GitListBranch {
Invoke-Expression "git branch -a -v"
}
function Invoke-GitListRemote {
Invoke-Expression "git remote -v"
}
function Invoke-GitFetchPrune {
Invoke-Expression "git fetch origin --prune"
}
function Invoke-GitFetchUpstream {
Invoke-Expression "git fetch upstream"
if (-not $?)
{
Write-Host "upstream not set, trying to configure..."
$remote = (Invoke-Expression "git remote get-url --all origin") -replace 'DanPeterson','GitQuest'
git remote add upstream $remote
Invoke-Expression "git fetch upstream"
}
}
function Invoke-GitRebaseUpstreamBranch {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $name
)
if (Invoke-Expression "git status --porcelain") {
throw "You must not have any uncommitted changes to use this command"
}
Write-Host "Fetching and pruning from origin and fetching upstream..."
Invoke-GitFetchPrune
Invoke-GitFetchUpstream
Write-Host "Switching to $name..."
Invoke-GitCheckoutBranch $name
Write-Host "Rebasing on top of upstream/$name..."
Invoke-Expression "git rebase upstream/$name"
Write-Host "Pushing any changes up to origin..."
Invoke-Expression "git push origin $name"
}
function Invoke-GitNewBranch {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $name
)
Invoke-Expression "git checkout -b $name"
}
function Invoke-GitLog {
param(
[Parameter(Mandatory=$false, Position=0)]
[int] $num
)
if ($num -eq 0) {
Invoke-Expression "git log --pretty=format:'%h%x09%an%x09%ad%x09%s'"
}
else {
Invoke-Expression "git log --pretty=format:'%h%x09%an%x09%ad%x09%s' -$num"
}
}
Set-Alias gs Invoke-GitStatus
Set-Alias gch Invoke-GitCheckoutBranch
Set-Alias gro Invoke-GitResetOrigin
Set-Alias glsb Invoke-GitListBranch
Set-Alias glsr Invoke-GitListRemote
Set-Alias gfp Invoke-GitFetchPrune
Set-Alias gfu Invoke-GitFetchUpstream
Set-Alias gru Invoke-GitRebaseUpstreamBranch
Set-Alias gnewb Invoke-GitNewBranch
Set-Alias glog Invoke-GitLog
## Useful tools
function All-Properties {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$InputObject
)
PROCESS {
foreach ($input in $InputObject) {
$input | Format-List ([string[]]($input | Get-Member -MemberType Property | %{ $_.Name } | Sort-Object))
}
}
}
function Test-Command {
param(
[Parameter(Mandatory=$true)]
[string] $Command
)
Get-Command $Command -EA SilentlyContinue
}
Set-Variable -Name ORIGBGCOLOR -Value $host.ui.rawui.BackgroundColor -Scope global
Set-Variable -Name ORIGFGCOLOR -Value $host.ui.rawui.ForegroundColor -Scope global
function Reset-Colors {
$host.ui.rawui.BackgroundColor = $global:ORIGBGCOLOR
$host.ui.rawui.ForegroundColor = $global:ORIGFGCOLOR
Clear-Host
}
function isURI($uri) {
($uri -as [System.URI]).AbsoluteURI -ne $null
}
function Get-Url {
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $Url,
[Parameter(Mandatory=$false, Position=1)]
[string] $Target
)
if (-not (isURI $url)) {
throw "You must supply a valid URL parameter"
}
if ($Target) {
$saveFile = $Target
}
else {
$saveFile = (Join-Path (Get-Location) (Split-Path -Leaf $url))
}
Write-Host "Download URL: $url"
Write-Host "Save destination: $saveFile"
$response = Invoke-WebRequest $url -OutFile $saveFile -PassThru
if ( $response.StatusCode -ne 200 ) {
throw "Download failed with status $($response.StatusCode)"
}
}
Set-Alias download Get-Url
Set-Alias dl Get-Url
function Open-Explorer {
param(
[Parameter(Mandatory=$false)]
[string] $Path = $(pwd)
)
& explorer.exe $Path
}
function Zip-Files {
param(
[Parameter(Mandatory=$true)]
[string] $ZipFileName,
[Parameter(Mandatory=$true)]
[string] $SourceDirectory
)
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$sourceDir = Resolve-Path $sourceDirectory
[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory,
$ZipFileName, $compressionLevel, $false)
}
Set-Alias open Open-Explorer
## Handy Unix compat
# which
function which {
param(
[Parameter(Mandatory=$true)]
[string] $Command
)
Test-Command $Command | Select-Object -ExpandProperty Definition
}
# ll
Set-Alias ll ls
# cd
Remove-Item Alias:cd
function cd {
if ($args[0] -eq $null -or $args[0] -eq '~') {
$private:pwd = $ENV:HOME
} elseif ($args[0] -eq "-") {
$private:pwd = $global:OLDPWD
}
else {
$private:pwd = $args[0]
}
$private:tmp = (Get-Location)
if ($private:pwd) {
Set-Location $private:pwd
}
Set-Variable -Name OLDPWD -Value $private:tmp -Scope global
if ((Resolve-Path .).Provider.Name -eq "FileSystem") {
[Environment]::CurrentDirectory = $private:pwd
}
New-Item 'HKCU:\Software\Implbits\Shell' -Force | New-ItemProperty -Name CurrentDirectory -Value (Resolve-Path ".").Path -Force | Out-Null
}
# Unix find
$unixfind = @(Get-ChildItem (Resolve-Path "$env:LOCALAPPDATA\GitHub") -Recurse -EA SilentlyContinue `
-Filter "find.exe" | ForEach-Object { $_.FullName })[0]
Set-Alias find $unixfind
# reset terminal
Set-Alias reset Reset-Colors
## Path
$env:Path = "$env:Path;$env:SystemDrive\Python27;$env:SystemDrive\Python27\Scripts"
$env:PATHEXT = "$env:PATHEXT;.PY"
## Start Directory
if (Test-Path "HKCU:\Software\Implbits\Shell") {
$private:prop = (Get-ItemProperty "HKCU:\Software\Implbits\Shell" -Name CurrentDirectory).CurrentDirectory
if ($private:prop) {
cd $private:prop
}
}
## History
$historyFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history
Register-EngineEvent PowerShell.Exiting -Action {
Get-History | Export-Clixml $historyFilePath
} | Out-Null
if (Test-Path $historyFilePath) {
Import-Clixml $historyFilePath | Add-History
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment