Loading

Windows也能拥有好用的命令行吗?Powershell+Terminal折腾记录(v1.0版本)

PS:本文写于2021年,现在已经是2024年,有了很多新变化,我在接下来的文章里会继续更新。

前言

Windows一向以图形化操作入门容易著称,所以对于命令行的支持一直为人所诟病,比起Linux或者mac都是被吊打的。这一局面在最近几个版本的Windows10更新里,特别是微软官方出了WindowsTerminal这个大杀器之后有所改善~

所以对于标题的这个问题,只能说「勉强可以吧」,本文介绍几个折腾命令行的方法。

效果图

偷一张官方的截图,应该没人发现吧

建议先熟读官方文档:https://docs.microsoft.com/zh-cn/windows/terminal/

软件包管理器 Scoop

不用羡慕Linux上好用的apt和yum,Windows上也有不错的软件包管理器,比如chocolate、官方的win-get,还有本文介绍的scoop,相比起chocol ate,scoop的软件包仓库都是社区维护开源的,安全性更有保障,并且比官方的win-get多很多~ 不够可以自己添加软件源,就像apt的PPA源。

官网:https://scoop.sh/

安装:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
iwr -useb get.scoop.sh | iex

oh-my-posh & powerline

用过oh-my-zsh的同学就知道这个oh-my-xxx的项目了~ Windows上自然不能少~

地址: https://ohmyposh.dev/docs/

首先下载powerline字体并安装,These fonts can be installed from the Cascadia Code GitHub releases page.

输入命令安装powershell插件,post-git需要git支持,If you don't already have it, install Git for Windows.

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

Open your PowerShell profile with notepad $PROFILE,输入以下内容:

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

打开terminal配置,修改字体,for example:

{
    // Make changes here to the powershell.exe profile.
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "name": "Windows PowerShell",
    "commandline": "powershell.exe",
    "fontFace": "Cascadia Code PL",
    "hidden": false
},

配置完的效果:

参考资料:https://docs.microsoft.com/en-us/windows/terminal/tutorials/powerline-setup

使用sudo(gsudo)

在用Windows的powershell的时候,有时一些操作是需要管理员权限的,但是不像Linux那么方便,一句sudo命令即可提权,Windows往往需要右键点击开始菜单,选择powershell的管理员模式。

麻烦不说,还可能导致心急的同学砸键盘

不过办法总比困难多,这不有了开源的gsudo,可以在Windows方便提权,安装方法如下~

安装:

PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex"

装完就可以直接在powershell里使用sudo命令提权了,同时可以在Terminal里添加一个管理员权限的标签卡,看看怎么配置

在terminal的配置文件中添加一项:

"list": [
  {
      "guid": "{41dd7a51-f0e1-4420-a2ec-1a7130b7e950}",
      "name": "Windows PowerShell Elevated",
      "commandline": "gsudo.exe powershell.exe",
      "hidden": false,
      "colorScheme": "Solarized Dark",
      "fontFace": "Fira Code",
      "icon" : "https://i.imgur.com/Giuj3FT.png"
  },
  //...
]

参考资料

posted @ 2024-02-19 22:47  程序设计实验室  阅读(978)  评论(0编辑  收藏  举报