Skip to content

Instantly share code, notes, and snippets.

@kensleDev
Last active April 25, 2024 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kensleDev/13f9290876e782b2e67aab8b463e55f7 to your computer and use it in GitHub Desktop.
Save kensleDev/13f9290876e782b2e67aab8b463e55f7 to your computer and use it in GitHub Desktop.
Dev terminal setup for Powershell

Powershell Config

This is a full featured powershell config that includes:

  • Automatic install of Powershell modules and thier corroponding command line tools.
  • Themed prompt via Oh-My-Posh
  • Auto completion of commands with list of options via PSReadLine
  • Directory navigation by partial names via Z
  • Result colorizing and file icons via Terminal Icons

All Avalible Modules/Tools

Install

  1. Install Powershell
  • Windows: Get from Windows Store - This is not the pre-installed version of Powershell(Windows Powershell), although this should work with the pre-installed version.
  • OSX: Follow instructions here to install homebrew, if already installed:
brew install powershell
  1. Update font in terminal editor with your faviorite [https://www.nerdfonts.com/font-downloads](Nerd Font)
  • Windows: Open Windows Terminal => go to settings => Powershell => Appearence
  • OSX: I prefer Kitty
  1. Start a new Powershell session and copy the content of Download.ps1 into the terminal

This will download Install.ps1 and takuya.omp.json to ~/.config/powershell

  1. Run ~/.config/powershell/Install.ps1

This will install the Powershell Module and the corropsonding command line tool if needed.

  1. Once the installation has finished restart the shell session or run:
pwsh

To restart the shell

Themeing

I have included Takuya's theme as the defalult prompt but there are many prompts avalible here

Credit

This config is a result of watching Takuya Matsuyama's Powershell config video and I will continue to build on it with my own preferences. Many thanks to Takuya for creating this video as it's been a big help in recreating my Zsh workflow in Powershell.

This setup has been tested on Windows and OSX but could quite easily be adapted to work on linux.

These are the inital differences between mine and Takuya's config:

  1. More Aliases
# General
Set-Alias c clear
Set-Alias l ls

# Files/Locations
function .. { cd .. }
function home { cd $env:USERPROFILE }
function dl { cd $env:USERPROFILE\Downloads }
function docs { cd $env:USERPROFILE\Documents }

# Programs
Set-Alias grep findstr
Set-Alias v nvim

# Git
Set-Alias g git
function ga { git add -A }
function gc { git commit }
function gac { git add -A; git commit }

  1. Option to allow tabbing through avalible options rather then printing them to the screen:
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
$powershellDir = "~\.config\powershell"
New-Item -ItemType Directory -Force -Path $powershellDir
cd $powershellDir
Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/kensleDev/13f9290876e782b2e67aab8b463e55f7/raw/66cdc60aa61da8a88a480c14b5fe817beba7dab9/profile.ps1' -OutFile 'profile.ps1'
Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/kensleDev/13f9290876e782b2e67aab8b463e55f7/raw/cda7a409a315bf767f830897b0b8f53611e11f1e/Install.ps1' -OutFile 'Install.ps1'
Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/kensleDev/13f9290876e782b2e67aab8b463e55f7/raw/13c1f7eb8ffa50e385c3b960c76ded9389bdaf3e/takuya.omp.json' -OutFile 'takuya.omp.json'
Set-ExecutionPolicy -s cu unrestricted
$OS_Installer = ''
if ($IsMacOS) {
$OS_Installer = 'brew'
} elseif ($IsWindows) {
$OS_Installer = 'scoop'
} else {
echo 'OS NOT SUPPORTED!'
}
function InstallInstaller($Installer) {
if (Get-Command $Installer -errorAction SilentlyContinue) {
"-> $Installer Installed"
} else {
if($Installer -eq 'scoop') {
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
# iwr -useb get.scoop.sh -outfile 'scoop-installer.ps1'
# .\scoop-installer.ps1 -ScoopDir 'C:\scoop' -ScoopGlobalDir 'C:\GlobalScoopApps' -
# rm scoop-installer.ps1
} elseif($Installer -eq 'brew') {
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
} else {
echo 'Installer NOT SUPPORTED!'
}
}
}
function UpdatePowershellConfigLocation {
$configLocation = ". $env:USERPROFILE\.config\powershell\user_profile.ps1"
$configLocation | Out-File $PROFILE.CurrentUserCurrentHost
}
function InstallProgram($program) {
$cmd = $OS_Installer + ' install ' + $program
Invoke-Expression $cmd
echo 'Installed: ' $program
}
function InstallModules {
Install-Module posh-git -Scope CurrentUser -force
# Install-Module oh-my-posh -Scope CurrentUser -force
Install-Module Terminal-Icons -Scope CurrentUser -force
Install-Module -Name z -force
Install-Module PSReadline -AllowPrerelease -Scope CurrentUser -force -SkipPublisherCheck
Install-Module PSFzf -Scope CurrentUser -force
Install-Module nvm
}
function InstallPrograms {
InstallProgram('https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json')
InstallProgram('git')
InstallProgram('z')
InstallProgram('fzf')
InstallProgram('nvm')
}
function CleanUp {
rm $env:USERPROFILE\.config\powershell\- -Force
}
UpdatePowershellConfigLocation
InstallInstaller($OS_Installer)
InstallPrograms
InstallModules
CleanUp
# set PowerShell to UTF-8
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Import-Module posh-git
# Import-Module oh-my-posh # Now handled via scoop in Install.ps1
$omp_config = Join-Path $PSScriptRoot ".\takuya.omp.json"
oh-my-posh --init --shell pwsh --config $omp_config | Invoke-Expression
Import-Module -Name Terminal-Icons
# PSReadLine
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -BellStyle None
Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# Fzf
Import-Module PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PSReadlineChordReverseHistory 'Ctrl+r'
# Aliases
# General
Set-Alias c clear
Set-Alias l ls
# Files/Locations
function .. { cd .. }
function home { cd $env:USERPROFILE }
function ~ { cd ~ }
function dl { cd $env:USERPROFILE\Downloads }
function docs { cd $env:USERPROFILE\Documents }
# Programs
Set-Alias grep findstr
Set-Alias v nvim
# Git
Set-Alias g git
function ga { git add -A }
function gc { git commit }
function gac { git add -A; git commit }
# Utilities
function which ($command) {
Get-Command -Name $command -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"background": "#0077c2",
"foreground": "#ffffff",
"leading_diamond": "\u256d\u2500\ue0b6",
"style": "diamond",
"template": " {{ .Name }} ",
"type": "shell"
},
{
"background": "#ef5350",
"foreground": "#FFFB38",
"properties": {
"root_icon": "\uf292"
},
"style": "diamond",
"template": "<parentBackground>\ue0b0</> \uf0e7 ",
"type": "root"
},
{
"background": "#444444",
"foreground": "#E4E4E4",
"powerline_symbol": "\ue0b0",
"properties": {
"style": "full"
},
"style": "powerline",
"template": " {{ .Path }} ",
"type": "path"
},
{
"background": "#FFFB38",
"background_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}#ffeb95{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}#c5e478{{ end }}",
"{{ if gt .Ahead 0 }}#C792EA{{ end }}",
"{{ if gt .Behind 0 }}#C792EA{{ end }}"
],
"foreground": "#011627",
"powerline_symbol": "\ue0b0",
"properties": {
"branch_icon": "\ue725 ",
"fetch_status": true,
"fetch_upstream_icon": true
},
"style": "powerline",
"template": " {{ .HEAD }} {{ if .Working.Changed }}{{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }}<#ef5350> \uf046 {{ .Staging.String }}</>{{ end }} ",
"type": "git"
}
],
"type": "prompt"
},
{
"alignment": "right",
"segments": [
{
"background": "#303030",
"foreground": "#3C873A",
"leading_diamond": " \ue0b6",
"properties": {
"fetch_package_manager": true,
"npm_icon": " <#cc3a3a>\ue5fa</> ",
"yarn_icon": " <#348cba>\uf61a</>"
},
"style": "diamond",
"template": "\ue718 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }}",
"trailing_diamond": "\ue0b4",
"type": "node"
},
{
"background": "#40c4ff",
"foreground": "#ffffff",
"invert_powerline": true,
"leading_diamond": " \ue0b6",
"style": "diamond",
"template": " \uf5ef {{ .CurrentDate | date .Format }} ",
"trailing_diamond": "\ue0b4",
"type": "time"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#21c7c7",
"style": "plain",
"template": "\u2570\u2500",
"type": "text"
},
{
"foreground": "#e0f8ff",
"foreground_templates": [
"{{ if gt .Code 0 }}#ef5350{{ end }}"
],
"properties": {
"always_enabled": true
},
"style": "plain",
"template": "\u276f{{ if gt .Code 0 }}\uf00d{{ else }}\uf42e{{ end }} ",
"type": "exit"
}
],
"type": "prompt"
}
],
"osc99": true,
"version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment