-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
157 lines (134 loc) · 4.82 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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
)
try {
$Command = Get-Command -Name $ProgramName -ErrorAction Stop -CommandType Application
return $Command.Source
}
catch {
Write-Error "$ProgramName is not a program."
}
}
function .. {
Set-Location ..
}
if (Test-CommandExists eza) {
Remove-Item -Path Alias:ls
function ls {
param([Parameter(ValueFromRemainingArguments=$true)] [string[]] $Args)
eza --icons --group-directories-first @Args
}
function ll {
param([Parameter(ValueFromRemainingArguments=$true)] [string[]] $Args)
eza --icons --group-directories-first --git -lh @Args
}
function lt {
param([Parameter(ValueFromRemainingArguments=$true)] [string[]] $Args)
eza --icons --group-directories-first --git -lT -L 3 @Args
}
function la {
param([Parameter(ValueFromRemainingArguments=$true)] [string[]] $Args)
eza --icons --group-directories-first -a @Args
}
function lla {
param([Parameter(ValueFromRemainingArguments=$true)] [string[]] $Args)
eza --icons --group-directories-first --git -lha @Args
}
}
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"
}