使用 tmux 和 Git 自定义我的 Linux 终端
终端 提示符

> 
> 设置你的控制台，以便你能知道身处哪个目录和该做什么
> 
> 
> 


![](/data/attachment/album/202007/25/102841u8u6ns4tp9z8bnha.jpg)


我使用 GNOME 终端，主要是因为它是我的发行版的默认设置。但是我终端内远非“默认值”。在我开始解释如何自定义它之前，它现在是这个样子：


![Moshe Zadka's terminal](/data/attachment/album/202007/25/102923ak3j6pqhmkfgfhpg.png "Moshe Zadka's terminal")


### 从底部开始


我使用终端多路复用技术 [tmux](https://opensource.com/article/20/1/tmux-console) 管理我的终端体验。


在上图的底部，你可以看到我的绿色 tmux 栏。底部的 `[3]` 表示它是第三个终端：每个终端都运行自己的 tmux 会话。（我创建了一个新会话来放大字体，这样可在截图中更容易看到；这是它与其他终端之间的唯一区别。）


提示符看起来也很有趣，对吧？在提示符中塞入了太多信息后，我喜欢插上一个换行符，这样一来，如果我想即兴进行 shell 编程或编写一个传递了五次的管道，也不会超出屏幕界限。这样做的代价是简单的命令序列（新建、复制、移动）会更快地滚动出我的屏幕。


行末是 <ruby> <a href="https://simple.wikipedia.org/wiki/Aleph_null">  阿列夫零 </a> <rt>  Aleph null </rt></ruby> 字符，它是最小的[无穷基数](https://gizmodo.com/a-brief-introduction-to-infinity-5809689)。我希望内容行的结束很明显，并且当我意识到“阿列夫”和下标 0 都是 Unicode 字符时，我无法抗拒使用“阿列夫零”作为提示符的一部分的诱惑。（数学极客们，团结起来！）


在此之前是我的用户名。由于我在不同用户名的多台计算机上使用相同的[点文件](https://opensource.com/article/19/3/move-your-dotfiles-version-control)（保存在 Git 中），因此这个还算有用。


在我的用户名之前，是我所在目录的最后一部分。完整路径通常太长且无用，而当前目录对于像我这样的经常忘记在做什么人来说是很有用的。在此之前是机器的名称。我所有的机器都以我喜欢的电视节目命名。我的旧笔记本是 `mcgyver`。


提示符中的第一位是我最喜欢的：一个让我知道目录的 Git 状态的字母。如果目录为“不在 Git 中”，那么是 `G`。如果目录为“没有问题”（OK），且无需任何操作，那么是 `K`。如果有 Git 未知的文件需要添加或忽略，那么是 `!`。如果需要提交，那么是 `C`。如果没有上游，那么是 `U`。如果存在上游，但我没有推送，那么是 `P`。该方案不是基于当前状态，而是描述了我要做的*下一个动作*。（要回顾 Git 术语，请阅读[本文](https://opensource.com/article/19/2/git-terminology)。）


终端功能是通过一个有趣的 Python 程序完成的。它运行 `python -m howsit`（在我把 [howsit](https://pypi.org/project/howsit/) 安装在虚拟环境中之后）。


你可以在上图中看到渲染效果，但是为了完整起见，这是我的 `PS1`：



```
[$(~/.virtualenvs/howsit/bin/python -m howsit)]\h:\W \u ℵ₀  
$

```



---


via: <https://opensource.com/article/20/7/tmux-git>


作者：[Moshe Zadka](https://opensource.com/users/moshez) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


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


OK
I use GNOME Terminal, mostly because it is my distribution's default. But what happens inside my terminal is far from "default." Before I get into how I customize it, here is what it looks like:

![Moshe Zadka's terminal Moshe Zadka's terminal](https://opensource.com/sites/default/files/uploads/terminal-tmux-moshezadka.png)

(Moshe Zadka, CC BY-SA 4.0)

## Start at the bottom

I use [tmux](https://opensource.com/article/20/1/tmux-console), a terminal multiplexer technology, to manage my terminal experience.

At the bottom of the image above, you can see my green tmux bar. The `[3]`

at the bottom indicates this terminal is the third one: each terminal runs its own tmux session. (I created a new one to make the font larger, so it's easier to see in this screenshot; this is the only difference between this terminal and my real ones.)

The prompt also looks funny, right? With so much information jammed into the prompt, I like to stick in a newline so that if I want to do impromptu shell programming or write a five-step pipeline, I can do it without having things spill over. The trade-off is that simple sequences of commands—touch this, copy that, move this—scroll off my screen faster.


The last thing on the line with the content is [Aleph null](https://simple.wikipedia.org/wiki/Aleph_null#:~:text=Aleph%20null%20(also%20Aleph%20naught,series%20of%20infinite%20cardinal%20numbers.), the smallest [infinite cardinality](https://gizmodo.com/a-brief-introduction-to-infinity-5809689). I like it when it is obvious where a content line ends, and when I realized that both Aleph and subscript 0 are Unicode characters, I could not resist the temptation to make Aleph null part of my prompt. (Math nerds, unite!)

Before that is my username; this is moderately useful since I use the same [dotfiles](https://opensource.com/article/19/3/move-your-dotfiles-version-control) (stored in Git) on multiple machines with different usernames.

Before my username is the last component of the directory I am in. The full path is often too long and useless, but the current directory is invaluable for someone, like me, who constantly forgets what he's working on. Before that is the name of the machine. All my machines are named after TV shows that I like. My older laptop is `mcgyver`

.

The first bit in the prompt is the bit I like the most: one letter that lets me know the Git status of the directory. It is `G`

if the directory is "(not in) Git," `K`

if the directory is "OK" and nothing needs to be done, `!`

if there are files unknown to Git that must be added or ignored, `C`

if I need to commit, `U`

if there is no upstream, and `P`

if an upstream exists, but I have not pushed. This scheme is not based on the current status but describes the *next action* I need to do. (To review Git terminology, give [this article](https://opensource.com/article/19/2/git-terminology) a read.)

This terminal functionality is accomplished with an interesting Python utility. It runs `python -m howsit`

(after I installed [howsit](https://pypi.org/project/howsit/) in a dedicated virtual environment).

You can see the rendering in the image above, but for completeness, here is my PS1:

```
[$(~/.virtualenvs/howsit/bin/python -m howsit)]\h:\W \u ℵ₀
$
```


## Comments are closed.