程序员的windows powershell配置
程序员的windows powershell配置
将Windows终端配置成类似于配置了ohmyzsh
的Linux/MacOS的终端一样
安装Scoop
scoop是一个windows软件包的管理工具,类似于MacOS的Homebrew,Ubuntu的APT。这边列一下scoop的基本安装,更详细和高级的使用请参考scoop官网,顺便提一下安装scoop需要翻墙
-
打开PowerShell终端,输入并运行下面的命令
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time irm get.scoop.sh | iex
-
成功安装scoop后,为scoop添加bucket
# 首先安装git, 因为给scoop添加bucket(可理解为软件仓库)的时候需要git scoop install git # 给scoop添加main bucket scoop bucket add main # 给scoop添加extras bucket scoop bucket add extras
-
scoop一些常用的命令
# 更新bucket仓库 scoop update # 更新软件 scoop update software_name1 software_name2 ... # 清理app scoop cleanup -a
PowerShell的配置文件
PowerShell的配置文件是在C:\%USERPROFILE%\Documents\WindowsPowerShell
目录下名字为Microsoft.PowerShell_profile.ps1
的文件,如果没有这个文件,可以先创建一下
安装PSReadLine
用于记录、提示历史命令,功能类似于ohmyzsh
的zsh-autosuggestions
插件,详情参考它的Github仓库,安装方法截取如下,命令均在PowerShell终端中输入
Install-Module -Name PowerShellGet -Force
Exit
Install-Module PSReadLine -AllowPrerelease -Force
Install-Module PSReadLine
配置PSReadLine
在PowerShell的配置文件中添加下面的内容
Import-Module PSReadLine # 导入PSReadLine模块
Set-PSReadLineOption -PredictionSource History # 提示历史命令
Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[36;7;238m"} # 美化提示时的命令颜色
Set-PSReadLineOption -PredictionViewStyle InlineView
# 下面的快捷键模式,可依据个人需求设置
#Set-PSReadLineOption -EditMode Emacs # 设置快捷键为Emacs模式
#Set-PSReadLineOption -EditMode Vi # 设置快捷键为Vi模式
Set-PSReadlineOption -BellStyle None # 设置不进行声音报警
# 设置终端输入输出编码格式
[System.Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")
[System.Console]::InputEncoding = [System.Text.Encoding]::GetEncoding("utf-8")
$env:LESSCHARSET = "utf-8"
美化终端prompt
在美化终端prompt之前,可先安装一些开源的字体,并将终端配置为这种字体,以便在终端中能显示一些特殊的字符,推荐开源字体nerd-fonts,进入Release
界面,点击Show all assets
选择字体下载,然后解压拷贝到C:\Windows\Fonts
中
- 安装oh-my-posh,在终端中输入命令
scoop install oh-my-posh
- 在PowerShell的配置文件中添加一行内容
oh-my-posh init pwsh --config "$Env:POSH_THEMES_PATH\negligible.omp.json" | Invoke-Expression
,保存配置文件,重启终端即可 - 可以通过在终端输入
Get-PoshThemes
命令预览oh-my-posh
中的所有prompt主题,然后修改配置文件来设置自己喜欢的主题
历史命令查询
历史命令调取的快捷键 Ctrl+R
- 安装依赖软件
scoop install grep gawk fzf
- 配置PowerShell
# 在PowerShell配置文件中添加一行
Set-PSReadLineKeyHandler -Chord Ctrl+r -ScriptBlock {
$command = Get-Content (Get-PSReadlineOption).HistorySavePath | awk '!a[$0]++' | fzf --tac
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($command)
}
美化Git Prompt
- 安装
posh-git
,在终端中输入命令
scoop install posh-git
- 在PowerShell配置文件中加入以下内容
Import-Module posh-git
设置终端为VisualStudio开发环境
相当于打开x64 Native Tools Command Prompt for VS 2019
的终端环境
在PowerShell配置文件中添加下面的内容
# 找到自己的VS安装目录下的Microsoft.VisualStudio.DevShell.dll文件,替换下面的目录
Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
# 通过命令获取Vs InstanceId "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
# 将获取到的id替换掉下面的<InstanceId>
Enter-VsDevShell -VsInstanceId <InstanceId> -SkipAutomaticLocation -DevCmdArguments '-arch=x64 -no_logo'
安装miniconda
- 安装miniconda3
scoop install miniconda3
- 配置miniconda
根据安装后的提示进行配置,或者在PowerShell的配置文件中添加下面内容
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
# !! 将安装路径替换为自己的安装路径
(& "C:\Users\Jack\scoop\apps\miniconda3\current\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion