windows PowerShell 高效开发

@一棵菜菜  March 29, 2021

前言

之前没怎么用过 windows 来开发,现在每天使用,感觉还是得提高基础开发效率才行呀。所以学习研究了下命令行方面的工具使用~ 还需继续拓展。网上相关教程方法很多,摘抄并小结成此文,方便后续查阅。

一、PowerShell 我的快捷键

  • 创建新窗格(分屏,splitPane):alt+plus(加号)
  • 在窗格间进行切换:alt+箭头键 (我的键盘)
  • 调整窗格的大小:alt+shift +箭头键调整
  • 关闭窗格:ctrl+w
官方文档:Windows 终端中的窗格

二、为 PowerShell 设置 alias (命令行命令别名)

1. 创建并修改Windows Powershell 启动执行文件

执行下面的命令,确定 PowerShell 的配置脚本的名称和位置(该脚本在每次启动 PowerShell 时自动运行)。

如果实际文档中不存在这个文件我们自己新建即可,如果存在可直接命令code $PROFILE打开。
echo $PROFILE
// 输出: C:\Users\Administrator\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

2 按格式输入命令

在脚本末尾添加我们的命令:

// 命令格式:
function 别名 { 需要替代的命令 }

// eg:
function work {cd D:\wwwroot}
function demo {cd D:\wwwroot\react-demo}

3 执行设定

管理员身份打开Powershell, 执行命令Set-ExecutionPolicy RemoteSigned。再重启 Powershell,输入"work",及到达指定文件啦~

参考文档《为 Windows PowerShell 设置 alias (命令行命令别名)》

三、教程:在 Windows 终端中设置 Powerline

给 PowerShell 带来 oh-my-zsh 的体验~~~

效果:

1604039291.png

1. 下载

管理员身份打开Powershell,下载安装 posh-git、oh-my-posh:

  • Posh-Git: 将 Git 状态信息添加到提示,并为 Git 命令、参数、远程和分支名称添加 tab 自动补全。
  • Oh-My-Posh: 为 PowerShell 提示符提供主题功能。
  • PSReadline: 允许在 PowerShell 中自定义命令行编辑环境。
Install-Module posh-git -Scope CurrentUser # posh-git
Install-Module oh-my-posh -Scope CurrentUser # oh-my-posh
Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck # 如果使用的是 PowerShell Core,则需要安装 PSReadLine

2. 自定义 PowerShell 提示符

code $PROFILE打开 PowerShell 的配置脚本(该脚本在每次启动 PowerShell 时自动运行)。
将以下内容添加到脚本末尾:

Import-Module posh-git # 引入 posh-git
Import-Module oh-my-posh # 引入 oh-my-posh
Set-PoshPrompt -Theme Paradox # 设置 oh-my-posh 主题为 Paradox
现在每个新实例启动时都会导入 Posh-Git 和 Oh-My-Posh,然后从 Oh-My-Posh 设置 Paradox 主题。

查看 Oh-My-Posh 附带的所有内置主题的命令:

Get-PoshThemes

