程序员的windows powershell配置

本文介绍了如何在WindowsPowerShell中安装和配置Scoop作为软件包管理工具,PSReadLine以增强历史命令提示,以及美化终端prompt、设置GitPrompt、模仿VisualStudio开发环境和安装miniconda等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序员的windows powershell配置

将Windows终端配置成类似于配置了ohmyzsh的Linux/MacOS的终端一样

安装Scoop

scoop是一个windows软件包的管理工具,类似于MacOS的Homebrew,Ubuntu的APT。这边列一下scoop的基本安装,更详细和高级的使用请参考scoop官网,顺便提一下安装scoop需要翻墙

  1. 打开PowerShell终端,输入并运行下面的命令

    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
    irm get.scoop.sh | iex
    
  2. 成功安装scoop后,为scoop添加bucket

    # 首先安装git, 因为给scoop添加bucket(可理解为软件仓库)的时候需要git
    scoop install git
    # 给scoop添加main bucket
    scoop bucket add main
    # 给scoop添加extras bucket
    scoop bucket add extras
    
  3. 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

用于记录、提示历史命令,功能类似于ohmyzshzsh-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

  1. 安装oh-my-posh,在终端中输入命令scoop install oh-my-posh
  2. 在PowerShell的配置文件中添加一行内容oh-my-posh init pwsh --config "$Env:POSH_THEMES_PATH\negligible.omp.json" | Invoke-Expression,保存配置文件,重启终端即可
  3. 可以通过在终端输入Get-PoshThemes命令预览oh-my-posh中的所有prompt主题,然后修改配置文件来设置自己喜欢的主题

历史命令查询

历史命令调取的快捷键 Ctrl+R

  1. 安装依赖软件
scoop install grep gawk fzf
  1. 配置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

  1. 安装posh-git,在终端中输入命令
scoop install posh-git
  1. 在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

  1. 安装miniconda3
scoop install miniconda3
  1. 配置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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值