logo 🤗

技术视野

聚焦科技前沿,分享技术解析,洞见未来趋势。在这里,与您一起探索人工智能的无限可能,共赴技术盛宴。

  1. 从 PowerShellGallery 中安装。依次安装PowerShellGet, PSReadLine。(需要管理员权限打开PowerShell)
Install-Module -Name PowerShellGet -Force
Install-Module PSReadLine -Force
  1. 查看已安装的模块 ,确保PSReadLine 大于等于2.2.0
Get-InstalledModule

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.4.7                PackageManagement                   PSGallery            PackageManagement (a.k.a. OneGet) is a...
2.2.5                PowerShellGet                       PSGallery            PowerShell module with commands for di...
1.1.0                PowerTab                            PSGallery            A module that enhances PowerShell's ta...
2.2.5                PSReadLine                          PSGallery            Great command line editing in the Powe...
1.0.2                windows-screenfetch                 PSGallery            Powershell port of the Bash Screenshot...
6.29.1               oh-my-posh                          PSGallery            A prompt theme engine for any shell
1.0.0                posh-git                            PSGallery            Provides prompt with Git status summar...
  1. 安装各个软件,并添加到path路径。例如:
  • lsd
  • vim
  • winget
  • fd
  • nvim
  • proces
  • python
  • rg
  • choose
  1. 编辑配置文件
vim $PROFILE

# 没装vim可以用notepad $PROFILE
  1. 配置文件内容如下:
Import-Module posh-git # 引入 posh-git 
Import-Module oh-my-posh # 引入 oh-my-posh 
Import-Module PSReadLine
Set-PoshPrompt -Theme ys # 设置主题为 Paradox 
Set-PSReadLineOption -EditMode Emacs  #  Emacs 键绑定, 类似Linux
Set-PSReadLineOption -PredictionSource History
# 下面两行可选,用处不大
Set-PSReadLineKeyHandler -Chord F8 HistorySearchBackward # 设置F8向后补全
Set-PSReadLineKeyHandler -Chord Shift+F8 HistorySearchForward # 设置Shift+F8向前补全

# Chocolatey profile 
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" 
if (Test-Path($ChocolateyProfile)) {
    Import-Module "$ChocolateyProfile" 
}
# 激活conda gea(可选)
conda activate gea
# 把winget换成apt
# Set-Alias apt winget
# 删除ls,cat的别名
Remove-Item alias:\ls
Remove-Item alias:\cat
# 重新给ls, cat立别名
Set-Alias ls lsd
Set-Alias cat bat
set-Alias find fd
Set-Alias df duf
Set-Alias yum choco
Set-Alias gcc clang
Set-Alias g++ clang++
# 设置vim 默认为nvim
Set-Alias vim nvim
Set-Alias grep rg
# 重注册ps命令
Remove-Item alias:\ps
Set-Alias ps procs
Set-Alias awk choose
Set-Alias sed sd
Set-Alias python3 python


# test(用于优化tab功能)
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)
        [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
        $Local:word = $wordToComplete.Replace('"', '""')
        $Local:ast = $commandAst.ToString().Replace('"', '""')
        winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}
  • 新Powershell配置
Import-Module posh-git # 引入 posh-git 
# Import-Module oh-my-posh # 引入 oh-my-posh(PowerShell不需要了)
Import-Module PSReadLine
# 初始化主题路径
# 获取所有主题
# Get-PoshThemes
# Set-PoshPrompt -Theme ys # 设置主题为ys
# 新设置主题方法
oh-my-posh init pwsh --config C:\Users\18826\AppData\Local\oh-my-posh\themes\ys.omp.json | Invoke-Expression
Set-PSReadLineOption -EditMode Emacs  #  Emacs 键绑定, 类似Linux
Set-PSReadLineOption -PredictionSource History
# 下面两行可选,用处不大
Set-PSReadLineKeyHandler -Chord F8 HistorySearchBackward # 设置F8向后补全
Set-PSReadLineKeyHandler -Chord Shift+F8 HistorySearchForward # 设置Shift+F8向前补全
#
# Chocolatey profile 
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" 
  if (Test-Path($ChocolateyProfile)) {
     Import-Module "$ChocolateyProfile" 
    }
# 激活conda gea(可选)
conda activate gea
# 把winget换成apt
# Set-Alias apt winget
# 删除ls,cat的别名
Remove-Item alias:\ls
Remove-Item alias:\cat
# 重新给ls, cat立别名
Set-Alias ls lsd
Set-Alias cat bat
set-Alias find fd
Set-Alias df duf
Set-Alias yum choco
Set-Alias gcc clang
Set-Alias g++ clang++
# 设置vim 默认为nvim
Set-Alias vim nvim
Set-Alias grep rg
# 重注册ps命令
Remove-Item alias:\ps
Set-Alias ps procs
Set-Alias awk choose
Set-Alias sed sd
Set-Alias python3 python


# test(用于优化tab功能)
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)
            [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
                    $Local:word = $wordToComplete.Replace('"', '""')
                            $Local:ast = $commandAst.ToString().Replace('"', '""')
                                    winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
                                                [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
                                                        }
                                                        }

将powershell启动加入病毒防护白名单,加快启动速度

  • 需要加入的路径
C:\Program Files (x86)\PowerShell\7-preview\pwsh.exe

C:\Users\18826\AppData\Local\Programs\oh-my-posh\bin\oh-my-posh.exe

版权属于:tlntin
作品采用:本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
更新于: 2023年05月26日 14:32


39 文章数
5 分类数
40 页面数
已在风雨中度过 1年160天12小时13分
目录
来自 《Windows配置PowerShell》
暗黑模式
暗黑模式
返回顶部
暗黑模式
暗黑模式
返回顶部