3. 在设置中将 Cascadia Code PL 设置为 fontFace

  1. 下载 microsoft的 Cascadia Code 字体:[如 CascadiaCode-2102.25.zip

](https://github.com/microsoft/cascadia-code/releases)。给git提示等用,否则会有乱码显示空方框等。

  1. 安装字体:解压后,右键单击字体文件,然后单击“为所有用户安装”。这会将字体安装到您的计算机上。
  2. 打开PowerShell 的 JSON 配置文件,在list中找到 PowerShell 的配置内容,并添加 "fontFace": "Cascadia Code PL",重启PS后就会显示很好看的 Cascadia Code Powerline 字形啦,也就没有乱码了。

Windows PowerShell 配置文件 settings.json 文件现在应如下所示:

    "list": [
      {
        // Make changes here to the powershell.exe profile.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "fontFace": "Cascadia Code PL", // 重要!将 Cascadia Code PL 指定为字体
        "hidden": false
      },
      ...
    ]
  1. PoserShell 的tab “+”右边的下拉菜单中选择“设置”,来打开settings.json。
  2. oh-my-posh主题下 git分支乱码怎么解决?

答:乱码是因为没有设置成支持 Powerline 的字体,可以试试Cascadia Code PL这个字体

现在打开 PowerShell,就可以看到和 oh-my-zsh 差不多的好看的效果啦~~


我的配置文件

最后附上我的配置文件和配置脚本供参考

配置文件 setting.json:

// This file was initially generated by Windows Terminal 1.3.2651.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
  "$schema": "https://aka.ms/terminal-profiles-schema",

  "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

  // You can add more global application settings here.
  // To learn more about global settings, visit https://aka.ms/terminal-global-settings

  // If enabled, selections are automatically copied to your clipboard.
  "copyOnSelect": false,

  // If enabled, formatted data is also copied to your clipboard
  "copyFormatting": false,

  // "theme":"system",

  // A profile specifies a command to execute paired with information about how it should look and feel.
  // Each one of them will appear in the 'New Tab' dropdown,
  //   and can be invoked from the commandline with `wt.exe -p xxx`
  // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
  "profiles": {
    "defaults": {
      // Put settings here that you want to apply to all profiles.
      "background": "#012456",
      "useAcrylic" : true,// 使用亚克力效果
      "acrylicOpacity": 0.85,//亚克力背景透明度(需启用useAcrylic)
      "snapOnInput" : true //输入的时候自动滚动到输入位置
      // "fontSize": 12,
      // "backgroundImage":"desktopWallpaper",
      // "backgroundImage":"D:/caiyichen/bg.png",
      // "backgroundImageOpacity" : 1
    },
    "list": [
      {
        // Make changes here to the powershell.exe profile.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "fontFace": "Cascadia Code PL", // 将 Cascadia Code PL 指定为字体
        "hidden": false
      },
      {
        // Make changes here to the cmd.exe profile.
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "hidden": false
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "hidden": false,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      },
      {
        "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
        "hidden": false,
        "name": "Ubuntu-20.04",
        "source": "Windows.Terminal.Wsl"
      }
    ]
  },
  // Add custom color schemes to this array.
  // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
  "schemes": [],
  // Add custom actions and keybindings to this array.
  // To unbind a key combination from your defaults.json, set the command to "unbound".
  // To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
  "actions": [
    // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
    // These two lines additionally bind them to Ctrl+C and Ctrl+V.
    // To learn more about selection, visit https://aka.ms/terminal-selection
    { "command": { "action": "copy", "singleLine": false }, "keys": "ctrl+c" },
    { "command": "paste", "keys": "ctrl+v" },

    // Press Ctrl+Shift+F to open the search box
    { "command": "find", "keys": "ctrl+f" },

    // Press Alt+Shift+D to open a new pane.
    // - "split": "auto" makes this pane open in the direction that provides the most surface area.
    // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
    // To learn more about panes, visit https://aka.ms/terminal-panes
    {
      "command": {
        "action": "splitPane",
        "split": "auto",
        "splitMode": "duplicate"
      },
      // "keys": "alt+shift+d"
      "keys": "ctrl+plus"
    },
    // Switch between panes
    {
      "command": { "action": "moveFocus", "direction": "down" },
      "keys": "ctrl+down"
    },
    {
      "command": { "action": "moveFocus", "direction": "left" },
      "keys": "ctrl+left"
    },
    {
      "command": { "action": "moveFocus", "direction": "right" },
      "keys": "ctrl+right"
    },
    {
      "command": { "action": "moveFocus", "direction": "up" },
      "keys": "ctrl+up"
    },
    // Resize the panes
    {
      "command": { "action": "resizePane", "direction": "down" },
      "keys": "ctrl+shift+down"
    },
    {
      "command": { "action": "resizePane", "direction": "left" },
      "keys": "ctrl+shift+left"
    },
    {
      "command": { "action": "resizePane", "direction": "right" },
      "keys": "ctrl+shift+right"
    },
    {
      "command": { "action": "resizePane", "direction": "up" },
      "keys": "alt+shift+up"
    },
    // Close the pane
    { "command": "closePane", "keys": "ctrl+w" }
  ]
}

脚本 Microsoft.PowerShell_profile.ps1:

# alias
function work { cd D:\wwwroot }
function ip { ipconfig }

Import-Module posh-git # 引入 posh-git
Import-Module oh-my-posh # 引入 oh-my-posh
Set-PoshPrompt -Theme star # 设置 oh-my-posh 主题为 star. powerline ys Paradox star material spaceship avit

添加新评论