全文共6894字,预计学习时长18分钟

打开网易新闻 查看精彩图片

图源:unsplash

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

术语

打开网易新闻 查看精彩图片

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

打开网易新闻 查看精彩图片

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

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

打开网易新闻 查看精彩图片

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

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

字体

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

打开网易新闻 查看精彩图片

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

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

Windows终端

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

打开网易新闻 查看精彩图片

点击“Settings”。| 图源:MartinThoma

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

// This file was initially generated byWindows Terminal 1.2.2381.0 // It should still be usable in newerversions, but newer versions might have additional // settings, help text, or changes thatyou will not see unless you clear this file // and let us generate a new one foryou. // To view thedefault settings, hold "alt" while clicking onthe "Settings" button. // For documentation on thesesettings, see: https://aka.ms/terminal-documentation { "$schema": "https://aka.ms/terminal-profiles-schema", "defaultProfile": "{2c4de342-38b7-51cf-b940-2309a097f518}", "initialCols": 120, "initialRows": 30, //Youcanaddmoreglobalapplicationsettingshere. //Tolearnmoreaboutglobalsettings,visithttps://aka.ms/terminal-global-settings //Ifenabled, selectionsareautomaticallycopiedtoyourclipboard. "copyOnSelect": false, //Ifenabled,formatteddataisalsocopiedtoyourclipboard "copyFormatting": false, //Aprofilespecifiesacommandtoexecutepairedwithinformationabouthowitshouldlookandfeel. //Eachoneofthemwillappearinthe'NewTab'dropdown, // andcanbeinvokedfromthecommandlinewith`wt.exe-pxxx` //Tolearnmoreaboutprofiles,visithttps://aka.ms/terminal-profile-settings "profiles": { "defaults": { //Putsettingsherethatyouwanttoapplytoallprofiles. }, "list": [ { //Makechangesheretothepowershell.exeprofile. "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "name": "WindowsPowerShell", "commandline": "powershell.exe", "hidden": false, "fontFace": "DejaVuSans Mono for Powerline" }, { //Makechangesheretothecmd.exeprofile. "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "name": "CommandPrompt", "commandline": "cmd.exe", "hidden": false }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "fontFace": "DejaVuSans Mono for Powerline", "colorScheme": "Solarized Dark", }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "AzureCloud Shell", "source": "Windows.Terminal.Azure" } ] }, //Addcustomcolorschemestothisarray. //Tolearnmoreaboutcolorschemes,visithttps://aka.ms/terminal-color-schemes "schemes": [ { "background": "#073642", "black": "#073642", "blue": "#268BD2", "brightBlack": "#002B36", "brightBlue": "#839496", "brightCyan": "#93A1A1", "brightGreen": "#586E75", "brightPurple": "#6C71C4", "brightRed": "#CB4B16", "brightWhite": "#FDF6E3", "brightYellow": "#657B83", "cyan": "#2AA198", "foreground": "#FDF6E3", "green": "#859900", "name": "Solarized Dark", "purple": "#D33682", "red": "#D30102", "white": "#EEE8D5", "yellow": "#B58900" }, { "background": "#FDF6E3", "black": "#073642", "blue": "#268BD2", "brightBlack": "#002B36", "brightBlue": "#839496", "brightCyan": "#93A1A1", "brightGreen": "#586E75", "brightPurple": "#6C71C4", "brightRed": "#CB4B16", "brightWhite": "#FDF6E3", "brightYellow": "#657B83", "cyan": "#2AA198", "foreground": "#073642", "green": "#859900", "name": "Solarized Light", "purple": "#D33682", "red": "#D30102", "white": "#EEE8D5", "yellow": "#B58900" }, ], //Addcustomkeybindingstothisarray. //Tounbindakeycombinationfromyourdefaults.json,setthecommandto"unbound". //Tolearnmoreaboutkeybindings,visithttps://aka.ms/terminal-keybindings "keybindings": [ //CopyandpasteareboundtoCtrl+Shift+CandCtrl+Shift+Vinyourdefaults.json. //ThesetwolinesadditionallybindthemtoCtrl+CandCtrl+V. //Tolearnmoreaboutselection, visithttps://aka.ms/terminal-selection { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" }, { "command": "paste", "keys": "ctrl+v" }, //PressCtrl+Shift+Ftoopenthesearchbox { "command": "find", "keys": "ctrl+shift+f" }, //PressAlt+Shift+Dtoopenanewpane. //-"split":"auto"makesthispaneopeninthedirectionthatprovidesthemostsurfacearea. //-"splitMode":"duplicate"makesthenewpaneusethefocusedpane'sprofile. //Tolearnmoreaboutpanes, visithttps://aka.ms/terminal-panes { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }, {"command": "closeTab", "keys": "ctrl+w"}, {"command": "newTab", "keys": "ctrl+t"} ] }

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

Linux和Windows终端:Aminal

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

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

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

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

Linux系统:Gnome终端

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

打开网易新闻 查看精彩图片

图源:Martin Thoma

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

打开网易新闻 查看精彩图片

图源:Martin Thoma

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

· 背景: #2e3436 / #555753

· 深红: #cc0000 / 浅红: ef2929

· 深绿: #4e9a06 / 浅绿: #8ae234

· 深黄: #c4a000 / 浅黄: #fce94f

· 深蓝: #3465a4 / 浅蓝: #729fcf

· 深紫: #75507b / 浅紫: #ad7fa8

· 深蓝绿: #06989a / 浅蓝绿: #34e2e2

· 深灰: #d3d7cf /浅灰: #eeeeec

打开网易新闻 查看精彩图片

Linux系统:fish shell

安装fish shell:

sudo apt-get install fish

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

接着安装“Oh My Fish”

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

然后将主题设置为agnoster:

omf install agnosteromf theme agnoster

别名

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

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

alias short='long'

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

# Shorten thingsalias ..='cd ..'alias ...='cd ../../'alias ll='ls -alF'alias la='ls -A'alias l='ls -CF'alias c='clear'# If your terminal supports colors, use them!alias ls='ls --color=auto'alias grep='grep --color=auto'alias fgrep='fgrep --color=auto'alias egrep='egrep --color=auto'alias diff='colordiff'# Works only if you have notify-sendalias alert='notify-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主题。

打开网易新闻 查看精彩图片

留言点赞关注

我们一起分享AI学习与发展的干货

如转载,请后台留言,遵守转载规范