byodian's blog

Powershell 终端配置

·

请使用 Windwos 操作系统测试本文涉及的命令。

探索 Powershell

常用命令

  1. Install-Module posh-git -Scope CurrentUser -Force - Install a module by name

  2. Uninstall-Module -Name oh-my-posh - Uninstall a module by name

  3. Get-InstalledModule -Name oh-my-posh | Uninstall-Module - Use the pipeline to uninstall a module

  4. display all environment variable

    1. Get-ChildItem -Path Env:
    2. dir Env:
    3. ls Env:
  5. setx Path "$Env:Path;C:\Program Files\nodejs\node.exe" permanently add node to the environment variable of the Path

  6. [guid]::NewGuid() generate a Universally Unique Identifier

配置 PowerShell

配置过程分两部分,基础配置和自定义配置两部分。在基础配置中我们会创建一些必要的 PowerShell 配置文件,包括 PowerShell 当前用户配置和自定义配置两个文件,而在自定义配置阶段,我们将会安装一些必要的插件以及编写 PowerShell 自定义配置文件。

注意:PowerShell 版本必须在 7.2 及以上

基础配置

  1. 安装 PowerShell微软商店入口

  2. 安装 Windows terminal

  3. 安装 scoop - A command-line installer for Windows

    打开一个 PowerShell,运行命令

    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
    irm get.scoop.sh | iex
  4. 安装 Winget - Install and manage applications

  5. 安装 git for windows - Offer a lightweight, native set of tools

    winget install -e --id Git.Git
  6. 安装 gccneovim

    scoop install gcc neovim
  7. 创建 PowerShell 配置文件

    创建 $PROFILE.CurrentUserCurrentHost 文件,生成的文件路径为$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

    # Create a profile
    New-Item -Path $PROFILE.CurrentUserCurrentHost -ItemType "file" -Force

    # or use notepad commond
    notepad $PROFILE

    创建 PowerSehll 自定义配置文件 user_profile.ps1

    # $Home\Documents\PowerShell 获取文件的上级路径 
    Split-Path $PROFILE.CurrentUserCurrentHOST

    mkdir $Home\.config\powershell
    New-Item $Home\.config\powershell\user_profile.ps1

    要使自定义配置文件生效需要在 $PROFILE.CurrentUserCurrentHost 文件中添加以下内容,并重新启动终端。

    # $Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
    . $env:USEPROFILE\.config\powershell\user_profile.ps1

    The $PROFILE variable 👉 about_profiles

    This variable stores the path to the "Current User, Current Host" profile. The other profiles are saved in note properties of the $PROFILE variable. For example:

    host-specific profilesvariable
    Current User, Current Host$PROFILE
    Current User, Current Host$PROFILE.CurrentUserCurrentHost
    Current User, All Hosts$PROFILE.CurrentUserAllHosts``
    All Users, Current Host$PROFILE.AllUsersCurrentHost
    All Users, All Hosts$PROFILE.AllUsersAllHosts

    以上所有的操作成功后,表示基础的配置准备已经完成,接下来进入安装插件阶段。

自定义配置(安装插件)

  1. 配置 prompt,下载 oh-my-posh (A prompt theme engine for any shell),安装步骤可以参考 oh-my-posh 官网安装教程

    • Prompt for Git repositories

    打开 PowerShell 终端,输入命令

    winget install JanDeDobbeleer.OhMyPosh -s winget
    # or
    scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json

    安装完成后,重启你的 PowerShell 终端,开始配置 prompt 主题。编辑之前创建的自定义配置文件,终端输入下面命令可打开此文件

    notepad $Home\.config\powershell\user_profile.ps1

    然后在配置文件中键入以下内容,开启主题的使用。Oh My Posh 内建了许多开箱即用的主题,这里我选择 robbyrussel 主题。

    oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussel.omp.json" | Invoke-Expression

    重启 PowerShell 终端,查看 robbyrussel 主题是否生效。

    你也可以自定义主题样式,详细步骤请参考Customize

  2. 安装 posh-git

    posh-git 是一个 PowerShell 模块,整合 git 和 PowerShell,提供 git 状态信息,此状态信息可以展示在终端 prompt 中,也提供 tab 键自动补全 git 命令、分支名称和路径等支持。安装步骤参考posh-git installation,简单步骤如下:

    # Installing posh-git via PowerShellGet
    PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force

    # or Installing posh-git via Chocolatey
    choco install poshgit

    # or Installing posh-git via Scoop
    scoop bucket add extras
    scoop install posh-git

    在自定义配置文件中添加一行,重启终端即可生效。

    # user_profile.ps1
    Import-Module posh-git
  3. 安装 terminal icons

    一个展示文件和文件夹的图标的 PowerShell 模块。

    此模块必须依赖 Nerd Fonts 字体,在 download 页面选择一款字体,下载并安装字体。

    在终端管理员模式下输入命令安装 Terminal-Icons 模块

    # Install-Module
    Install-Module -Name Terminal-Icons -Repository PSGallery -Force

    # or use scoop
    scoop bucket add extras
    scoop install terminal-icons

    添加下面的内容到自定义配置文件

    # user_profile.ps1
    Import-Module Terminal-Icons
  4. 安装 z

    基于你的 cd 命令历史,z 让你在 PowerShell 中快速导航文件系统。在终端管理员模式下输入命令安装 z 模块

    Install-Module -Name z -Force -AllowClobber 

    配置自定义文件,重启终端

    # user_profile.ps1
    Import-Module z
  5. 安装 PSReadLine - autocompletion

    How to use PSReadLine

    Install-Module -Name PowerShellGet -Force
    Install-Module -Name PSReadLine -Force -SkipPublisherCheck

    To start using, just import the module:

    Import-Module PSReadLine

    To view the current key bindings:

    Get-PSReadLineKeyHandler

    To use Emacs key bindings, you can use:

    Set-PSReadLineOption -EditMode Emacs

    Specifies the source for PSReadLine to get predictive suggestions.

    Set-PSReadLineOption -PredictionSource History

    Sets the style for the display of the predictive text. The default is ListView.

    Set-PSReadLineOption -PredictionViewStyleInlineView

    # user_profile.ps1
    Import-Module PSReadLine
    Set-PSReadLineOption -EditMode Emacs
    Set-PSReadLineOption -PredictionSource HisoryAndPlugin
    Set-PSReadLineOption -PredictionViewStyle ListView
  6. 安装 fzf PSFzf

    PSFzf is a PowerShell module that wraps fzf, a fuzzy file finder for the command line. Failed to import module

    # 安装 fzf
    scoop install fzf
    # 安装 PSFzf
    Install-Module -Name PSFzf -RequiredVersion 2.0.0 -Scope CurrentUser -Force

    自定义文件添加下面的内容

    Import-Module PSFzf
    Set-PsFzfOption -PSReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'
  7. 重启配置文件

. $PROFILE

user_profile.ps1 自定义配置文件

Import-Module posh-git
Import-Module Terminal-Icons
Import-Module z
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussel.omp.json" | Invoke-Expression

# PSReadLine
Import-Module PSReadLine
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle InlineView

# fzf
Import-Module PSFzf
Set-PsFzfOption -PSReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'

# Alias
Set-Alias ll ls
Set-Alias c clear
Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe'
Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe'

function which($command) {
Get-command -Name $command -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

资源

参考