Skip to content

Instantly share code, notes, and snippets.

@holtalanm
Last active September 3, 2019 19:30
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 holtalanm/5b4fb21f1b7626a0cae7ac2e89f53dea to your computer and use it in GitHub Desktop.
Save holtalanm/5b4fb21f1b7626a0cae7ac2e89f53dea to your computer and use it in GitHub Desktop.
# Init Script for PowerShell
# Created as part of cmder project
# !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
# !!! Use "%CMDER_ROOT%\config\user-profile.ps1" to add your own startup commands
# Compatibility with PS major versions <= 2
if(!$PSScriptRoot) {
$PSScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path
}
if ($ENV:CMDER_USER_CONFIG) {
# write-host "CMDER IS ALSO USING INDIVIDUAL USER CONFIG FROM '$ENV:CMDER_USER_CONFIG'!"
}
# We do this for Powershell as Admin Sessions because CMDER_ROOT is not beng set.
if (! $ENV:CMDER_ROOT ) {
if ( $ENV:ConEmuDir ) {
$ENV:CMDER_ROOT = resolve-path( $ENV:ConEmuDir + "\..\.." )
} else {
$ENV:CMDER_ROOT = resolve-path( $PSScriptRoot + "\.." )
}
}
# Remove trailing '\'
$ENV:CMDER_ROOT = (($ENV:CMDER_ROOT).trimend("\"))
# do not load bundled psget if a module installer is already available
# -> recent PowerShell versions include PowerShellGet out of the box
$moduleInstallerAvailable = [bool](Get-Command -Name 'Install-Module' -ErrorAction SilentlyContinue | Out-Null)
# do not load bundled psget if a module installer is already available
# -> recent PowerShell versions include PowerShellGet out of the box
$moduleInstallerAvailable = [bool](Get-Command -Name 'Install-Module' -ErrorAction SilentlyContinue | Out-Null)
# Add Cmder modules directory to the autoload path.
$CmderModulePath = Join-path $PSScriptRoot "psmodules/"
if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderModulePath) ){
$env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;")
}
try {
Get-command -Name "vim" -ErrorAction Stop >$null
} catch {
# # You could do this but it may be a little drastic and introduce a lot of
# # unix tool overlap with powershel unix like aliases
# $env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\usr\bin")
# set-alias -name "vi" -value "vim"
# # I think the below is safer.
new-alias -name "vim" -value $($ENV:CMDER_ROOT + "\vendor\git-for-windows\usr\bin\vim.exe")
new-alias -name "vi" -value vim
}
try {
# Check if git is on PATH, i.e. Git already installed on system
Get-command -Name "git" -ErrorAction Stop >$null
} catch {
$env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\cmd")
# for bash.exe, which in the cmd version is found as <GIT>\usr\bin\bash.exe
$env:Path += $(";" + $env:CMDER_ROOT + "\vendor\git-for-windows\bin")
}
$gitLoaded = $false
function Import-Git($Loaded){
if($Loaded) { return }
$GitModule = Get-Module -Name Posh-Git -ListAvailable
if($GitModule | select version | where version -le ([version]"0.6.1.20160330")){
Import-Module Posh-Git > $null
}
if(-not ($GitModule) ) {
Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart cmder."
}
# Make sure we only run once by alawys returning true
return $true
}
function checkGit($Path) {
if (Test-Path -Path (Join-Path $Path '.git') ) {
$gitLoaded = Import-Git $gitLoaded
Write-VcsStatus
return
}
$SplitPath = split-path $path
if ($SplitPath) {
checkGit($SplitPath)
}
}
if (Get-Module PSReadline -ErrorAction "SilentlyContinue") {
Set-PSReadlineOption -ExtraPromptLineCount 1
}
# Enhance Path
$env:Path = "$Env:CMDER_ROOT\bin;$env:Path;$Env:CMDER_ROOT"
#
# Prompt Section
# Users should modify their user-profile.ps1 as it will be safe from updates.
#
# Pre assign the hooks so the first run of cmder gets a working prompt.
[ScriptBlock]$PrePrompt = {}
[ScriptBlock]$PostPrompt = {}
[ScriptBlock]$CmderPrompt = {
$Host.UI.RawUI.ForegroundColor = "White"
Microsoft.PowerShell.Utility\Write-Host $pwd.ProviderPath -NoNewLine -ForegroundColor Green
checkGit($pwd.ProviderPath)
}
<#
This scriptblock runs every time the prompt is returned.
Explicitly use functions from MS namespace to protect from being overridden in the user session.
Custom prompt functions are loaded in as constants to get the same behaviour
#>
[ScriptBlock]$Prompt = {
$realLASTEXITCODE = $LASTEXITCODE
$host.UI.RawUI.WindowTitle = Microsoft.PowerShell.Management\Split-Path $pwd.ProviderPath -Leaf
PrePrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
CmderPrompt
Microsoft.PowerShell.Utility\Write-Host "`nλ " -NoNewLine -ForegroundColor "DarkGray"
PostPrompt | Microsoft.PowerShell.Utility\Write-Host -NoNewline
$global:LASTEXITCODE = $realLASTEXITCODE
return " "
}
# Drop *.ps1 files into "$ENV:CMDER_ROOT\config\profile.d"
# to source them at startup.
if (-not (test-path "$ENV:CMDER_ROOT\config\profile.d")) {
mkdir "$ENV:CMDER_ROOT\config\profile.d"
}
pushd $ENV:CMDER_ROOT\config\profile.d
foreach ($x in Get-ChildItem *.ps1) {
# write-host write-host Sourcing $x
. $x
}
popd
# Drop *.ps1 files into "$ENV:CMDER_USER_CONFIG\config\profile.d"
# to source them at startup. Requires using cmder.exe /C [cmder_user_root_path] argument
if ($ENV:CMDER_USER_CONFIG -ne "" -and (test-path "$ENV:CMDER_USER_CONFIG\profile.d")) {
pushd $ENV:CMDER_USER_CONFIG\profile.d
foreach ($x in Get-ChildItem *.ps1) {
# write-host write-host Sourcing $x
. $x
}
popd
}
$CmderUserProfilePath = Join-Path $env:CMDER_ROOT "config\user-profile.ps1"
if (Test-Path $CmderUserProfilePath) {
# Create this file and place your own command in there.
. "$CmderUserProfilePath"
}
if ($ENV:CMDER_USER_CONFIG) {
$CmderUserProfilePath = Join-Path $ENV:CMDER_USER_CONFIG "user-profile.ps1"
}
if (Test-Path $CmderUserProfilePath) {
. "$CmderUserProfilePath"
} else {
# This multiline string cannot be indented, for this reason I've not indented the whole block
Write-Host -BackgroundColor Darkgreen -ForegroundColor White "First Run: Creating user startup file: $CmderUserProfilePath"
$UserProfileTemplate = @'
# Use this file to run your own startup commands
## Prompt Customization
<#
.SYNTAX
<PrePrompt><CMDER DEFAULT>
λ <PostPrompt> <repl input>
.EXAMPLE
<PrePrompt>N:\Documents\src\cmder [master]
λ <PostPrompt> |
#>
[ScriptBlock]$PrePrompt = {
}
# Replace the cmder prompt entirely with this.
# [ScriptBlock]$CmderPrompt = {}
[ScriptBlock]$PostPrompt = {
}
## <Continue to add your own>
'@
New-Item -ItemType File -Path $CmderUserProfilePath -Value $UserProfileTemplate > $null
}
# Once Created these code blocks cannot be overwritten
Set-Item -Path function:\PrePrompt -Value $PrePrompt -Options Constant
Set-Item -Path function:\CmderPrompt -Value $CmderPrompt -Options Constant
Set-Item -Path function:\PostPrompt -Value $PostPrompt -Options Constant
# Functions can be made constant only at creation time
# ReadOnly at least requires `-force` to be overwritten
Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly
{
"globals" :
{
"alwaysShowTabs" : true,
"defaultProfile" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"initialCols" : 120,
"initialRows" : 30,
"keybindings" :
[
{
"command" : "closeTab",
"keys" :
[
"ctrl+w"
]
},
{
"command" : "newTab",
"keys" :
[
"ctrl+t"
]
},
{
"command" : "newTabProfile0",
"keys" :
[
"ctrl+shift+1"
]
},
{
"command" : "newTabProfile1",
"keys" :
[
"ctrl+shift+2"
]
},
{
"command" : "newTabProfile2",
"keys" :
[
"ctrl+shift+3"
]
},
{
"command" : "newTabProfile3",
"keys" :
[
"ctrl+shift+4"
]
},
{
"command" : "newTabProfile4",
"keys" :
[
"ctrl+shift+5"
]
},
{
"command" : "newTabProfile5",
"keys" :
[
"ctrl+shift+6"
]
},
{
"command" : "newTabProfile6",
"keys" :
[
"ctrl+shift+7"
]
},
{
"command" : "newTabProfile7",
"keys" :
[
"ctrl+shift+8"
]
},
{
"command" : "newTabProfile8",
"keys" :
[
"ctrl+shift+9"
]
},
{
"command" : "nextTab",
"keys" :
[
"ctrl+tab"
]
},
{
"command" : "openSettings",
"keys" :
[
"ctrl+,"
]
},
{
"command" : "prevTab",
"keys" :
[
"ctrl+shift+tab"
]
},
{
"command" : "scrollDown",
"keys" :
[
"ctrl+shift+down"
]
},
{
"command" : "scrollDownPage",
"keys" :
[
"ctrl+shift+pgdn"
]
},
{
"command" : "scrollUp",
"keys" :
[
"ctrl+shift+up"
]
},
{
"command" : "scrollUpPage",
"keys" :
[
"ctrl+shift+pgup"
]
},
{
"command" : "switchToTab0",
"keys" :
[
"alt+1"
]
},
{
"command" : "switchToTab1",
"keys" :
[
"alt+2"
]
},
{
"command" : "switchToTab2",
"keys" :
[
"alt+3"
]
},
{
"command" : "switchToTab3",
"keys" :
[
"alt+4"
]
},
{
"command" : "switchToTab4",
"keys" :
[
"alt+5"
]
},
{
"command" : "switchToTab5",
"keys" :
[
"alt+6"
]
},
{
"command" : "switchToTab6",
"keys" :
[
"alt+7"
]
},
{
"command" : "switchToTab7",
"keys" :
[
"alt+8"
]
},
{
"command" : "switchToTab8",
"keys" :
[
"alt+9"
]
}
],
"requestedTheme" : "system",
"showTabsInTitlebar" : true,
"showTerminalTitleInTitlebar" : true,
"wordDelimiters" : " ./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}~?\u2502"
},
"profiles" :
[
{
"acrylicOpacity" : 0.5,
"background" : "#3F3F3F",
"closeOnExit" : true,
"colorScheme" : "Zenburn",
"commandline" : "powershell.exe -ExecutionPolicy Bypass -NoLogo -NoProfile -NoExit -Command \"Invoke-Expression '. ''%ConEmuDir%\\..\\profile.ps1'''\"",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "DejaVu Sans Mono for Powerline",
"fontSize" : 9,
"guid" : "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
"name" : "Windows PowerShell",
"padding" : "15px, 15px, 15px, 15px",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : false
},
{
"acrylicOpacity" : 0.75,
"closeOnExit" : true,
"colorScheme" : "Campbell",
"commandline" : "cmd.exe",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"guid" : "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
"name" : "cmd",
"padding" : "15px, 15px, 15px, 15px",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : true
},
{
"acrylicOpacity" : 0.5,
"closeOnExit" : true,
"colorScheme" : "Zenburn",
"commandline" : "wsl.exe -d Ubuntu-18.04",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "DejaVu Sans Mono for Powerline",
"fontSize" : 9,
"guid" : "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
"name" : "Ubuntu-18.04",
"padding" : "15px, 15px, 15px, 15px",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : false
}
],
"schemes" :
[
{
"background" : "#0C0C0C",
"black" : "#0C0C0C",
"blue" : "#0037DA",
"brightBlack" : "#767676",
"brightBlue" : "#3B78FF",
"brightCyan" : "#61D6D6",
"brightGreen" : "#16C60C",
"brightPurple" : "#B4009E",
"brightRed" : "#E74856",
"brightWhite" : "#F2F2F2",
"brightYellow" : "#F9F1A5",
"cyan" : "#3A96DD",
"foreground" : "#F2F2F2",
"green" : "#13A10E",
"name" : "Campbell",
"purple" : "#881798",
"red" : "#C50F1F",
"white" : "#CCCCCC",
"yellow" : "#C19C00"
},
{
"background" : "#282C34",
"black" : "#282C34",
"blue" : "#61AFEF",
"brightBlack" : "#5A6374",
"brightBlue" : "#61AFEF",
"brightCyan" : "#56B6C2",
"brightGreen" : "#98C379",
"brightPurple" : "#C678DD",
"brightRed" : "#E06C75",
"brightWhite" : "#DCDFE4",
"brightYellow" : "#E5C07B",
"cyan" : "#56B6C2",
"foreground" : "#DCDFE4",
"green" : "#98C379",
"name" : "One Half Dark",
"purple" : "#C678DD",
"red" : "#E06C75",
"white" : "#DCDFE4",
"yellow" : "#E5C07B"
},
{
"background" : "#FAFAFA",
"black" : "#383A42",
"blue" : "#0184BC",
"brightBlack" : "#4F525D",
"brightBlue" : "#61AFEF",
"brightCyan" : "#56B5C1",
"brightGreen" : "#98C379",
"brightPurple" : "#C577DD",
"brightRed" : "#DF6C75",
"brightWhite" : "#FFFFFF",
"brightYellow" : "#E4C07A",
"cyan" : "#0997B3",
"foreground" : "#383A42",
"green" : "#50A14F",
"name" : "One Half Light",
"purple" : "#A626A4",
"red" : "#E45649",
"white" : "#FAFAFA",
"yellow" : "#C18301"
},
{
"background" : "#073642",
"black" : "#073642",
"blue" : "#268BD2",
"brightBlack" : "#002B36",
"brightBlue" : "#839496",
"brightCyan" : "#93A1A1",
"brightGreen" : "#586E75",
"brightPurple" : "#6C71C4",
"brightRed" : "#CB4B16",
"brightWhite" : "#FDF6E3",
"brightYellow" : "#657B83",
"cyan" : "#2AA198",
"foreground" : "#FDF6E3",
"green" : "#859900",
"name" : "Solarized Dark",
"purple" : "#D33682",
"red" : "#D30102",
"white" : "#EEE8D5",
"yellow" : "#B58900"
},
{
"background" : "#FDF6E3",
"black" : "#073642",
"blue" : "#268BD2",
"brightBlack" : "#002B36",
"brightBlue" : "#839496",
"brightCyan" : "#93A1A1",
"brightGreen" : "#586E75",
"brightPurple" : "#6C71C4",
"brightRed" : "#CB4B16",
"brightWhite" : "#FDF6E3",
"brightYellow" : "#657B83",
"cyan" : "#2AA198",
"foreground" : "#073642",
"green" : "#859900",
"name" : "Solarized Light",
"purple" : "#D33682",
"red" : "#D30102",
"white" : "#EEE8D5",
"yellow" : "#B58900"
},
{
"background" : "#3F3F3F",
"black" : "#332323",
"blue" : "#AA50AA",
"brightBlack" : "#E37170",
"brightBlue" : "#6464AF",
"brightCyan" : "#8CD0D3",
"brightGreen" : "#7F9F7F",
"brightPurple" : "#C880C8",
"brightRed" : "#008000",
"brightWhite" : "#DCDC00",
"brightYellow" : "#F0DFAF",
"cyan" : "#AFAFFF",
"foreground" : "#FFFFFF",
"green" : "#008080",
"name" : "Zenburn",
"purple" : "#DCDCCC",
"red" : "#C08080",
"white" : "#FFFFFF",
"yellow" : "#DCDC00"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment