-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
129 lines (111 loc) · 3.98 KB
/
Microsoft.PowerShell_profile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# UTILITY ---------------------------------------------------------------------
$PROFILE_DIR = "$(Split-Path -Path $PROFILE)"
function Test-CommandExists {
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Command
)
if (Get-Command $Command -ErrorAction SilentlyContinue) {
Write-Output $true
}
else {
Write-Output $false
}
}
$env:EDITOR = if (Test-CommandExists nvim) {
"nvim"
}
elseif (Test-CommandExists vim) {
"vim"
}
elseif (Test-CommandExists code) {
"code"
}
# Uses -> https://www.ipify.org
function Get-PubIP {
return (Invoke-RestMethod -Uri "https://api.ipify.org?format=json").ip
}
# APPEARANCE ------------------------------------------------------------------
$PSStyle.FileInfo.Directory = "$($PSStyle.Bold)$($PSStyle.Foreground.Blue)"
Set-PSReadLineOption -Colors @{
Default = "$($PSStyle.Reset)"
InlinePrediction = "`e[90;3m"
Operator = "Blue"
Parameter = "Blue"
}
# KEYBINDINGS -----------------------------------------------------------------
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineKeyHandler -Key Ctrl+w -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Key Ctrl+Backspace -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Key Ctrl+LeftArrow -Function BackwardWord
Set-PSReadLineKeyHandler -Key Ctrl+RightArrow -Function ForwardWord
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# ALIASES ---------------------------------------------------------------------
Set-Alias -Name touch -Value New-Item
function which {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ProgramName
)
$Command = Get-Command $ProgramName -ErrorAction SilentlyContinue
if ($Command -and $Command.CommandType -eq 'Application') {
$Command.Source
}
else {
Write-Error "$ProgramName is not a program."
}
}
function .. {
Set-Location ..
}
if (Test-CommandExists "lazygit") {
Set-Alias -Name lg -Value lazygit
}
# SCOOP-SEARCH INTEGRATION ----------------------------------------------------
if (Test-CommandExists "scoop") {
if (-not (Test-CommandExists "scoop-search")) {
scoop install scoop-search
}
Invoke-Expression (&scoop-search --hook)
}
# STARSHIP --------------------------------------------------------------------
if (Test-CommandExists "starship") {
function Invoke-Starship-PreCommand {
# Set the window title
$Host.UI.RawUI.WindowTitle = $PWD.Path.Replace("$HOME", "~")
# Add a newline when needed
if ($Host.UI.RawUI.CursorPosition.Y -ne 0) {
Write-Host
}
# Support Windows Terminal tab/pane duplication
if ($env:WT_SESSION) {
$current_location = $executionContext.SessionState.Path.CurrentLocation
$prompt = "`e]9;12`a"
if ($current_location.Provider.Name -eq "FileSystem") {
$prompt += "`e]9;9;`"$($current_location.ProviderPath)`"`e\"
}
$Host.UI.Write($prompt)
}
}
# Environmental variables
$env:STARSHIP_CONFIG = "$PROFILE_DIR/starship.toml"
if ($IsWindows) {
$env:STARSHIP_CACHE = "$HOME\AppData\Local\Temp\starship"
}
# Create Starship start script if it doesn't exist
if (-not (Test-Path -Path "$env:STARSHIP_CACHE/Start-Starship.ps1" -PathType Leaf)) {
New-Item -ItemType Directory -Force $env:STARSHIP_CACHE | Out-Null
starship completions powershell >"$env:STARSHIP_CACHE/Start-Starship.ps1"
starship init powershell --print-full-init >>"$env:STARSHIP_CACHE/Start-Starship.ps1"
}
# Start Starship
. "$env:STARSHIP_CACHE/Start-Starship.ps1"
}