用 Starship 定制你的 shell 提示符
提示符

> 
> 控制你的提示符，让你需要的所有信息触手可及。
> 
> 
> 


![](/data/attachment/album/202202/14/232227pkh7bxi1a9asbfd5.jpg "Cosmic stars in outer space")


没有什么比我忘记在我的 Git 仓库中 `git add` 文件更让我恼火的了。我在本地测试，提交，然后推送，却发现在持续集成阶段失败了。更糟糕的是，我在 `main` 分支而不是特性分支上，并不小心推送到它。最好的情况是，因为分支保护而失败，我需要做一些操作才能把改动推送到一个分支。更糟糕的是，我没有正确配置分支保护，不小心直接推送到了 `main` 分支。


如果这些信息能在提示中直接获得，那不是很好吗？


在提示符中甚至还有更多有用的信息。虽然 Python 虚拟环境的名称在提示符中，但虚拟环境的 Python 版本却不在提示符中。


可以仔细地将 `PS1` 环境变量配置为所有相关的信息。这可能会变得很长，很烦人，而且调试起来并不简单。


这就是 Starship 被设计来解决的问题。


### 安装 Starship


Starship 的初始设置只需要两个步骤：安装并配置你的 shell。安装可以很简单：



```
$ curl -fsSL https://starship.rs/install.sh

```

阅读安装脚本，确保你理解它的作用，然后让它可执行并运行它：



```
$ chmod +x install.sh
$ ./install.sh

```

还有其他的安装方法，在其网站上有介绍。你可以在构建镜像的步骤中设置虚拟机或容器。


### 配置 Starship


下一步是配置你的 shell 来使用它。要一次性尝试，假设 shell 是 `bash` 或 `zsh`，请运行以下命令：



```
$ eval "$(starship init $(basename $SHELL))"

```

你的提示符立即改变：



```
localhost in myproject on  master
>

```

如果你喜欢你所看到的，把 `eval "$(starship init $(basename $SHELL))"` 添加到你的 shell 的 `rc` 文件中，使其永久化。


### 自定义 Starship


默认安装假定你可以安装“电脑迷字体”，例如 [Fantasque Sans Mono](https://github.com/belluzj/fantasque-sans)。 特别是，你需要一种带有来自 Unicode 的“私有实现”部分的字形的字体。


这在控制终端时非常有效，但有时，终端的配置并不容易。例如，当使用一些浏览器内的 shell 抽象时，配置浏览器的字体可能是不太容易的。


该码位的最大用户是 Git 集成，它使用一个特殊的自定义符号来表示“分支”。禁用它可以通过使用文件 `~/.config/starship.toml` 来配置 `starship.rs`。


禁用分支符号是通过配置 `git_branch` 部分的 `format` 变量完成的：



```
[git_branch]
format = "on [$branch]($style) "

```

`starship.rs` 的一个好处是，改变配置会立即生效。保存文件，按下**回车**，看看字体是否符合预期。


还可以配置提示符中不同部分的颜色。例如，如果 Python 部分的亮黄色在白色背景上有点难看，你可以配置为蓝色：



```
[python]
style = "blue bold"

```

许多语言都有配置支持，包括 Go、.NET 和 JavaScript。还支持显示命令的持续时间（只针对耗时超过阈值的命令）等。


### 控制提示符


控制你的提示符，让你需要的所有信息触手可及。安装 Starship，让它为你工作，并享受吧！




---


via: <https://opensource.com/article/22/2/customize-prompt-starship>


作者：[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
Nothing irritates me more than when I forget to `git add`

files in my Git repository. I test locally, commit, and push, only to find out it failed in the continuous integration phase. Even worse is when I'm on the `main`

branch instead of a feature branch and accidentally push to it. The best-case scenario is that it fails because of branch protection, and I need to do some surgery to get the changes to a branch. Even more worse, I did not configure branch protection properly, and I accidentally pushed it directly to `main`

.


Wouldn't it be nice if the information was available right in the prompt?

There is even more information that is useful in the prompt. While the name of Python virtual environments is in the prompt, the Python version the virtual environment has is not.

It is possible to carefully configure the `PS1`

environment variable to all relevant information. This can get long, annoying, and non-trivial to debug.

This is the problem that Starship got designed to solve.

## Install Starship

The initial setup for Starship only requires two steps: Installing and configuring your shell to use it. Installation can be as simple as:

`$ curl -fsSL https://starship.rs/install.sh`


Read over the install script to make sure you understand what it does, and then make it executable and run it:

```
$ chmod +x install.sh
$ ./install.sh
```


There are other ways to install, covered on the website. You can develop virtual machines or containers at the image-building step.

## Configuring Starship

The next step is to configure your shell to use it. To try it as a one-off, assuming the shell is `bash`

or `zsh`

, run the following:

`$ eval "$(starship init $(basename $SHELL))"`


Your prompt changes immediately:

```
localhost in myproject on master
>
```


If you like what you see, add `eval "$(starship init $(basename $SHELL))"`

to your shell's `rc`

file to make it permanent.

## Customizing Starship

The default installation assumes that you can install a "Nerd font," such as [Fantasque Sans Mono](https://github.com/belluzj/fantasque-sans). You want, particularly, a font with glyphs from Unicode's "private implementation" section.

This works great when controlling the terminal, but sometimes, the terminal is not easy to configure. For example, when using some in-browser shell abstraction, configuring the browser font can be non-trivial.

The biggest user of the code points is the Git integration, which uses a special custom symbol for "branch." Disabling it can be done by configuring `starship.rs`

using the file `~/.config/starship.toml`

.

Disabling the branch symbol is done by configuring the `git_branch`

section's `format`

variable:

```
[git_branch]
format = "on [$branch]($style) "
```


One of the nice things about `starship.rs`

is that changing the configuration has an immediate effect. Save the file, press **Enter**, and see if the font looks as intended.

It's also possible to configure the color of different sections in the prompt. For example, if the Python section's bright yellow is a bit harder to see on a white background, you can configure blue:

```
[python]
style = "blue bold"
```


There is configuration support for many languages, including Go, .NET, and JavaScript. There is also support for showing command duration (only for commands which take longer than a threshold) and more.

## Take the con

Take control of your prompt, and have all the information you need at your fingertips. Install Starship, make it work for you, and enjoy!

## 1 Comment