zhangjunlei26 / my-note

锁事小记
2 stars 0 forks source link

Microsoft.PowerShell_profile.ps1 #27

Open zhangjunlei26 opened 3 years ago

zhangjunlei26 commented 3 years ago
chcp 65001
#------------------------------- Import Modules BEGIN -------------------------------

# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
# Set-ExecutionPolicy Bypass -Scope CurrentUser
# Set-ExecutionPolicy Bypass -Scope LocalMachine
# Get-ExecutionPolicy -List

# Install-Module PSColor -Scope AllUsers -Force -Verbose
# Install-Module DirColors -Scope AllUsers -Force -Verbose
# Install-Module Get-ChildItemColor -Scope AllUsers -Force -Verbose
# Install-Module WindowsConsoleFonts -Scope AllUsers -Force -Verbose
# Install-Module posh-git -Scope AllUsers -Force -Verbose
# Install-Module PSReadLine -Scope AllUsers -Force -Verbose
# Install-Module git-aliases -Scope AllUsers -Force -Verbose
# Install-Module oh-my-posh -Force -Verbose -Scope CurrentUser -Proxy http://127.0.0.1:12639
# 引入 ps-read-line ,常用键盘映射如下

# 安装主大量主题
# https://github.com/JanDeDobbeleer/oh-my-posh/tree/main/themes
# Get-PoshThemes
# Update-Module

Import-Module PSColor
Import-Module DirColors
Import-Module posh-git
Import-Module Get-ChildItemColor
Import-Module WindowsConsoleFonts
# 引入 ps-read-line ,常用键盘映射如下
#https://docs.microsoft.com/zh-cn/powershell/module/psreadline/about/about_psreadline?view=powershell-7.1
Import-Module PSReadLine
# 引入git-aliases
Import-Module git-aliases -DisableNameChecking
$GitPromptSettings.EnableFileStatus = $true
$GitPromptSettings.EnablePromptStatus = $true
$GitPromptSettings.AnsiConsole = $true

# 设置类似linux终端快捷键
Set-PSReadlineOption -EditMode Emacs
Set-PSReadlineKeyHandler -Key 'ctrl+d' -Function ViExit
Set-PSReadlineKeyHandler -Key 'ctrl+l' -Function ClearScreen
# posh-windows-amd64.exe init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression
# oh-my-posh init pwsh --config "$env:POST_THEMES_PATH\\powerlevel10k_classic.omp.json" | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\\jandedobbeleer.omp.json" | Invoke-Expression
#------------------------------- Import Modules END -------------------------------

#-------------------------------  Set Hot-keys BEGIN  -------------------------------

# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History
# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# 设置 Tab 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
#Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
#Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#-------------------------------  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 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 Chocolotey
    Write-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyan
    choco outdated
}
#-------------------------------    Functions END     -------------------------------

#-------------------------------   Set Alias BEGIN    -------------------------------
#region mamba initialize
# !! Contents within this block are managed by 'mamba shell init' !!
$env:MAMBA_ROOT_PREFIX = "d:\tools\micromamba"
$env:MAMBA_EXE = "d:\tools\micromamba\micromamba.exe"
(& $env:MAMBA_EXE 'shell' 'hook' -s 'powershell' -p $env:MAMBA_ROOT_PREFIX) | Out-String | Invoke-Expression
#endregion

function ma { micromamba activate $Args[0] }
function mda { micromamba deactivate }

Set-Alias -name conda -value micromamba
fnm env --use-on-cd | Out-String | Invoke-Expression

# 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
Set-Alias l Get-ChildItemColor -option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope

# 4 目录及文件
function Make-Link { cmd /C mklink $args }
Set-Alias mklink Make-Link
function which($name) { Get-Command $name | Select-Object Definition }
function rmrf($item) { Remove-Item $item -Recurse -Force }
function mkfile($file) { "" | Out-File $file -Encoding ASCII }

# 5. 打开当前工作目录
function OpenCurrentFolder {
    param
    (
        # 输入要打开的路径
        # 用法示例:open C:\
        # 默认路径:当前工作文件夹
        $Path = '.'
    )
    Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder

function Open-Current-Directory {
    explorer .
}
Set-Alias e. Open-Current-Directory

function Enter-Sources {
    cd e:\rail_ws\
}
Set-Alias src Enter-Sources
#-------------------------------    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     -------------------------------