-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathposh-aliases.ps1
189 lines (164 loc) · 6.71 KB
/
posh-aliases.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# ----------------------------------------------------------------------------------- #
# Aliases: #
# ----------------------------------------------------------------------------------- #
# Helper functions
function New-Directory {
<#
.SYNOPSIS
Creates a new directory and cd into it. Alias: mkcd
#>
[CmdletBinding()]
param ([Parameter(Mandatory = $True)]$Path)
New-Item -Path $Path -ItemType Directory | Out-Null
Set-Location -Path $Path
}
function New-File {
<#
.SYNOPSIS
Creates a new file with the specified name and extension. Alias: touch
#>
[CmdletBinding()]
param ([Parameter(Mandatory = $true, Position = 0)][string]$Name)
New-Item -ItemType File -Name $Name -Path $PWD | Out-Null
}
function Find-File {
<#
.SYNOPSIS
Finds a file in the current directory and all subdirectories. Alias: ff
#>
[CmdletBinding()]
param ([Parameter(ValueFromPipeline, Mandatory = $true, Position = 0)][string]$SearchTerm)
$result = Get-ChildItem -Recurse -Filter "*$SearchTerm*" -File -ErrorAction SilentlyContinue
$result.FullName
}
function Find-String {
<#
.SYNOPSIS
Searches for a string in a file or directory. Alias: grep
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$SearchTerm,
[Parameter(ValueFromPipeline, Mandatory = $false, Position = 1)]
[Alias('d')][string]$Directory,
[Parameter(Mandatory = $false)]
[Alias('f')][switch]$Recurse
)
if ($Directory) {
if ($Recurse) { Get-ChildItem -Recurse $Directory | Select-String $SearchTerm; return }
Get-ChildItem $Directory | Select-String $SearchTerm
return
}
if ($Recurse) { Get-ChildItem -Recurse | Select-String $SearchTerm; return }
Get-ChildItem | Select-String $SearchTerm
}
function Get-Aliases {
<#
.SYNOPSIS
Show information of user's defined aliases. Alias: aliases
#>
[CmdletBinding()]
param()
#requires -Module PSScriptTools
Get-MyAlias |
Sort-Object Source, Name |
Format-Table -Property Name, Definition, Version, Source -AutoSize
}
function Get-CommandInfo {
<#
.SYNOPSIS
Displays the definition of a command. Alias: which
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$Name
)
Get-Command $Name | Select-Object -ExpandProperty Definition
}
function Remove-MyItem {
<#
.SYNOPSIS
Removes an item and (optionally) all its children. Alias: rm
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[switch]$rf,
[Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$Path
)
Remove-Item $Path -Recurse:$rf -Force:$rf
}
# ----------------------------------------------------------------------------------- #
# general
Set-Alias -Name 'aliases' -Value 'Get-Aliases'
Set-Alias -Name 'ff' -Value 'Find-File'
Set-Alias -Name 'grep' -Value 'Find-String'
Set-Alias -Name 'mkcd' -Value 'New-Directory'
Set-Alias -Name 'touch' -Value 'New-File'
Set-Alias -Name 'which' -Value 'Get-CommandInfo'
#Remove-Item Alias:rm -Force -ErrorAction SilentlyContinue
Set-Alias -Name 'rm' -Value 'Remove-MyItem'
# ----------------------------------------------------------------------------------- #
# We need 'posh-alias' module to add the following aliases.
if (!(Get-Command 'Add-Alias' -ErrorAction SilentlyContinue)) {
Install-Module -Name "posh-alias" -Scope CurrentUser -Force
}
# ----------------------------------------------------------------------------------- #
# eza
if (Get-Command eza -ErrorAction SilentlyContinue) {
Remove-Item Alias:ls -Force -ErrorAction SilentlyContinue
$_eza_params = '--icons --header --hyperlink --group --git -I="*NTUSER.DAT*|*ntuser.dat*" --group-directories-first'
Add-Alias ls "eza $_eza_params"
Add-Alias la "eza $_eza_params -al --time-style=relative --sort=modified"
Add-Alias ld "eza $_eza_params -lDa --show-symlinks" # lists only directories
Add-Alias lf "eza $_eza_params -lfa --show-symlinks" # lists only files (included hidden files)
Add-Alias ll "eza $_eza_params -lbhHigUmuSa" # Lists everything in details of date
Add-Alias lt "eza $_eza_params -lT" # Tree view of detailed information
Add-Alias tree "eza $_eza_params --tree" # Tree view
}
# windows file explorer
Add-Alias e 'Invoke-Item .'
# common locations
Add-Alias dotf "Set-Location $env:DOTFILES"
Add-Alias dotp "Set-Location $env:DOTPOSH"
Add-Alias home "Set-Location $env:USERPROFILE"
Add-Alias docs "Set-Location $env:USERPROFILE\Documents"
Add-Alias desktop "Set-Location $env:USERPROFILE\Desktop"
Add-Alias downloads "Set-Location $env:USERPROFILE\Downloads"
# network
Add-Alias flushdns 'ipconfig /flushdns'
Add-Alias displaydns 'ipconfig /displaydns'
Add-Alias chrome 'Start-Process chrome'
Add-Alias edge 'Start-Process microsoft-edge:'
# powershell reload /restart
# Source: - https://stackoverflow.com/questions/11546069/refreshing-restarting-powershell-session-w-out-exiting
if (Test-Path -Path $PROFILE) {
Add-Alias reload '. $PROFILE'
}
if (Test-Path -Path $PROFILE.CurrentUserAllHosts) {
Add-Alias reload '. $PROFILE.CurrentUserAllHosts'
}
Add-Alias restart 'Get-Process -Id $PID | Select-Object -ExpandProperty Path | ForEach-Object { Invoke-Command { & "$_" } -NoNewScope }'
# windows system
if (Get-Command fastfetch -ErrorAction SilentlyContinue) {
Add-Alias sysinfo 'fastfetch -c all'
} else {
Add-Alias sysinfo 'Get-ComputerInfo'
}
Add-Alias lock 'Invoke-Command { rundll32.exe user32.dll,LockWorkStation }'
Add-Alias hibernate 'shutdown.exe /h'
Add-Alias shutdown 'Stop-Computer'
Add-Alias reboot 'Restart-Computer'
Add-Alias paths '$env:PATH -Split ";"'
Add-Alias envs 'Get-ChildItem Env:'
Add-Alias profiles 'Get-PSProfile {$_.exists -eq "True"} | Format-List'
Add-Alias HKLM: 'Set-Location HKLM:'
Add-Alias HKCU: 'Set-Location HKCU:'
# List NPM (NodeJS) Global Packages
# To export global packages to a file, for-example: `npm-ls > global_packages.txt`
Add-Alias npm-ls '(npm ls -g | Select-Object -skip 1).Trim().Split() | ForEach-Object { if ($_ -match [regex]::Escape("@")) { Write-Output $_ } };'
Add-Alias bun-ls '(bun pm ls -g | Select-Object -Skip 1).Trim().Split() | ForEach-Object { if ($_ -match [regex]::Escape("@")) { Write-Output $_ } };'
Add-Alias pnpm-ls '(pnpm ls -g | Select-Object -Skip 5) | ForEach-Object { $name = $_.Split()[0]; $version = $_.Split()[1]; Write-Output "$name@$version" };'