-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.ps1
315 lines (255 loc) · 8.86 KB
/
default.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
$version = "1.0.5"
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal $identity
$isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
Remove-Variable identity
Remove-Variable principal
# Conda Integration
If (Test-Path "C:\ProgramData\miniconda3\Scripts\conda.exe") {
(& "C:\ProgramData\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
Function Test-CommandExists {
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
try { if (Get-Command $command) { RETURN $true } }
Catch { Write-Host "$command does not exist"; RETURN $false }
Finally { $ErrorActionPreference = $oldPreference }
}
# Editor Setup ---------------------------------------------------------------------------------
if (Test-CommandExists nvim) {
$EDITORCL='nvim'
} elseif (Test-CommandExists pvim) {
$EDITORCL='pvim'
} elseif (Test-CommandExists vim) {
$EDITORCL='vim'
} elseif (Test-CommandExists vi) {
$EDITORCL='vi'
}
if (Test-CommandExists code) {
$EDITOR='code'
} elseif (Test-CommandExists notepad) {
$EDITOR='notepad++'
} elseif (Test-CommandExists notepad++) {
$EDITOR='sublime_text'
} elseif (Test-CommandExists sublime_text) {
$EDITOR='notepad'
}
if ($EDITORCL){
Set-Alias -Name vim -Value $EDITORCL
}
Set-Alias -Name gedit -Value $EDITOR
Set-Alias -Name n -Value notepad
# Useful shortcuts for traversing directories---------------------------------------------------------------------
function cd... { Set-Location ..\.. }
function cd.... { Set-Location ..\..\.. }
function exp { explorer.exe . }
# Compute file hashes - useful for checking successful downloads -------------------------------------------------
function md5 { Get-FileHash -Algorithm MD5 $args }
function sha1 { Get-FileHash -Algorithm SHA1 $args }
function sha256 { Get-FileHash -Algorithm SHA256 $args }
# Drive shortcuts ------------------------------------------------------------------------------------------------
function HKLM: { Set-Location HKLM: }
function HKCU: { Set-Location HKCU: }
function Env: { Set-Location Env: }
# Creates drive shortcut for Work Folders, if current user account is using it
if (Test-Path "$env:USERPROFILE\Work Folders") {
New-PSDrive -Name Work -PSProvider FileSystem -Root "$env:USERPROFILE\Work Folders" -Description "Work Folders"
function Work: { Set-Location Work: }
}
# Set up command prompt and window title. Use UNIX-style convention for identifying
# whether user is elevated (root) or not. Window title shows current version of PowerShell
# and appends [ADMIN] if appropriate for easy taskbar identification
# Setup Prompt Style ------------------------------------------------------------------------------------------------
function prompt {
if (Test-Path .git) {
git branch | ForEach-Object {
if ($_ -match "^\*(.*)") {
Write-Host ("(" + $matches[1].Trim() + ")") -nonewline -foregroundcolor white -BackgroundColor red
}
}
}
if (Test-CommandExists conda){
Write-Host ($env:CONDA_PROMPT_MODIFIER.Trim()) -nonewline -foregroundcolor darkblue -BackgroundColor DarkYellow -ErrorAction SilentlyContinue
}
if ($isAdmin) {
Write-Host ("[" + (Get-Location) + "]") -nonewline -foregroundcolor black -BackgroundColor red
"# "
}
else {
Write-Host ("[" + (Get-Location) + "]") -nonewline -foregroundcolor black -BackgroundColor Blue
"$ "
}
}
# Setup Window title ------------------------------------------------------------------------------------------------
$Host.UI.RawUI.WindowTitle = "PowerShell {0}" -f $PSVersionTable.PSVersion.ToString()
if ($isAdmin) {
$Host.UI.RawUI.WindowTitle += " [ADMIN]"
}
# Does the the rough equivalent of dir /s /b. For example, dirs *.png is dir /s /b *.png
function dirs {
if ($args.Count -gt 0) {
Get-ChildItem -Recurse -Include "$args" | Foreach-Object FullName
}
else {
Get-ChildItem -Recurse | Foreach-Object FullName
}
}
# Simple function to start a new elevated process. If arguments are supplied then
# a single command is started with admin rights; if not then a new admin instance
# of PowerShell is started.
# Set UNIX-like aliases for the admin command, so sudo <command> will run the command
# with elevated rights.
function admin {
if ($args.Count -gt 0) {
$argList = "& '" + $args + "'"
Start-Process "$psHome\pwsh.exe" -Verb runAs -ArgumentList $argList
}
else {
Start-Process "$psHome\pwsh.exe" -Verb runAs
}
}
Set-Alias -Name su -Value admin
Set-Alias -Name sudo -Value admin
# Proflie Utils -------------------------------------------------------------------------------------------------------------
# Retrieve profile version
function profile-version{
Write-host "Powershell Tweaks Version " + $version
Write-host "Licenced under The UNLICENSE."
Write-host "This is free and unencumbered software released into the public domain."
}
# Make it easy to edit this profile once it's installed
function Edit-Profile {
if ($host.Name -match "ise") {
$psISE.CurrentPowerShellTab.Files.Add($profile.CurrentUserAllHosts)
}
else {
gedit $profile
}
}
function reload-profile {
& $PROFILE
}
function upgrade-profile {
$Message = "The upgrade overrrides the profile with the latest version in web. All the changes you made locally will be lost are you sure you want to continue?"
do {
Write-Host $Message -ForegroundColor Yellow
$response = Read-Host "Press 'Y' to continue or any other key to exit: "
if ($response -ne 'Y') {
Write-Host "Execution stopped." -ForegroundColor Red
return # Exit the function if input is not 'Y'
}
} while ($response -ne 'Y')
Write-Host "Updating the profile..."
Invoke-RestMethod https://github.com/dlsathvik04/PowerShell-Tweaks/raw/main/default.ps1 -OutFile $PROFILE
Invoke-Command { & "pwsh.exe"} -NoNewScope
}
function uninstall-profile {
rm $PROFILE
Invoke-Command { & "pwsh.exe"} -NoNewScope
}
# We don't need these any more; they were just temporary variables to get to $isAdmin.
# Delete them to prevent cluttering up the user profile.
function ll { Get-ChildItem -Path $pwd -File }
function gcom {
git add .
git commit -m "$args"
}
function gpush {
git add .
git commit -m "$args"
git push
}
function Get-PubIP {
(Invoke-WebRequest http://ifconfig.me/ip ).Content
}
function find-file($name) {
Get-ChildItem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object {
$place_path = $_.directory
Write-Output "${place_path}\${_}"
}
}
function unzip ($file) {
Write-Output("Extracting", $file, "to", $pwd)
$fullFile = Get-ChildItem -Path $pwd -Filter .\cove.zip | ForEach-Object { $_.FullName }
Expand-Archive -Path $fullFile -DestinationPath $pwd
}
function ix ($file) {
curl.exe -F "f:1=@$file" ix.io
}
Set-Alias -Name grep -Value findstr
function df {
get-volume
}
function sed($file, $find, $replace) {
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function export-global($name, $value) {
if ($isAdmin) {
[Environment]::SetEnvironmentVariable($name, $value, 'Machine')
}
else {
[Environment]::SetEnvironmentVariable($name, $value, 'User')
}
}
Set-Alias -Name pkill -Value Stop-Process
Set-Alias -Name touch -Value New-Item
function pgrep($name, $id, $search, $port) {
if ($id) {
Get-Process -Id $id
}
elseif ($search) {
Get-Process *$search*
}
elseif ($port) {
Get-NetTCPConnection -LocalPort $port | Get-Process -Id { $_.OwningProcess }
}
else {
if ($name){
Get-Process $name
}else{
Get-Process
}
}
}
function source($dir) {
$Env:Path += ";$dir"
}
# C and Cpp Utils -----------------------------------------------------------------------------------------------------
function run{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $path,
[Parameter()]
[switch] $c,
[Parameter()]
[switch] $cpp,
[Parameter()]
[switch] $t
)
if ($c) {
gcc $path -o runc.exe
.\runc.exe
} elseif($cpp){
g++ $path -o runc.exe
.\runc.exe
}
else {
Write-Host "No Compiler specified"
}
if ($t){
rm .\runc.exe
}
}
if (Test-CommandExists mingw32-make){
Set-Alias -Name make -Value mingw32-make
}
# Manage path here
# source <Path>