在Windows系统和Linux系统中,如何打造一个好终端?

开发 前端
在十多年的开发生涯中,笔者热衷于使用shell。命令永远不变,笔者可以创建可靠、快捷的自定义快捷方式,但默认值欠佳。阅读本文将带你了解如何在系统上获得很棒的shell +终端。

本文转载自公众号“读芯术”(ID:AI_Discovery)。

在十多年的开发生涯中,笔者热衷于使用shell。命令永远不变,笔者可以创建可靠、快捷的自定义快捷方式,但默认值欠佳。阅读本文将带你了解如何在系统上获得很棒的shell +终端。

术语

在Windows系统和Linux系统中,如何打造一个好终端?

shell用于执行命令。终端(terminal)是运行shell的包装器。在终端上可以设置字体、字体大小、配色方案以及支持多个选项卡。终端仿真器的示例包括GNOME终端、KDE的Konsole,Terminator和XTerm。对于Linux系统,笔者建议保留默认值。Windows适用Windows Terminal;Mac适用iTerm2。

图源:MartinThoma)

(Linux上的四个终端仿真器(Gnome Terminal、Konsole、XTerm、Terminator)。XTerm不直接支持标签。其他的打开了2个选项卡。它们都运行Bash shell。

系统的Shell包括ZSH、Bash、fish。Windows系统典型的shell是PowerShell。可以通过执行echo $ 0来查看正在运行的shell。Linux系统使用更多的是Bash shell。

 

在Windows系统和Linux系统中,如何打造一个好终端?

 

 图源:Martin Thoma

(在Gnome终端中的三个Linux shell(ZSH、Bash、fish),ZSH shell经过了大量更改;Bash shell和fish shell显示默认值。可见,fish shell具有其他两个shell缺少的内联自动提示功能。)

每个shell都有提示符(prompt)。提示符是光标之前写入的内容。它表示可以输入指令并提供有用的背景信息。在上面的示例中,提示符包含用户名moose,当前计算机pc08 ,当前工作目录 ~/GitHub/MartinThoma/flake8-simplify,活跃git分支feature/19以及进行了±修改的事实。

字体

无论选择哪个,字体都很重要。你可能想要一个等宽字体,相信我一定要安装powerline字体。powerline字体能使shell使用像图像的字符。它可以使提示符效果更好。笔者比较喜欢UbuntuMono和DroidSans Mono:

在Windows系统和Linux系统中,如何打造一个好终端?

上面一行是Ubuntu Mono,下面是Droid Sans Mono。二者都不错,Ubuntu Mono略胜一筹。| 图源:Martin Thoma

也有Fira Code、Jetbrains Mono 这样的“编程字体”。笔者不喜欢它们,因为很难识别所写内容。

Windows终端

首先确保已安装Windows终端。启动终端并找到设置。它是这个指下的“小箭头”:

在Windows系统和Linux系统中,如何打造一个好终端?

点击“Settings”。| 图源:MartinThoma

可以选择JSON文件随自己喜好而更改。如下:

  1. // This file was initially generated byWindows Terminal 1.2.2381.0 
  2.           // It should still be usable in newerversions, but newer versions might have additional 
  3.           // settings, help text, or changes thatyou will not see unless you clear this file 
  4.           // and let us generate a new one foryou. 
  5.              // To view thedefault settings, hold "alt" while clicking onthe "Settings" button. 
  6.           // For documentation on thesesettings, see: https://aka.ms/terminal-documentation 
  7.           { 
  8.               "$schema": "https://aka.ms/terminal-profiles-schema", 
  9.                  "defaultProfile": "{2c4de342-38b7-51cf-b940-2309a097f518}", 
  10.              "initialCols":  120, 
  11.              "initialRows":  30, 
  12.                  //Youcanaddmoreglobalapplicationsettingshere. 
  13.              //Tolearnmoreaboutglobalsettings,visithttps://aka.ms/terminal-global-settings 
  14.                  //Ifenabled, selectionsareautomaticallycopiedtoyourclipboard. 
  15.              "copyOnSelect": false, 
  16.                  //Ifenabled,formatteddataisalsocopiedtoyourclipboard 
  17.              "copyFormatting": false, 
  18.                  //Aprofilespecifiesacommandtoexecutepairedwithinformationabouthowitshouldlookandfeel. 
  19.              //Eachoneofthemwillappearinthe NewTab dropdown, 
  20.              //   andcanbeinvokedfromthecommandlinewith`wt.exe-pxxx` 
  21.              //Tolearnmoreaboutprofiles,visithttps://aka.ms/terminal-profile-settings 
  22.              "profiles": 
  23.              { 
  24.                  "defaults": 
  25.                  { 
  26.                       //Putsettingsherethatyouwanttoapplytoallprofiles. 
  27.                  }, 
  28.                  "list": 
  29.                  [ 
  30.                       { 
  31.                           //Makechangesheretothepowershell.exeprofile. 
  32.                           "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", 
  33.                           "name": "WindowsPowerShell", 
  34.                           "commandline": "powershell.exe", 
  35.                           "hidden": false, 
  36.                           "fontFace": "DejaVuSans Mono for Powerline" 
  37.                       }, 
  38.                       { 
  39.                           //Makechangesheretothecmd.exeprofile. 
  40.                           "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", 
  41.                           "name": "CommandPrompt", 
  42.                           "commandline": "cmd.exe", 
  43.                           "hidden": false 
  44.                       }, 
  45.                       { 
  46.                           "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", 
  47.                           "hidden": false, 
  48.                           "name": "Ubuntu", 
  49.                           "source": "Windows.Terminal.Wsl", 
  50.                           "fontFace": "DejaVuSans Mono for Powerline", 
  51.                           "colorScheme":  "Solarized Dark", 
  52.                       }, 
  53.                       { 
  54.                           "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", 
  55.                           "hidden": false, 
  56.                           "name": "AzureCloud Shell", 
  57.                           "source": "Windows.Terminal.Azure" 
  58.                       } 
  59.                  ] 
  60.              }, 
  61.                  //Addcustomcolorschemestothisarray. 
  62.              //Tolearnmoreaboutcolorschemes,visithttps://aka.ms/terminal-color-schemes 
  63.              "schemes": [ 
  64.                               { 
  65.                                   "background":  "#073642", 
  66.                                   "black":  "#073642", 
  67.                                   "blue":  "#268BD2", 
  68.                                   "brightBlack":  "#002B36", 
  69.                                   "brightBlue":  "#839496", 
  70.                                   "brightCyan":  "#93A1A1", 
  71.                                   "brightGreen":  "#586E75", 
  72.                                  "brightPurple":  "#6C71C4", 
  73.                                   "brightRed":  "#CB4B16", 
  74.                                   "brightWhite":  "#FDF6E3", 
  75.                                   "brightYellow":  "#657B83", 
  76.                                   "cyan":  "#2AA198", 
  77.                                   "foreground":  "#FDF6E3", 
  78.                                   "green":  "#859900", 
  79.                                   "name":  "Solarized Dark", 
  80.                                   "purple":  "#D33682", 
  81.                                   "red":  "#D30102", 
  82.                                   "white":  "#EEE8D5", 
  83.                                   "yellow":  "#B58900" 
  84.                               }, 
  85.                               { 
  86.                                   "background":  "#FDF6E3", 
  87.                                   "black":  "#073642", 
  88.                                   "blue":  "#268BD2", 
  89.                                   "brightBlack":  "#002B36", 
  90.                                   "brightBlue":  "#839496", 
  91.                                   "brightCyan":  "#93A1A1", 
  92.                                   "brightGreen":  "#586E75", 
  93.                                   "brightPurple":  "#6C71C4", 
  94.                                   "brightRed":  "#CB4B16", 
  95.                                   "brightWhite":  "#FDF6E3", 
  96.                                   "brightYellow":  "#657B83", 
  97.                                   "cyan":  "#2AA198", 
  98.                                   "foreground":  "#073642", 
  99.                                   "green":  "#859900", 
  100.                                   "name":  "Solarized Light", 
  101.                                   "purple":  "#D33682", 
  102.                                   "red":  "#D30102", 
  103.                                   "white":  "#EEE8D5", 
  104.                                   "yellow":  "#B58900" 
  105.                               }, 
  106.              ], 
  107.                  //Addcustomkeybindingstothisarray. 
  108.              //Tounbindakeycombinationfromyourdefaults.json,setthecommandto"unbound". 
  109.              //Tolearnmoreaboutkeybindings,visithttps://aka.ms/terminal-keybindings 
  110.              "keybindings": 
  111.              [ 
  112.                  //CopyandpasteareboundtoCtrl+Shift+CandCtrl+Shift+Vinyourdefaults.json. 
  113.                  //ThesetwolinesadditionallybindthemtoCtrl+CandCtrl+V. 
  114.                  //Tolearnmoreaboutselection, visithttps://aka.ms/terminal-selection 
  115.                  { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" }, 
  116.                  { "command": "paste", "keys": "ctrl+v" }, 
  117.     
  118.   
  119.                              //PressCtrl+Shift+Ftoopenthesearchbox 
  120.                  { "command": "find", "keys": "ctrl+shift+f" }, 
  121.     
  122.   
  123.                              //PressAlt+Shift+Dtoopenanewpane. 
  124.                  //-"split":"auto"makesthispaneopeninthedirectionthatprovidesthemostsurfacearea. 
  125.                  //-"splitMode":"duplicate"makesthenewpaneusethefocusedpane sprofile. 
  126.                  //Tolearnmoreaboutpanes, visithttps://aka.ms/terminal-panes 
  127.                  { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, 
  128.                     "keys": "alt+shift+d" }, 
  129.                  {"command": "closeTab", "keys": "ctrl+w"}, 
  130.                  {"command": "newTab", "keys": "ctrl+t"} 
  131.              ] 
  132.           } 

下载并安装所有4种“ DejaVu Sans Mono Powerline”字体。在已知的所有系统上,字体安装都是一个流程:双击字体,然后跳出一个有“安装”按钮的窗口。

Linux和Windows终端:Aminal

Aminal是用Go语言编写的终端仿真器。可用于Linux、Windows和Mac。它允许通过配置文件完成颜色、键盘快捷键等配置。首先需要在系统中安装并配置Go语言。Ubuntu的示例如下:

  1. $ sudo apt-get install golang libgl1-mesa-dev xorg-dev 
  2. $ export GOPATH="$HOME/go" 
  3. $ export GOBIN=$(go env GOPATH)/bin 

接着就可以安装并运行aminal了:

  1. $ go get -u github.com/liamg/aminal 
  2. $ aminal 

Linux系统:Gnome终端

可以通过编辑文件来自定义Gnome终端。这里笔者用12号字设置了Ubuntu Mono衍生的powerline常规字体。

在Windows系统和Linux系统中,如何打造一个好终端?

图源:Martin Thoma

命令是为了zsh而设置,因为这是笔者最喜欢的shell。

在Windows系统和Linux系统中,如何打造一个好终端?

图源:Martin Thoma

颜色设置为solarized dark(从左到右、从上到下)

  • 背景: #2e3436 / #555753
  • 深红: #cc0000 / 浅红: ef2929
  • 深绿: #4e9a06 / 浅绿: #8ae234
  • 深黄: #c4a000 / 浅黄: #fce94f
  • 深蓝: #3465a4 / 浅蓝: #729fcf
  • 深紫: #75507b / 浅紫: #ad7fa8
  • 深蓝绿: #06989a / 浅蓝绿: #34e2e2
  • 深灰: #d3d7cf /浅灰: #eeeeec

在Windows系统和Linux系统中,如何打造一个好终端?

Linux系统:fish shell

安装fish shell:

  1. sudo apt-get install fish 

更改终端仿真器的默认shell为fish。在Gnome终端叫做“自定义命令”

接着安装“Oh My Fish”:

  1. curl -L https://get.oh-my.fish | fish 

然后将主题设置为agnoster:

  1. omf install agnoster 
  2. omf theme agnoster 

别名

要使终端表现出色,核心部分的命令就要简短。因此为命令创建一个别名,即简短版的原命令。最常见是将目录更改为上一级。假如是在 /home/user/foo/bar中,则要转到 /home/user/foo。大多数shell都必须输入cd .. 。我喜欢将其缩写为 ..。所以别名就是alias ..='cd ..' 。

语法可能不同,具体取决于shell。对于Bash、ZSH和fish:

  1. alias shortlong  

在bash中要插入到 ~/.bashrc,ZSH中则是 ~/.zshrc,fish中也不相同。一些别名如下:

  1. # Shorten things 
  2. alias ..cd ..  
  3. alias ...cd ../../  
  4. alias llls -alF  
  5. alias lals -A  
  6. alias lls -CF  
  7. alias cclear # If your terminal supports colors, use them! 
  8. alias lsls= ls --color=auto  
  9. alias grepgrep= grep --color=auto  
  10. alias fgrepfgrep= fgrep --color=auto  
  11. alias egrepegrep= egrep --color=auto  
  12. alias diffcolordiff # Works only if you have notify-send 
  13. alias alertnotify-send --urgency=low -i "$([ $? = 0 ] && echoterminal || echo error)" "$(history|tail -n1|sed -e   s/^s*[0-9]+s*//;s/[;&|]s*alert$//   )"  

总结一下:

  • 使用不错的终端仿真器。建议Linux系统使用Gnome终端,Mac系统使用iTerm 2,Windows系统使用Windows终端。
  • 安装比较好的powerline字体,比如Ubuntu Mono Powerline。
  • 在终端仿真器中按照喜好调整字体、字号以及颜色。
  • 安装一个好的shell。建议Linux系统使用fish shell,Windows系统使用PowerShell。
  • 按需调整shell的提示符。笔者个人喜欢agnoster主题。

 

责任编辑:赵宁宁 来源: 读芯术
相关推荐

2021-09-14 12:34:33

LinuxLinux终端

2019-06-19 13:50:21

NeofetchLinux

2017-05-25 11:24:18

达观数据NER系统

2021-07-19 09:37:59

Windows 操作系统Linux

2016-07-05 15:09:44

2019-11-11 10:45:44

LinuxWindows 10Debian 10

2024-04-01 11:52:46

2012-11-20 09:33:02

2022-07-20 09:38:31

Python语音合成代码

2021-08-11 14:06:43

服务器WindowsLinux

2021-08-24 16:20:38

Linux终端

2011-03-15 16:07:33

Windows AzuWCF

2017-03-15 19:24:29

Windows 10Windows时钟

2020-09-14 10:50:24

WindowsWSL 2Linux

2009-02-20 19:56:26

WindowsLinux双系统

2018-11-01 09:45:40

2021-08-01 08:12:55

Windows 微软WSL

2018-09-18 09:38:11

RPC远程调用网络通信

2023-11-06 14:28:11

Linux海底世界

2009-09-11 08:44:36

点赞
收藏

51CTO技术栈公众号