Skip to content

Instantly share code, notes, and snippets.

@Ray-Eldath
Last active December 29, 2022 15:49
Show Gist options
  • Save Ray-Eldath/961824f60e4f7c55f9331f866fefafa8 to your computer and use it in GitHub Desktop.
Save Ray-Eldath/961824f60e4f7c55f9331f866fefafa8 to your computer and use it in GitHub Desktop.
Useful Powershell profile
#
# The top three lines are for my oh-my-posh and Terminus. Comment them out if you don't need.
#
# Actions defined:
# - ls: shorten ls.
# - open: locate to that directory if provided a directory, open the files if provide
# files. the file whose extension is specified in $global:openByNPP will opened
# by Notepad++ or any other program you set.
# - cd1, cd2: memorize the current path and change to this path at the next innovation.
# - sizeof: show the size of a file.
# - fq, ufq
#
# Author: Ray-Eldath <ray.eldath@outlook.com>. 2019.
#
Invoke-Expression (&starship init powershell)
function listFilesName {
Get-ChildItem -Name
}
$global:openByNPP = '.ps1', '.psm1', '.java', '.kt', '.scala', '.sc', '.gradle', '.properties', '.gitignore', '.html', '.css', '.less', '.json', '.yml', '.yaml', '.toml', ''
function openFile($path) {
if ($global:openByNPP -match [System.IO.Path]::GetExtension($path)) {
Start-Process notepad++ $path
echo 'open by Nodepad++'
return
}
echo 'open by default'
Invoke-Item $path
}
function openFiles {
if (!(Test-Path -Path $args[0] -PathType leaf)) { # is a folder
if ($args.length -ne 1) {
Write-Error -Message "Error: only one path can be supplied when opening a folder." -Category InvalidArgument
}
cd $args[0]
return
}
foreach ($e in $args) {
openFile $e
}
}
function fileSize() {
if ($args.length -ne 1) {
Write-Error -Message "Error: only one path can be supplied." -Category InvalidArgument
}
$arg = $args[0]
if (!(Test-Path -Path $arg -PathType leaf)) { # is a folder
'is a folder'
return
}
$size = [int] (Get-Item $arg).length
if ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
elseif ($size -gt 1GB) {[string]::format("{0:0.00} GB", $size / 1GB)}
elseif ($size -gt 1MB) {[string]::format("{0:0.00} MB", $size / 1MB)}
elseif ($size -gt 1KB) {[string]::format("{0:0.00} KB", $size / 1KB)}
elseif ($size -gt 0) {[string]::format("{0:0.00} B", $size)}
else { "" }
}
$global:unset = "<unset>"
$global:cd1 = '~\Documents\Powershell\.cd1'
$global:cd2 = $unset
function markDic1 {
if ($args.length -gt 1) {
Write-Error -Message "Error: only one argument can be sup." -Category InvalidArgument
}
if ($args[0] -eq "clear" -OR $args[0] -eq 'c') {
Remove-Item -Path $global:cd1
echo "file ${cd1} deleted."
return
}
if (Test-Path $global:cd1) {
$path = Get-Content -Path $global:cd1
cd $path
echo "cd by ${cd1} (${path})."
} else {
New-Item -Path $global:cd1 -ItemType File | Out-Null
$path = (Get-Location).Path
$path | Out-File -FilePath $global:cd1
"write ${path} into ${cd1}."
}
}
function markDic2 {
if ($args.length -gt 1) {
Write-Error -Message "Error: only one argument can be sup." -Category InvalidArgument
}
if ($args[0] -eq "clear" -OR $args[0] -eq 'c') {
$global:cd2 = $global:unset
echo 'cd2 cleared'
return
}
if ($global:cd2 -eq $global:unset) {
$global:cd2 = (Get-Location).Path
'set cd2 to ' + $global:cd2
} else {
cd $global:cd2
echo 'cd by cd2 (' + $global:cd2 + ')'
}
}
function touchAndOpen {
touch $args[0]
open $args[0]
}
function setProxies {
$proxy = "http://127.0.0.1:1080"
$env:http_proxy = $proxy
$env:https_proxy = $proxy
$env:all_proxy = $proxy
}
function unsetProxies {
Remove-Item Env:\http_proxy
Remove-Item Env:\https_proxy
Remove-Item Env:\all_proxy
}
Remove-Item alias:\ls
Remove-Item alias:\rm
Set-Alias sizeof fileSize
Set-Alias ls listFilesName
Set-Alias open openFiles
Set-Alias cd1 markDic1
Set-Alias cd2 markDic2
Set-Alias to touchAndOpen
Set-Alias fq setProxies
Set-Alias ufq unsetProxies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment