凯蒂阿姨的自定义 Mac 终端
终端 Mac

> 
> 开源意味着我可以在任何终端上找到熟悉的 Linux。
> 
> 
> 


![](/data/attachment/album/202008/21/002323xqslvqnnmdz487dq.jpg)


十年前，我开始了我的第一份工作，它要求我使用 Linux 作为我的笔记本电脑的操作系统。如果我愿意的话，我可以使用各种 Linux 发行版，包括 Gentoo，但由于我过去曾短暂地使用过Ubuntu，所以我选择了 Ubuntu Lucid Lynx 10.04。


我的 [Konsole](https://konsole.kde.org/) 终端使用的是 [Zenburn](https://github.com/brson/zenburn-konsole) 主题，有一个类似于这样的 Bash 提示符：



```
machinename ~/path/to/folder $

```

现在，我使用 Mac，具体来说是 macOS Catalina，我使用 [iTerm2](https://www.iterm2.com/) 与 [Zenburn 主题](https://gist.github.com/fooforge/3373215)，zsh 提示符是这样的：



```
machinename ~/path/to/folder
$

```

我想，十年来几乎相同的提示符，我已经赢得了*老古板*的称号，不过这只是标志着，我的喜好和习惯与现在耍酷的孩子们不一样而已。


仿佛是为了证明我的古板观点，我想改变我的终端和我的旧终端一样。在 Mac 上获得一个看起来和感觉像 Lucid Lynx 的设置并不简单，而且我很花了一些时间。


我最近最大的改变是从 Bash 转移到 zsh，并迁移了我的 [Bash 魔改](https://opensource.com/article/20/1/bash-scripts-aliases)。但这只是其中一个重大的转变。我学到了许多新式的经验，现在我把这些经验赠送给你，亲爱的读者。


### Coreutils 对选项的顺序更宽容


从 Ubuntu 转移到 macOS 并没有太大的转变，直到我开始觉得我失去了 Unix 范。我试着运行一些基本的操作，比如删除文件夹，但却被告知我错误地调用了 `rm`。


事实证明，GNU 风格的实用程序可能看起来 BSD 风格的差不多，但最大的可用性差异之一是*参数顺序*。未命名参数的排列顺序并不一致。例如：`rm`。


下面是我们熟悉的 GNU 风格的删除目录的命令：



```
$ rm path/to/folder -rf

```

这与同一命令的 BSD 风格版本形成鲜明对比：



```
$ rm path/to/folder -rf
rm: path/to/folder: is a directory
rm: -rf: No such file or directory

```

我通过 [Homebrew](https://opensource.com/article/20/6/homebrew-mac) 安装 [Coreutils](https://formulae.brew.sh/formula/coreutils) 解决了这个问题。这将 GNU 实用程序引入到了 macOS，并使我不必为那些已经深深扎根于我的肌肉记忆中的命令记住选项顺序，从而对选项顺序更加宽容。


### 强大的 iTerm2


我不知道有哪个操作系统的资深用户会对默认终端满意。在 macOS 这块土地上，我选择了 [iTerm2](https://www.iterm2.com/)，它允许我比基本的操作系统终端应用更灵活。我最喜欢的 iTerm 强大功能之一是能够使用 `Command+D` 和 `Command+Shift+D` 来垂直和水平分割窗格。还有很多技巧需要学习，但仅是简单的分割窗格就值得用 iTerm2 换掉默认终端。


### 上下文感知的插件


即使是一个古板的用户也会自定义终端提示，其中一个原因是为了获得一些情境感知。我喜欢终端给我提供上下文，并回答所有想到的问题。不仅仅是我在哪个文件夹里，而是：我在什么机器上？这是个 Git 仓库吗？如果是，我在哪个分支？我是在 Python 虚拟环境中吗？


这些问题的答案最终都归结为一类称之为“上下文感知插件”的终端扩展。


对于当前的 Git 分支，我使用了这个 [parse\_git\_branch()](https://gist.github.com/kevinchappell/09ca3805a9531b818579) 方法（如果你使用的是 [Oh My Zsh](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git)，也有类似的插件）。对于 Python 来说，virtualenv 会自动给提示符加前缀。Oh My Zsh 有如此多的[插件](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins)，你一定能找到改善你生活的东西。


至于我的本地机？我就直接用 `PS1` 格式，因为我喜欢这样的基本信息，而且 macOS 并没有*真正*让你给机器起个名字。


### 多行提示符也不错


观察力强的读者可能会注意到，十年来我的提示符有一个变化，就是现在它是两行。这是最近的一个变化，我慢慢学会了喜欢，因为我前面提到的所有这些插件都让我的提示符变得很长很长。你在文件系统中导航不能太深，要不你试图做任何基本的事情都会输入换行。随之而来的是偶尔的重绘问题和可读性问题。


我收到的关于解决这个问题的建议大多围绕着“哦，你在用 zsh？用 [Powerlevel10k](https://github.com/romkatv/powerlevel10k) 吧！”这对于像我这样不固步自封的人来说是不错的，但我能够从这些主题中学习到一些，并从中获取一点技巧。


我所做的是在我的提示符中的最后一个 `$` 前加一个 `$'\n'`，这样我的上下文信息 —— 当前机器、当前文件夹、当前 GitHub 分支、当前 virtualenv 等等 —— 都可以在一行中出现，然后我的命令就可以顺利输入了。


我发现唯一的问题是学会在哪里*看*。我习惯于让我的眼睛从行的中心开始，因为以前我的提示符就是从那里开始的。我正在慢慢学会向左看提示符，但这是一个缓慢的过程。我有十几年的眼睛习惯要撤销。


### 使用适合你的方法


如果你喜欢某种风格或工具，那么你的这种偏好是绝对有效的。你可以尝试其他的东西，但千万不要认为你必须使用最新和最伟大的，只是为了像很酷的孩子一样。你的风格和喜好可以随着时间的推移而改变，但千万不要被迫做出对你来说不舒服的改变。


*等下一次，凯蒂阿姨再给你吐槽一下 IDE。*




---


via: <https://opensource.com/article/20/7/mac-terminal>


作者：[Katie McLaughlin](https://opensource.com/users/glasnt) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
A decade ago, I started my first job that required me to use Linux as my laptop's operating system. I was offered a range of variants, including Gentoo, if I was so inclined, but since I had used Ubuntu briefly in the past, I opted for Ubuntu Lucid Lynx 10.04.

My terminal, [Konsole](https://konsole.kde.org/), was themed in [Zenburn](https://github.com/brson/zenburn-konsole) and had a Bash prompt that looked like this:

`machinename ~/path/to/folder $`


Nowadays, I'm running on a Mac, specifically macOS Catalina using [iTerm2](https://www.iterm2.com/) with the [Zenburn theme](https://gist.github.com/fooforge/3373215) and a zsh prompt that looks like this:

```
machinename ~/path/to/folder
$
```


I think after a decade with a near-identical prompt, I have earned the title of *curmudgeon*, if only as a sign that I have preferences and habits that go against what the cool kids are doing nowadays.

As if to prove my curmudgeonly point, I wanted to change my terminal to match my old one. Getting a setup that looks and feels like Lucid Lynx on Mac isn't simple and took some time.

My biggest recent change was moving from Bash to zsh and migrating my [Bash hacks](https://opensource.com/article/20/1/bash-scripts-aliases). But that was only one of the major shifts. I learned many new-fangled lessons that I now bestow onto you, dear reader.

## Coreutils forgives flag order

Moving from Ubuntu to macOS wasn't too much of a shift until I started thinking I was losing my Unix-foo. I'd try running basic operations like removing folders and be told that I was invoking `rm`

incorrectly.

It turns out that the GNU-style utilities may look like BSD-style utilities, but one of the biggest usability differences is *flag order*. The order in which unnamed parameters are listed does not line up. For instance: `rm`

.

Here's the familiar GNU-style command to remove a directory:

`$ rm path/to/folder -rf`


This contrasts with the BSD-style version of the same command:

```
$ rm path/to/folder -rf
rm: path/to/folder: is a directory
rm: -rf: No such file or directory
```


I got around this by installing [Coreutils](https://formulae.brew.sh/formula/coreutils) through [Homebrew](https://opensource.com/article/20/6/homebrew-mac). This brings GNU utilities to macOS and makes flag order more forgiving by allowing me to not have to remember flag order for commands that are deeply ingrained into my muscle memory.

## iTerm2 is powerful

I'm not sure of any operating system where power users are happy with the default terminal. In macOS land, I settled on [iTerm2](https://www.iterm2.com/), which allows me more flexibility than the base OS's terminal application. One of my favorite iTerm power features is being able to use **Command**+**D** and **Command**+**Shift**+**D** to split panes vertically and horizontally. There are many more tricks to be learned, but easy split panes alone can make iTerm2 worth the switch from the default option.

## Context-aware plugins


One reason even a curmudgeon of a user customizes a terminal prompt is to gain some situational awareness. I enjoy it when a terminal gives me context and answers all the questions that come to mind. Not just what folder I'm in, but: What machine am I on? Is this a Git repository? If so, what branch am I in? Am I in a Python virtual environment?

Answers to these questions go into a category of terminal extensions that can be called "context-aware plugins."

For the current Git branch, I used this [parse_git_branch()](https://gist.github.com/kevinchappell/09ca3805a9531b818579) method (there is a similar plugin for [Oh My Zsh](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git), if you're using that). For Python, virtualenv prefixes to the prompt automatically. Oh My Zsh has so many [plugins](https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins), you're sure to find something to improve your life.

As for my local machine? I just place it directly in the PS1 format because I'm basic like that, and macOS doesn't *really* let you name your machines.

## Multi-line prompts are fine

The observant reader may notice the one change in my prompt over a decade is that it's now two lines. This is a recent change that I'm slowly learning to love because all those plugins I mentioned earlier make my prompt looonnngggg. You can navigate only so deep in a filesystem before you start having line-wrapped command inputs trying to do anything basic. And with that comes occasional redraw issues and readability concerns.

The suggestions I received about resolving this revolved mostly around, "Oh, you're using zsh? Use [Powerlevel10k](https://github.com/romkatv/powerlevel10k)!" Which is fine for those who aren't stuck in their ways, like me. But I was able to learn from these themes and take a small bit of suggestion from them.

What I've done is to add a `$'\n'`

before the final `$`

in my prompt, which allows my context-aware information—current machine, current folder, current GitHub branch, current virtualenv, and the like—to all live on one line, and then my commands can be entered without issues.

The only problem I've found is learning where to *look*. I'm used to having my eyes start at the center of the line because that's where the prompt used to start. I'm slowly learning to look left to the prompt, but it's a slow process. I have a decade of eye training to undo.

## Use what works for you

If you prefer a certain style or tool, then you are absolutely valid in that preference. You can try other things, but never think you have to use the latest and greatest just to be like the cool kids. Your style and preferences can change over time, but never be forced into changes that aren't comfortable for you.

*Join us next time, when Aunty Katie complains about IDEs.*

## 3 Comments