mac终端升级
数据科学家工具箱 (Data Scientist’s Toolbox)
Have you ever bought a mattress?
你买过床垫吗?
Let me tell you about my experience with the notorious mattress salesmen.
让我告诉您我在臭名昭著的床垫销售员那里的经历。
Salesmen would talk their way into making you buy something you don’t really need or a more expensive version of something you do need.
推销员会说服他们让您购买真正不需要的东西,或者购买确实需要的东西的较贵版本。
Personally, I like to shop in peace. That means I would prefer not having a salesman follow me while I browse. However, it is nearly impossible to find a mattress store without salesmen.
就个人而言,我喜欢安心购物。 这意味着在浏览时,我宁愿不要推销员跟着我。 但是,几乎不可能找到没有推销员的床垫商店。
Once you heard one of the sales pitches, you realise that all of them are exactly the same. Almost like the script of a movie that you watch over and over again.
听到其中一种推销音后,您就会意识到它们都是完全相同的。 就像您一次又一次观看电影的剧本一样。
“Our beds are good for your spine.”
“我们的床对您的脊椎有益。”
“Think about the medical bill you would have later on in life if you sleep on bad mattress.”
“想想如果您睡在不好的床垫上,以后会遇到的医疗费用。”
“What’s the point of having money if you could not enjoy it?”
“如果您不能享受金钱,那有什么意义呢?”
At one point though, one of the pitches stood out.
然而,在某一时刻,其中一个球场显得突出。
“How much time do you spend everyday sitting in your car? Two hours? And how much does your car cost? Similarly, how much time do you spend everyday on the bed?”
“您每天花多少时间坐在车上? 两个小时? 您的汽车要花多少钱? 同样,您每天在床上花费多少时间?”
Then he showcased a $5,000 mattress.
然后,他展示了一张价值5,000美元的床垫。
I’m not here to tell you to buy a $5,000 mattress, though.
不过,我并不是要告诉您购买5,000美元的床垫。
What I learned from that sales pitch is you should pay more attention to the things you actually use in your daily life.
我从销售推销中学到的是,您应该更加注意自己在日常生活中实际使用的东西。
If you work as either a software engineer, machine learning engineer, or data scientist, there is a high chance that:
如果您是软件工程师,机器学习工程师或数据科学家,则很有可能:
- You use MacOS 您使用MacOS
- You spend most of your time using the terminal 您将大部分时间都花在使用终端上
I believe that improving the whole terminal experience, something you use everyday, is a good investment that will be worth it in the long run.
我相信,改善日常使用的整个终端体验是一项不错的投资,从长远来看,这是值得的。
That being said, are you still using the default MacOS terminal?
话虽如此,您仍在使用默认的MacOS终端吗?
终点站 (The Terminal)
There are a lot of popular terminal replacements out there. I have tried some of them, and here’s what I would recommend to my friends.
那里有很多受欢迎的终端机替代品。 我已经尝试过其中的一些,这就是我向朋友推荐的内容。
iTerm2 (iTerm2)
By far the most powerful replacement for MacOS terminal.
迄今为止,MacOS终端的最强大替代品。
On a glance, it looks very similar to the default terminal. However, it comes with a lot of features that are not present in the Terminal and other terminal replacements.
乍一看,它看起来与默认终端非常相似。 但是,它具有许多功能,这些功能在终端机和其他终端机替代品中不存在。
In my opinion, the one feature that stood out the most is Profile.
我认为,最引人注目的一项功能是Profile。
Being a developer, most of the time you will be accessing remote servers to work on them. Each of the server will have unique IP address, and you don’t want to remember the IP for each one.
作为开发人员,大多数时候您将访问远程服务器以在其上工作。 每个服务器都将具有唯一的IP地址,并且您不想记住每个服务器的IP。
You could create an entry in the ssh config file ~/.ssh/config
that looks like this:
您可以在ssh配置文件~/.ssh/config
中创建一个条目,如下所示:
Host work_server
HostName <IP address>
User <username>
Then, you could simply type ssh work_server
instead of manually typing the IP address over and over again.
然后,您只需键入ssh work_server
而不必ssh work_server
手动键入IP地址。
In iTerm2, you can create a Profile for every machine instead. Here’s the setup for a simple profile to ssh into a server.
在iTerm2中,可以为每台计算机创建一个Profile。 这是一个简单的配置文件ssh进入服务器的设置。
After that, you could simply select the profile from the drop down menu to open a new terminal tab where you are already connected through ssh into that machine.
之后,您可以简单地从下拉菜单中选择配置文件,以打开一个新的终端选项卡,在该选项卡中您已经通过ssh连接到该计算机。
This is just one of the use cases of how I use Profiles, there are a lot of other use cases for this feature.
这只是我使用个人档案的用例之一,此功能还有很多其他用例。
Do you use different conda environment for each project? Create a profile to directly open the project’s working directory and run the command conda activate environment
to save your time.
每个项目是否使用不同的conda环境? 创建一个配置文件以直接打开项目的工作目录,并运行conda activate environment
命令以节省您的时间。
One thing to note though, if you’re not using the Profile to ssh, you need to change the Command to Login Shell instead, and then add the command at the Send text at start
field. You could also run multiple commands by separating them with ;
不过要注意的一件事是,如果您不使用Profile来ssh,则需要将Command更改为Login Shell,然后Send text at start
字段中添加命令。 您也可以使用;
将它们分开来运行多个命令;
For the default starting directory, you could put cd /path/to/workdir
inside the Send text at start
or specify it in the Directory
section.
对于默认的起始目录,可以将cd /path/to/workdir
放在“ Send text at start
或在“ Directory
部分中指定它。
Want to make your flow even faster? Specify a shortcut key to open a Profile using hotkeys, such as ⌘^A.
是否想让您的流程更快? 指定快捷键以使用诸如^ A之类的热键打开配置文件。
超 (Hyper)
Do you like beautiful terminal straight out-of-the-box? This might be the one for you.
您是否喜欢开箱即用的漂亮终端? 这可能是您的最佳选择。
Easily the best looking terminal replacement for MacOS, Hyper is built using HTML, CSS, and JS.
Hyper是使用HTML,CSS和JS构建的,很容易成为MacOS的最佳外观终端替代品。
One notable feature that differentiates Hyper from the other terminal is its plugins. It has tons of custom plugins made just for Hyper. Here’s a repository called awesome-hyper that contains curated Hyper plugins complete with a short description of them.
插件是Hyper与其他终端区别的一个显着特征。 它有大量专门为Hyper设计的自定义插件。 这是一个名为awesome-hyper的存储库 包含精选的Hyper插件,并附有简短描述。
You could easily install plugins and even change themes or color schemes by editing ~/.hyper.js
configuration file.
您可以通过编辑~/.hyper.js
配置文件轻松安装插件,甚至更改主题或配色方案。
There are a lot of interesting plugins, such as hypergoogle
which enables you to search google straight through Hyper. It will open a new browser window though, so you don’t have to worry about navigating a webpage inside the terminal. hyper-spotify
will show currently playing song on Spotify at the top or bottom of your terminal and lets you control it, perfect for developers who don’t want to stay in the terminal at all time.
有很多有趣的插件,例如hypergoogle
,它使您可以直接通过Hyper搜索google。 但是,它将打开一个新的浏览器窗口,因此您不必担心在终端机内部浏览网页。 hyper-spotify
会在终端的顶部或底部在Spotify上显示当前正在播放的歌曲,并让您对其进行控制,非常适合不想一直留在终端中的开发人员。
Unfortunately, Hyper does not have the Profiles feature like iTerm2. It’s a perfectly good and capable terminal, but the convenience of Profiles kept pulling me back towards iTerm2.
不幸的是,Hyper没有像iTerm2这样的配置文件功能。 这是一个非常优秀且功能强大的终端,但是Profiles的便利性使我一直回到iTerm2。
荣誉奖: 敏捷 (Honorable mention: Alacritty)
Alacritty takes pride in being “the fastest terminal emulator in existence.” The secret? They use GPU to power up the terminal, which result in very fast rendering compared to other terminal.
Alacritty以成为“现有最快的终端模拟器”而感到自豪。 秘密? 他们使用GPU为终端供电,与其他终端相比,渲染速度非常快。
The downside? It consumes a lot of power.
不足之处? 它消耗很多功率。
If you are always working from a desk and your machine is always connected with the charging cable, then it might not seem that bad. On the other hand, if you sometimes work from the comfort of your sofa, it would become uncomfortable quite fast once the bottom of your laptop becomes hot and the battery needs to be recharged much more often.
如果您始终在办公桌旁工作,并且您的机器始终与充电电缆相连,那么看起来似乎还不错。 另一方面,如果您有时在舒适的沙发上工作,则一旦笔记本电脑的底部变热并且需要更频繁地为电池充电,它就会变得非常不舒服。
On top of that, Alacritty also does not support Profiles like iTerm2, so it’s not for me.
最重要的是,Alacritty还不支持像iTerm2这样的配置文件,因此不适合我。
It depends on your main focus though. If you are all about that speed and battery drain is not a problem for you, then by all means, this is the one for you.
这取决于您的主要重点。 如果您只追求那种速度,而电池消耗对您来说不是问题,那么从某种意义上说,这就是您的选择。
自定义终端 (Customising Your Terminal)
Now that you have chosen your terminal, it’s time to start customising them.
现在您已经选择了终端,是时候开始自定义它们了。
No matter which terminal you picked, the customisation should work for any of them, even if you decide to use entirely different terminal than the ones listed above.
无论选择哪个终端,即使您决定使用与上述终端完全不同的终端,自定义设置也应适用于其中任何一个终端。
Here’s a quick script that I use to install Homebrew, zsh, oh-my-zsh, and powerlevel10k.
这是我用来安装Homebrew,zsh,oh-my-zsh和powerlevel10k的快速脚本。
Copying the commands and executing them line-by-line is okay, since there are only a few lines.
可以复制命令并逐行执行它们,因为只有几行。
You could also copy and save the content to a file called terminal_upgrade.sh
and then open the terminal, cd
into the directory, and give permission to execute by using this command.
您还可以将内容复制并保存到名为terminal_upgrade.sh
的文件中,然后打开终端,进入目录cd
,并使用此命令授予执行权限。
chmod +x terminal_upgrade.sh
After that, execute this command to run the script.
之后,执行此命令以运行脚本。
./terminal_upgrade.sh
Do note that it could not automatically install the powerlevel10k theme though, please read the Powerlevel10k section for further instructions.
请注意,尽管它不能自动安装powerlevel10k主题,请阅读Powerlevel10k部分以获取更多说明。
I would go into more details for every item in the sections below.
我将在以下各节中为每一项提供更多详细信息。
家酿 (Homebrew)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Homebrew is essential for any developer that uses MacOS. It is a package manager that allows you to install a lot of things directly from the terminal.
对于使用MacOS的任何开发人员来说,自制软件都是必不可少的。 它是一个软件包管理器,它使您可以直接从终端安装很多东西。
Almost any mainstream package is installable through Homebrew, and you could install even more stuffs by installing cask for Homebrew.
几乎所有主流软件包都可以通过Homebrew安装,并且您可以通过为Homebrew安装cask来安装更多东西。
brew install cask
Even you could install iTerm2 through Homebrew’s cask.
甚至您也可以通过Homebrew的容器安装iTerm2。
brew cask install iterm2
In short, this is a must have package manager, just like apt
in Linux.
简而言之,这是一个必须具有的软件包管理器,就像Linux中的apt
一样。
sh (zsh)
brew install zsh zsh-completions
If iTerm2 is the replacement for Terminal, zsh is the replacement for bash.
如果iTerm2替代Terminal,则zsh替代bash。
Short for Z shell, it is built on top of sh (Bourne Shell), just like its counterpart bash (Bourne Again Shell).
Z shell的缩写,它建立在sh(Bourne Shell)的顶部,就像它的对应bash(Bourne Again Shell)一样。
It has minor improvements such as not needing to type cd
to navigate through the documents.
它有一些小的改进,例如不需要键入cd
即可浏览文档。
I rarely use the feature since not all terminal you will work on is using zsh, so it’s a good practice to use cd
even though you know zsh doesn’t need that.
我很少使用该功能,因为并非您将要使用的所有终端都使用zsh,因此即使您知道zsh不需要cd
,这也是使用cd
的一种好习惯。
However, the main reason you should use zsh is because of oh-my-zsh.
但是,应使用zsh的主要原因是因为oh-my-zsh。
我的天啊 (oh-my-zsh)
sh -c “$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Oh-my-zsh is a framework for zsh that makes it much easier to install and manage plugins for zsh.
Oh-my-zsh是zsh的框架,可以更轻松地安装和管理zsh插件。
Just like the plugins for Hyper, oh-my-zsh’s plugins are defined inside a file. In this case, it’s the ~/.zshrc
file.
就像Hyper的插件一样,oh-my-zsh的插件在文件中定义。 在这种情况下,它是~/.zshrc
文件。
One thing to note, this is a completely different plugin manager than the one Hyper has. It’s possible to use both Hyper and oh-my-zsh, just keep in mind plugins for Hyper is defined in ~/.Hyper.js
and plugins for oh-my-zsh is inside ~/.zshrc
需要注意的是,这是与Hyper完全不同的插件管理器。 它可以同时使用超和OH-MY-zsh的,只要记住插件超在定义~/.Hyper.js
和插件哦,我的-的zsh里面~/.zshrc
Here’s a list of notable plugins for oh-my-zsh, and also list of themes for oh-my-zsh.
这是oh-my-zsh的著名插件的列表,以及oh-my-zsh 的主题列表 。
To install the plugins you desire, simply put it inside the plugins=()
line inside the oh-my-zsh configuration file.
要安装所需的插件,只需将其放在oh-my-zsh配置文件内的plugins=()
行中即可。
For themes, you could specify which theme you want to use in the line specifying ZSH_THEME=...
line, which brings us to our last point.
对于主题,您可以在指定ZSH_THEME=...
行的行中指定要使用的主题,这将我们带到最后一点。
功率水平10k (Powerlevel10k)
cd $ZSH_CUSTOM/themesgit clone --depth=1 https://github.com/romkatv/powerlevel10k.git
Powerlevel10k is the much faster version of its predecessor (Powerlevel9k) and packed with more features.
Powerlevel10k是其前身(Powerlevel9k)的更快版本,并具有更多功能。
In short, it’s my go-to theme for the terminal.
简而言之,这是终端的我的首选主题。
After running the script to clone Powerlevel10k inside the $ZSH_CUSTOM/themes
directory, you need to edit the line inside ~/.zshrc
that defines the ZSH_THEME
to the following.
运行脚本以在$ZSH_CUSTOM/themes
目录中克隆$ZSH_CUSTOM/themes
,您需要在~/.zshrc
ZSH_THEME
编辑以下定义ZSH_THEME
的行。
ZSH_THEME=powerlevel10k/powerlevel10k
Reload the zsh for the change to take effect by executing
通过执行以下命令重新加载zsh以使更改生效
source ~/.zshrc
It should start the Powerlevel10k configuration wizard. If it doesn’t run p10k configure
to manually start the wizard and follow the on-screen instructions to choose how you want the terminal to look like.
它应该启动Powerlevel10k配置向导。 如果未运行, p10k configure
为手动启动向导,然后按照屏幕上的说明选择所需的终端外观。
If you have a lot of plugins in your oh-my-zsh, it may take a while to load the plugins when you first open the terminal. Powerlevel10k has the instant prompt feature to let you start using the terminal while the plugins are loaded in the background.
如果您的oh-my-zsh中有很多插件,那么在您第一次打开终端时可能需要一段时间才能加载这些插件。 Powerlevel10k具有即时提示功能,可让您在将插件加载到后台的同时开始使用终端。
恭喜啦 (Congratulations)
You are now officially a member of the Enhanced MacOS Terminal Club. Gone are the days where you stare at mediocre terminal sessions.
您现已正式成为增强型MacOS终端俱乐部的成员。 盯着普通的终端会议的日子已经一去不复返了。
Here’s what your terminal should look like if you did install everything listed above.
如果您确实安装了上面列出的所有内容,那么您的终端应该是这样的。
It might look a bit different if you choose different customisation option inside Powerlevel10k, but in general it’s similar. Personally, I like the way it shows the active Conda environment and also the active branch of the repository.
如果您在Powerlevel10k中选择不同的自定义选项,则可能看起来有些不同,但通常情况是相似的。 就个人而言,我喜欢它显示活动的Conda环境以及存储库的活动分支的方式。
Having a beautiful terminal alone will not make you a better programmer, but it will surely help you on your way to being one. Here’s another article of mine that could help you master the terminal.
仅仅拥有一个漂亮的终端并不能使您成为一名更好的程序员,但是它一定会帮助您成为一名程序员。 这是我的另一篇文章,可以帮助您掌握终端。
It shows how you could easily chain programs inside the terminal, create your own custom commands, and some useful commands that you may or may not have used yet.
它显示了如何轻松地将程序链接到终端中,如何创建自己的自定义命令以及一些您可能已经使用或尚未使用的有用命令。
One last thing to note, there is no wrong way to customise your terminal. It’s your terminal, not anyone else’s.
最后要注意的一点是,自定义终端没有错误的方法。 这是您的终端,而不是其他任何人的终端。
Everything I listed above is what I always do to my own terminal since I started my career as a data scientist. Your needs may be different than mine and that is perfectly fine.
自从我作为数据科学家开始职业生涯以来,上面列出的所有内容都是我一直在自己的终端上所做的事情。 您的需求可能与我的有所不同,这很好。
It doesn’t matter how you customise your terminal, but I hope it makes you aware of how awful the default MacOS Terminal is.
定制终端的方式无关紧要,但是我希望它使您知道默认MacOS终端的糟糕程度。
You know what Robin Sharma said, “Own Your Terminal. Elevate Your Life” or was it something else?
您知道Robin Sharma所说的话: “拥有您的终端机。 提升生活”还是其他?
翻译自: https://towardsdatascience.com/maybe-its-time-to-upgrade-your-terminal-experience-a9e12b2af77
mac终端升级