Skip to content

Instantly share code, notes, and snippets.

@LittleNewton
Created March 14, 2023 16:16
Show Gist options
  • Save LittleNewton/263f742b2612842403740a77d5610039 to your computer and use it in GitHub Desktop.
Save LittleNewton/263f742b2612842403740a77d5610039 to your computer and use it in GitHub Desktop.
Shared on-my-posh
<#
* FileName: Microsoft.PowerShell_profile.ps1 / PowerShell configuration file.
* Author: 刘 鹏
* Email: littleNewton6@gmail.com
* Created Date: 2021, Aug. 21
* Updated Date: 2023, Mar. 15
* Copyright: No copyright. You can use this code for anything with no warranty.
#>
#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git
# 引入 ps-read-line
Import-Module PSReadLine
# 设置 PowerShell 主题,需要先安装 oh-my-posh.exe (https://ohmyposh.dev/docs/installation/windows)
oh-my-posh init pwsh --config $env:POSH_THEMES_PATH\paradox.omp.json | Invoke-Expression
#------------------------------- Import Modules END -------------------------------
#------------------------------- Set Hot-keys BEGIN -------------------------------
# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History
# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# 设置 Tab 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
# 回到行首/行尾
Set-PSReadlineKeyHandler -Key "Ctrl+a" -Function BeginningOfLine
Set-PSReadlineKeyHandler -Key "Ctrl+e" -Function EndOfLine
# 前进/后退一个单词
Set-PSReadlineKeyHandler -Key 'Alt+f' -Function ShellForwardWord
Set-PSReadlineKeyHandler -Key 'Alt+b' -Function ShellBackwardWord
# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
# 启用预测性 IntelliSense
Set-PSReadLineOption -PredictionSource History
#------------------------------- Set Hot-keys END -------------------------------
#------------------------------- Functions BEGIN -------------------------------
# Python 直接执行
$env:PATHEXT += ";.py"
# 更新系统组件
function Update-Packages {
# update pip
# Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan
# $a = pip list --outdated
# $num_package = $a.Length - 2
# for ($i = 0; $i -lt $num_package; $i++) {
# $tmp = ($a[2 + $i].Split(" "))[0]
# pip install -U $tmp
# }
# update conda
Write-Host "Step 1: 更新 Anaconda base 虚拟环境" -ForegroundColor Magenta -BackgroundColor Cyan
conda activate base
conda upgrade python
# update TeX Live
$CurrentYear = Get-Date -Format yyyy
Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan
tlmgr update --self
tlmgr update --all
# update Apps using winget
Write-Host "Step 3: 通过 winget 更新 Windows 应用程序" -ForegroundColor Magenta -BackgroundColor Cyan
winget upgrade
# winget upgrade --all
}
#------------------------------- Functions END -------------------------------
#------------------------------- Set Alias BEGIN -------------------------------
# 1. 编译函数 make
function MakeThings {
nmake.exe $args -nologo
}
Set-Alias -Name make -Value MakeThings
# 2. 更新系统 os-update
Set-Alias -Name os-update -Value Update-Packages
# 3. 查看目录 ls & ll
function ListDirectory {
(Get-ChildItem).Name
Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem
# 4. 打开当前工作目录
function OpenCurrentFolder {
param
(
# 输入要打开的路径
# 用法示例:open C:\
# 默认路径:当前工作文件夹
$Path = '.'
)
Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder
# 5. 更改工作目录
function Change-Directory {
param (
# 输入要切换到的路径
# 用法示例:cd D:/
# 默认路径:C 盘的桌面
$Path = '$env:USERPROFILE\Desktop'
)
Set-Location $Path
}
Set-Alias -Name cd -Value Change-Directory -Option AllScope
#------------------------------- Set Alias END -------------------------------
#------------------------------- Set Network BEGIN -------------------------------
# 1. 获取所有 Network Interface
function Get-AllNic {
Get-NetAdapter | Sort-Object -Property MacAddress
}
Set-Alias -Name getnic -Value Get-AllNic
# 2. 获取 IPv4 关键路由
function Get-IPv4Routes {
Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'}
}
Set-Alias -Name getip -Value Get-IPv4Routes
# 3. 获取 IPv6 关键路由
function Get-IPv6Routes {
Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'}
}
Set-Alias -Name getip6 -Value Get-IPv6Routes
#------------------------------- Set Network END -------------------------------
{
"$help": "https://aka.ms/terminal-documentation",
"$schema": "https://aka.ms/terminal-profiles-schema",
"actions":
[
{
"command":
{
"action": "newTab",
"index": 3
},
"keys": "ctrl+shift+4"
},
{
"command":
{
"action": "adjustFontSize",
"delta": 1.0
},
"keys": "ctrl+plus"
},
{
"command":
{
"action": "switchToTab",
"index": 5
},
"keys": "ctrl+alt+6"
},
{
"command":
{
"action": "nextTab"
},
"keys": "ctrl+tab"
},
{
"command":
{
"action": "switchToTab",
"index": 4
},
"keys": "ctrl+alt+5"
},
{
"command":
{
"action": "moveFocus",
"direction": "left"
},
"keys": "alt+left"
},
{
"command":
{
"action": "switchToTab",
"index": 8
},
"keys": "ctrl+alt+9"
},
{
"command":
{
"action": "resizePane",
"direction": "up"
},
"keys": "alt+shift+up"
},
{
"command":
{
"action": "moveFocus",
"direction": "up"
},
"keys": "alt+up"
},
{
"command": "duplicateTab",
"keys": "ctrl+shift+d"
},
{
"command":
{
"action": "newTab",
"index": 1
},
"keys": "ctrl+shift+2"
},
{
"command": "paste",
"keys": "ctrl+shift+v"
},
{
"command": "paste",
"keys": "shift+insert"
},
{
"command":
{
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
},
"keys": "alt+shift+d"
},
{
"command":
{
"action": "adjustFontSize",
"delta": -1.0
},
"keys": "ctrl+minus"
},
{
"command":
{
"action": "newTab",
"index": 6
},
"keys": "ctrl+shift+7"
},
{
"command":
{
"action": "newTab"
},
"keys": "ctrl+n"
},
{
"command":
{
"action": "switchToTab",
"index": 1
},
"keys": "ctrl+alt+2"
},
{
"command": "find",
"keys": "ctrl+f"
},
{
"command":
{
"action": "resizePane",
"direction": "left"
},
"keys": "alt+shift+left"
},
{
"command":
{
"action": "newTab",
"index": 8
},
"keys": "ctrl+shift+9"
},
{
"command":
{
"action": "prevTab"
},
"keys": "ctrl+shift+tab"
},
{
"command":
{
"action": "switchToTab",
"index": 2
},
"keys": "ctrl+alt+3"
},
{
"command":
{
"action": "splitPane",
"split": "right"
},
"keys": "alt+shift+plus"
},
{
"command":
{
"action": "moveFocus",
"direction": "down"
},
"keys": "alt+down"
},
{
"command":
{
"action": "copy",
"singleLine": false
},
"keys": "ctrl+shift+c"
},
{
"command":
{
"action": "copy",
"singleLine": false
},
"keys": "ctrl+insert"
},
{
"command":
{
"action": "splitPane",
"split": "down"
},
"keys": "alt+shift+minus"
},
{
"command": "scrollUpPage",
"keys": "ctrl+shift+pgup"
},
{
"command":
{
"action": "scrollUp"
},
"keys": "ctrl+shift+up"
},
{
"command": "resetFontSize",
"keys": "ctrl+0"
},
{
"command": "scrollDownPage",
"keys": "ctrl+shift+pgdn"
},
{
"command":
{
"action": "splitPane",
"split": "auto"
},
"keys": "alt+shift+\\"
},
{
"command":
{
"action": "switchToTab",
"index": 6
},
"keys": "ctrl+alt+7"
},
{
"command":
{
"action": "openSettings"
},
"keys": "ctrl+;"
},
{
"command":
{
"action": "newTab",
"index": 0
},
"keys": "ctrl+shift+1"
},
{
"command":
{
"action": "closeTab"
},
"keys": "alt+w"
},
{
"command":
{
"action": "moveFocus",
"direction": "right"
},
"keys": "alt+right"
},
{
"command":
{
"action": "splitPane",
"profile": "Ubuntu-20.04",
"split": "down"
},
"keys": "alt+shift+u"
},
{
"command":
{
"action": "newTab",
"index": 4
},
"keys": "ctrl+shift+5"
},
{
"command":
{
"action": "newTab",
"index": 2
},
"keys": "ctrl+shift+3"
},
{
"command":
{
"action": "switchToTab",
"index": 7
},
"keys": "ctrl+alt+8"
},
{
"command":
{
"action": "resizePane",
"direction": "down"
},
"keys": "alt+shift+down"
},
{
"command":
{
"action": "resizePane",
"direction": "right"
},
"keys": "alt+shift+right"
},
{
"command":
{
"action": "switchToTab",
"index": 3
},
"keys": "ctrl+alt+4"
},
{
"command":
{
"action": "scrollDown"
},
"keys": "ctrl+shift+down"
},
{
"command": "closePane",
"keys": "alt+shift+w"
},
{
"command":
{
"action": "newTab",
"index": 5
},
"keys": "ctrl+shift+6"
},
{
"command":
{
"action": "newTab",
"index": 7
},
"keys": "ctrl+shift+8"
},
{
"command":
{
"action": "switchToTab",
"index": 0
},
"keys": "ctrl+alt+1"
}
],
"alwaysShowTabs": true,
"confirmCloseAllTabs": false,
"copyFormatting": "none",
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"disabledProfileSources":
[
"Windows.Terminal.Azure"
],
"focusFollowMouse": false,
"initialCols": 135,
"initialPosition": "1195,631",
"initialRows": 45,
"profiles":
{
"defaults": {},
"list":
[
{
"closeOnExit": "graceful",
"colorScheme": "Homebrew",
"commandline": "C:/Program Files/PowerShell/7/pwsh.exe -nologo",
"cursorColor": "#FFFFFF",
"cursorShape": "emptyBox",
"font":
{
"face": "FiraCode NF",
"size": 11.0
},
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"historySize": 9001,
"icon": "C:/Program Files/PowerShell/7/assets/Powershell_av_colors.ico",
"name": "PowerShell 7",
"padding": "8",
"snapOnInput": true,
"source": "Windows.Terminal.PowershellCore",
"startingDirectory": ".",
"tabTitle": "PowerShell"
},
{
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"hidden": true,
"name": "Command Prompt"
},
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": true,
"name": "Windows PowerShell"
}
]
},
"schemes":
[
{
"background": "#0C0C0C",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#012456",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell Powershell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#283033",
"black": "#000000",
"blue": "#6666E9",
"brightBlack": "#666666",
"brightBlue": "#00A2FF",
"brightCyan": "#53FFFF",
"brightGreen": "#00D900",
"brightPurple": "#FF08FF",
"brightRed": "#FF6060",
"brightWhite": "#E5E5E5",
"brightYellow": "#FDFF73",
"cursorColor": "#FFFFFF",
"cyan": "#00A6B2",
"foreground": "#00FF00",
"green": "#00A600",
"name": "Homebrew",
"purple": "#FF00FF",
"red": "#FC5275",
"selectionBackground": "#FFFFFF",
"white": "#BFBFBF",
"yellow": "#FFFF00"
},
{
"background": "#282C34",
"black": "#282C34",
"blue": "#61AFEF",
"brightBlack": "#5A6374",
"brightBlue": "#61AFEF",
"brightCyan": "#56B6C2",
"brightGreen": "#98C379",
"brightPurple": "#C678DD",
"brightRed": "#E06C75",
"brightWhite": "#DCDFE4",
"brightYellow": "#E5C07B",
"cursorColor": "#FFFFFF",
"cyan": "#56B6C2",
"foreground": "#DCDFE4",
"green": "#98C379",
"name": "One Half Dark",
"purple": "#C678DD",
"red": "#E06C75",
"selectionBackground": "#FFFFFF",
"white": "#DCDFE4",
"yellow": "#E5C07B"
},
{
"background": "#FAFAFA",
"black": "#383A42",
"blue": "#0184BC",
"brightBlack": "#4F525D",
"brightBlue": "#61AFEF",
"brightCyan": "#56B5C1",
"brightGreen": "#98C379",
"brightPurple": "#C577DD",
"brightRed": "#DF6C75",
"brightWhite": "#FFFFFF",
"brightYellow": "#E4C07A",
"cursorColor": "#4F525D",
"cyan": "#0997B3",
"foreground": "#383A42",
"green": "#50A14F",
"name": "One Half Light",
"purple": "#A626A4",
"red": "#E45649",
"selectionBackground": "#FFFFFF",
"white": "#FAFAFA",
"yellow": "#C18301"
},
{
"background": "#002B36",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#FFFFFF",
"cyan": "#2AA198",
"foreground": "#839496",
"green": "#859900",
"name": "Solarized Dark",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#FFFFFF",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#FDF6E3",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#002B36",
"cyan": "#2AA198",
"foreground": "#657B83",
"green": "#859900",
"name": "Solarized Light",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#FFFFFF",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#FFFFFF",
"cyan": "#06989A",
"foreground": "#D3D7CF",
"green": "#4E9A06",
"name": "Tango Dark",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#FFFFFF",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#FFFFFF",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#000000",
"cyan": "#06989A",
"foreground": "#555753",
"green": "#4E9A06",
"name": "Tango Light",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#FFFFFF",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#000080",
"brightBlack": "#808080",
"brightBlue": "#0000FF",
"brightCyan": "#00FFFF",
"brightGreen": "#00FF00",
"brightPurple": "#FF00FF",
"brightRed": "#FF0000",
"brightWhite": "#FFFFFF",
"brightYellow": "#FFFF00",
"cursorColor": "#FFFFFF",
"cyan": "#008080",
"foreground": "#C0C0C0",
"green": "#008000",
"name": "Vintage",
"purple": "#800080",
"red": "#800000",
"selectionBackground": "#FFFFFF",
"white": "#C0C0C0",
"yellow": "#808000"
}
],
"tabWidthMode": "equal",
"theme": "system",
"themes": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment