-
Notifications
You must be signed in to change notification settings - Fork 0
/
Microsoft.PowerShell_profile.ps1
126 lines (107 loc) · 4.11 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
# UTILITY ---------------------------------------------------------------------
$PROFILE_DIR = "$(Split-Path -Path $PROFILE)"
function Test-CommandExists {
param (
[Parameter(Position=0,mandatory=$true)]
[string]$Command
)
return [bool](Get-Command $Command -ErrorAction SilentlyContinue)
}
$env:SHELL = "pwsh" # Can cause problems
$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
}
# Source -> https://github.com/ChrisTitusTech/winutil
if ($IsWindows) {
function winutil {
# Define the command to execute
$command = 'Invoke-RestMethod "https://christitus.com/win" | Invoke-Expression'
# Check if the script is being run as an administrator
if (-not ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
# Launch the script with administrator privileges
if (Test-CommandExists "wt.exe") {
# Use Windows Terminal if installed
Start-Process wt.exe -ArgumentList "pwsh.exe -Command `"$command`"" -Verb RunAs
}
else {
Start-Process pwsh.exe -ArgumentList "-Command `"$command`"" -Verb RunAs
}
}
else {
# Execute the command directly if already running as administrator
Invoke-Expression $command
}
}
}
# 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 .. {
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
}
# Enable tab/pane duplication in Windows Terminal
if ($env:WT_SESSION) {
$loc = $executionContext.SessionState.Path.CurrentLocation;
$prompt = "$([char]27)]9;12$([char]7)"
if ($loc.Provider.Name -eq "FileSystem") {
$prompt += "$([char]27)]9;9;`"$($loc.ProviderPath)`"$([char]27)\"
}
$host.ui.Write($prompt)
}
}
# Environmental variables
$env:STARSHIP_CONFIG = "$PROFILE_DIR/starship.toml"
if ($IsWindows) {
$env:STARSHIP_CACHE = "$HOME/AppData/Local/Temp"
}
# Start Starship
. "$PROFILE_DIR/Start-Starship.ps1"
}