拾遗笔记技术、工具与一些顺手记下的东西

PowerShell 配置命令自动补全

2023-11-04原创约 6 分钟

一、需求:

看到老师上课用 mac 命令行有自动补全功能,发现真的爽。但是自己的 windows powershell 不能使用自动补全功能。有了需求,就想找到能完成目前的任务,然后找到可以在 powershell 下面使用自动补全功能。

Windows Terminal 安装

用了一个学期的好用的终端推荐,微软应用商店搜索 “Windows Terminal”,安装

安装完成后可在任意页面右键打开终端。

随便一个目录,使用管理员权限打开 powershell

然后输入下面步骤:

管理员权限打开

二、操作记录

1、安装:´PSReadLine´ version 2.1.0

*powershell

Install-Module PSReadLine -RequiredVersion 2.1.0

2、初始化:

*powershell

Import-Module PSReadLine

Set-PSReadLineOption -PredictionSource History

⁂

如果出现图中问题需要更改一下 Win10 的执行策略,更改后再回到出错的那步重新进行,输入以下命令:

*powershell

Set-ExecutionPolicy RemoteSignedf

# 同意更改 输入Y

3、检测是否有配置好文件

*powershell

Test-path $profile

如果返回是‘false’

执行下面:

*powershell

New-item –type file –force $profile

如果返回是 ture,或者上一步创建好,直接下一步

4、编辑 profile 配置文件:

编辑文件:

*powershell

notepad $profile

输入一下内容:

*highlighter- mathematica

# Shows navigable menu of all options when hitting Tab

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

# Autocompletion for arrow keys

Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward

Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

# auto suggestions

Import-Module PSReadLine

Set-PSReadLineOption -PredictionSource History

保存之后,退出。

5、重新打开一个新的 powershell 或者 Terminal 页面。

随便输入一次命令,然后第二部输入首字母就会有自动提示了,按左右键就可以补全。上下键是可以切换以前面的一样的命令。

[

](https://img2022.cnblogs.com/blog/2777408/202207/2777408-20220721100818536-52276558.png)

下一篇准备进行 Windows Terminal 美化

参考文章:Fish-like Autosuggestion in Powershell - DEV Community

让 Windows 终端 飞一会 - 掘金 (juejin.cn)

打造 Win10 完美终端体验(Windows Terminal + oh-my-zsh)+ 报错解决方案 - 掘金 (juejin.cn)

EOF