百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

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

liuian 2024-11-28 00:50 27 浏览

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



在十多年的开发生涯中,笔者热衷于使用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:



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


Windows终端


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



可以选择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常规字体。



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



颜色设置为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 agnoster
omf 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 things
alias ..='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-send
alias 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学习与发展的干货

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

相关推荐

打开新世界,教你用RooCode+Copliot+Mcp打造一个自己的Manus

本文耗时两天打造,想要一遍走通需要花点时间,建议找个专注的时间开搞!这不仅是个免费使用claude3.5的方案,也是一个超级智能体方案,绝对值得一试!最近Manus真是赚足了眼球,然而我还是没有邀请码...

Git仓库(git仓库有哪些)

#Git仓库使用方法流程详解##一、环境搭建与基础配置###1.1安装与初始化-**安装Git**:官网下载安装包,默认配置安装-**配置全局信息**:```bashgitconfig...

idea版的cursor:Windsurf Wave 7(ideawalk)

在企业环境中,VisualStudioCode和JetBrains系列是最常用的开发工具,覆盖了全球绝大多数开发者。这两类IDE各有优势,但JetBrains系列凭借其针对特定语言和企业场景的深度...

Ai 编辑器 Cursor 零基础教程:推箱子小游戏实战演练

最近Ai火的同时,Ai编辑器Cursor同样火了一把。今天我们就白漂一下Cursor,使用免费版本搞一个零基础教程,并实战演练一个“网页版的推箱子小游戏”。通过这篇文章,让你真正了解cursor是什么...

ChatGPT深度集成于苹果Mac软件 编码能力得到提升

【CNMO科技消息】近日,OpenAI发布了针对MacOS的桌面应用程序,并宣布了一系列与各类应用程序的互操作性功能,标志着ChatGPT正在从聊天机器人向AI智能体工具进化。此次发布的MacOS桌面...

日常开发中常用的git操作命令和使用技巧

日常开发中常用的git操作命令,从配置、初始化本地仓库到提交代码的常用git操作命令使用git前的配置刚使用git,先要在电脑上安装好git,接着我们需要配置一下帐户信息:用户名和邮箱。#设置用户名...

Trae IDE 如何与 GitHub 无缝对接?

TraeIDE内置了GitHub集成功能,让开发者可以直接在IDE里管理代码仓库和版本控制。1.直接从GitHub克隆项目如果你想把GitHub上的代码拉到本地,Trae提供了...

China's diplomacy to further provide strong support for country's modernization: FM

BEIJING,March7(Xinhua)--ChineseForeignMinisterWangYisaidFridaythatChina'sdiplomacywil...

三十分钟入门基础Go(Java小子版)(java入门级教程)

前言Go语言定义Go(又称Golang)是Google的RobertGriesemer,RobPike及KenThompson开发的一种静态、强类型、编译型语言。Go语言语法与...

China will definitely take countermeasures in response to arbitrary pressure: FM

BEIJING,March7(Xinhua)--Chinawilldefinitelytakecountermeasuresinresponsetoarbitrarypre...

Go操作etcd(go操作docker实现沙箱)

Go语言操作etcd,这里推荐官方包etcd/clientv3。文档:https://pkg.go.dev/go.etcd.io/etcd/clientv3etcdv3使用gRPC进行远程过程调...

腾讯 Go 性能优化实战(腾讯游戏优化软件)

作者:trumanyan,腾讯CSIG后台开发工程师项目背景网关服务作为统一接入服务,是大部分服务的统一入口。为了避免成功瓶颈,需要对其进行尽可能地优化。因此,特别总结一下golang后台服务...

golang 之JWT实现(golang gin jwt)

什么是JSONWebToken?JSONWebToken(JWT)是一个开放标准(RFC7519),它定义了一种紧凑且自包含的方式,用于在各方之间以JSON方式安全地传输信息。由于此信息是经...

一文看懂 session 和 cookie(session cookie的区别)

-----------cookie大家应该都熟悉,比如说登录某些网站一段时间后,就要求你重新登录;再比如有的同学很喜欢玩爬虫技术,有时候网站就是可以拦截住你的爬虫,这些都和cookie有关。如果...

有望取代 java?GO 语言项目了解一下

GO语言在编程界一直让人又爱又恨,有人说“GO将统治下一个十年”,“几乎所有新的、有趣的东西都是用Go写的”;也有人说它过于死板,使用感太差。国外有Google、AWS、Cloudflar...