mdi-chevron-left
Last:当前STM32开发的几种最流行的开发环境对比
Next:STM32使用HAL库操作FLASH的注意事项
mdi-chevron-right
## 序
微软在2019年公布了一个全新的开源命令行终端项目:[**Windows Terminal**](https://github.com/microsoft/terminal/releases),并在2020年初在Github上发布了第一个大版本号v1开头的release版本,至今已发布到了v1.5.3242版本。
此前Windows平台上除了cmd以外,还有power shell,但使用起来的体验跟Linux的shell相比,简直天差地别。不谈相关命令语法本身的设计问题,只谈cmd和power shell的界面,比如多国语言显示、代码着色、字体美观程度等等,都不尽如人意。
新推出的**Windows Terminal**有了飞一样的提升,不光颜值担当,内功也相当深厚,甚至具备显卡GPL加速渲染的能力,下面我们就来从安装到配置体验一番。
## 安装
有两种安装方式
1. 直接打开Microsoft Store,搜索terminal进行安装。
<img src="https://files.hexcode.cn//1.png" style="zoom:80%;" />
2. 打开该项目在Github上的Release页面,下载安装:
[https://github.com/microsoft/terminal/releases](https://github.com/microsoft/terminal/releases)
## 配置
安装完成后,我们打开进行简单的体验,发现其本质是一个具有多标签页能力的命令行窗体,新建命令行标签页时可以选择使用cmd或是Power Shell,同时解决了一些之前命令行的痛点,比如之前提到的字体美观度、字符集编码等等。
下面,我们进行一些简单的配置,让它更加方便地被使用。
### 默认管理员打开
很多时候,我们的命令需要管理员权限执行,跟Linux下使用`su`或者`sudo`不同,在windows上一旦打开一个命令行,就很难将自身切换成管理员模式,只有关掉窗体,然后找到cmd的图标,然后右击,以管理员身份执行。
这里介绍一个神器[`gsudo`](https://github.com/gerardog/gsudo),这是windows下模拟Linux中`sudo`命令的工具,在Github上开源:https://github.com/gerardog/gsudo
根据Github上的介绍进行安装,这里不多赘述,我很多软件在Windows上都是使用choco进行包管理的,所以这里我是使用choco的方式安装的。
安装好后,`gsudo`默认加入了环境变量,我们可以在cmd中敲入`gsudo`快速切入管理员模式了。
当然,我们还可以让Windows Terminal帮我们使用`gsudo`打开cmd/powershell作为第一个命令行标签页。
```json
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44ba}",
"name": "Windows PowerShell Elevated",
"commandline": "gsudo.exe powershell.exe",
"hidden": false,
"colorScheme":"One Half Dark",
"fontFace":"Fira Code",
"icon":"https://i.imgur.com/Giuj3FT.png"
},
```
我们只需要打开Windows Terminal的Settings,在弹出的文本编辑器中,会打开Windows Terminal的配置文件,名为`settings.json`
该文件32行左右,有个list数组,在数组元素的顶端添加以上节点数据。于是,我们就成功地在开头增加了一个命令行图标,它的执行脚本是`gsudo.exe powershell.exe`也就是用管理员身份打开了`powershell`脚本。我们给这个命令行条目分配了一个随机`uuid`,在`settings.json`的顶部,大概在11行,有一个`defaultProfile`字段,我们修改为刚才分配好的`uuid`号即可让windows terminal启动时默认打开这一条目,即以管理员身份打开`powershell`。

值得注意的是,在上述代码的后三行,有几个关乎颜值的配置可以自行配置,`colorScheme`字段代表配色方案,我这里使用的是内置`One Half Dark`主题,更多主题可以点击[这里查看](https://docs.microsoft.com/zh-cn/windows/terminal/customize-settings/color-schemes),也可以根据文档自行设计主题方案。`fontFace`是字体,`icon`是列表中显示的图标,可以使用资源URL方式作为值,这些自不必多说。
### Git-bash
命令行用得最多最顺手的还是Linux下的shell,windows的cmd和powershell的那些命令,总觉得生疏,很多时候都要搜索一下才想起来怎么用。
那么有什么办法在windows中体验Linux的shell呢,我想你会说WSL,但太重量级了,另外我个人不太建议使用WSL,Linux要么运行在服务器上,要么运行在虚拟机中(VirtualBox or VMware),WSL我感觉只适合玩玩,实际作用并不大。
其实对于程序员来说,windows版的Git提供了轻量级的Linux shell解决方案git-bash,在Git的安装目录中有`\Git\bin\bash.exe`和`\Git\bin\sh.exe`任君选择。
于是,我们模仿上面的例子,在`settings.json`中再添加一个元素:
```json
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
"hidden": false,
"name": "git bash",
"commandline": "gsudo \"D:\\Program Files\\Git\\bin\\bash.exe\"",
"colorScheme":"One Half Dark",
"fontFace":"Fira Code",
"icon":"http://docs.hexcode.cn/git.png"
},
```
这样,我们就可以通过它来以管理员身份启动一个bash shell,无论是执行linux下什么指令,都不在话下。即便有些指令涉及到windows的保护资源,因为有`gsudo`的加持,操作起来也非常方便。


## 结语
好了,关于Windows Terminal的使用经验就分享到这里,最后贴一份我自用的`settings.json`,方便不想折腾的小伙伴直接使用。如果您还有什么关于使用Windows Terminal的技术,欢迎评论区沟通交流,您还可以关注我的个人公众号【编程者说】持续关注有关软硬件、前后端各种IT技能的开发心得。


```json
// This file was initially generated by Windows Terminal 1.4.3141.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": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
// 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,
// 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.
},
"list":
[{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
"hidden": false,
"name": "git bash",
"commandline": "gsudo \"D:\\Program Files\\Git\\bin\\bash.exe\"",
"colorScheme": "One Half Dark",
"fontFace": "Fira Code",
"icon": "http://docs.hexcode.cn/git.png"
}, {
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44ba}",
"name": "Windows PowerShell Elevated",
"commandline": "gsudo.exe powershell.exe",
"hidden": false,
"colorScheme": "One Half Dark",
"fontFace": "Fira Code",
"icon": "https://i.imgur.com/Giuj3FT.png"
}, {
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"colorScheme": "One Half Dark",
"fontFace": "Fira Code",
"hidden": false
}, {
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"colorScheme": "One Half Dark",
"fontFace": "Fira Code",
"commandline": "cmd.exe",
"hidden": false
}, {
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"colorScheme": "One Half Dark",
"fontFace": "Fira Code",
"source": "Windows.Terminal.Azure"
}
]
},
// 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+shift+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"
}
]
}
```
mdi-chevron-left
Last:当前STM32开发的几种最流行的开发环境对比
Next:STM32使用HAL库操作FLASH的注意事项
mdi-chevron-right