Windows 下终端及工具配置

markdown 写这篇文章的主要目的是总结一下我目前在 Windows 上使用的命令行终端和 Shell 的配置,让自己拥有有更舒适愉悦的使用体验。 ## Powershell 美化与配置 在 Windows 下会时常用到 Powershell 来进行一些简单的命令行操作,比如使用 Python、Node 等。 然而系统自带的 Powershell 丑的不忍直视,为了让自己用它的时候拥有更加愉悦的心情,得稍微折腾一下。在 Linux 环境下有大名鼎鼎的 [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh),于是 Windows 下便也诞生了 [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh) ,用来美化 Powershell。
安装的方法也非常简单,确保你已经安装了 [Git](https://git-scm.com/) 客户端。 首先打开 Powershell 输入以下指令安装 `posh-git` 以及 `oh-my-posh` ```powershell Install-Module posh-git -Scope CurrentUser Install-Module oh-my-posh -Scope CurrentUser ``` 之后运行命令 `notepad $PROFILE` 来打开默认的配置文件,并在最后加上: ```powershell Import-Module posh-git Import-Module oh-my-posh Set-Theme Paradox ``` 这样便能启用之前安装的两个插件,以及将主题设置为了 Paradox。oh-my-posh 还支持其它的一些[主题](https://github.com/JanDeDobbeleer/oh-my-posh#themes)。 除此之外,为了一些特殊的符号的显示正常,我们还需要 Powerline 字体,才可以得到和上图一样的效果。 这里不得不推荐一下微软官方的开源字体 [Cascadia Code](https://github.com/microsoft/cascadia-code),在 Windows Terminal 中将 `fontFace` 设置成 `Cascadia Code PL` 即可。 其它的还有 [Powerline fonts](https://github.com/powerline/fonts) 和 [Nerd-fonts](https://github.com/ryanoasis/nerd-fonts) ## Windows Terminal 的配置
从去年 Build 大会发布以来,[Windows Terminal](https://github.com/microsoft/terminal) 就一直在 GitHub 上积极的迭代,功能也日渐完善,现在它已经是我在 Windows 环境下的主力终端了。 目前 Windows Terminal 还没有可视化的设置界面,需要通过手动编辑 配置文件才能修改配置。可以参考它的 [SettingSchema](https://github.com/microsoft/terminal/blob/master/doc/cascadia/SettingsSchema.md) 来增删配置。这里也有一份修改官方的修改教程:[Editing Windows Terminal JSON Settings](https://github.com/microsoft/terminal/blob/master/doc/user-docs/UsingJsonSettings.md#adding-copy-and-paste-keybindings)。 我将全局的字体修改为了 ["Cascadia Code PL"](https://github.com/microsoft/cascadia-code) 以及将字号调整成了 12。 ```json "defaults": { // Put settings here that you want to apply to all profiles "fontFace" : "Cascadia Code PL", "fontSize" : 12 } ``` 我更加喜欢 "One Half Dark" 的主题。内置主题的话可以在 [ColorTool](https://github.com/microsoft/terminal/tree/master/src/tools/ColorTool) 里面的 [schemes](https://github.com/microsoft/terminal/tree/master/src/tools/ColorTool/schemes) 里找到,也可以自己在配置文件的 `schemes` 块添加自己的主题,如 [dracula](https://draculatheme.com/windows-terminal)。 ```json { // Make changes here to the powershell.exe profile "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "name": "PowerShell", "commandline": "powershell.exe", "hidden": false, "colorScheme" : "One Half Dark", "startingDirectory" : "." }, ``` 在 `keybindings` 区域可以添加一些常用的快捷键。 ```json // Add any keybinding overrides to this array. // To unbind a default keybinding, set the command to "unbound" "keybindings": [ { "command": "copy", "keys": ["ctrl+shift+c"] }, { "command": "paste", "keys": ["ctrl+shift+v"] }, { "command": {"action": "splitPane", "split": "auto"}, "keys": ["ctrl+|"]} ] ``` 为了能够更方便的使用,还可以在系统右键添加全局的 "Open Terminal Here" 的菜单。这样一来,在任何文件夹里面可以方便的打开 Terminal 并且定位到那个文件夹下面。可以在 GitHub 上的这个 [Issue](https://github.com/microsoft/terminal/issues/1060) 里找到一些社区用户提供的方法。
创建一个 `reg` 文件,将下面内容中的 `` 改为你用户的名字后,保存并执行即可。 ```bat Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell\wt] @="Windows Terminal here" "Icon"="C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminal_0.10.781.0_x64__8wekyb3d8bbwe\\WindowsTerminal.exe" [HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command] @="C:\\Users\\\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe -d ." [HKEY_CLASSES_ROOT\Directory\shell\wt] @="Windows Terminal here" "Icon"="C:\\Program Files\\WindowsApps\\Microsoft.WindowsTerminal_0.10.781.0_x64__8wekyb3d8bbwe\\WindowsTerminal.exe" [HKEY_CLASSES_ROOT\Directory\shell\wt\command] @="C:\\Users\\\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe -d ." ``` ## WSL 的配置 在不少时候需要用暂时到 Linux 的环境。 众所周知,在 [Windows Subsystem for Linux (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10) 问世之后,Linux "真的变成了" Windows 的一个"子系统"了。 之前有使用过 [oh-my-zsh](https://github.com/ohmyzsh/ohmyzsh) 但是 zsh 的启动速度过于感人,就转而改用了开箱即用的 [fish-shell]([https://github.com/fish-shell/fish-shell](https://github.com/fish-shell/fish-shell))。 这里以在 Ubuntu 环境下为例。 安装 fish-shell 非常简单,只需要执行 `sudo apt install fish` 即可。 之后继续安装 [oh-my-fish](https://github.com/oh-my-fish/oh-my-fish):`curl -L https://get.oh-my.fish | fish`。 之后可以通过 `omf theme` 来查看[主题](https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md),`omf install ` 来安装主题。 想要默认启动 fish shell 的话需要在 `~/.bashrc` 文件最后加上`fish`。 参考 - https://www.hanselman.com/blog/NowIsTheTimeToMakeAFreshNewWindowsTerminalProfilesjson.aspx - https://www.hanselman.com/blog/WindowsTerminal10IsComingUpdateNowAndSetUpYourSplitPaneHotkeys.aspx

Comments