Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the shell prompt by Starship to realize freedom of information

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian for you to introduce in detail "Starship how to customize the shell prompt to achieve freedom of information", the content is detailed, the steps are clear, the details are handled properly, I hope this "how to customize the shell prompt to achieve freedom of information" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Control your prompt so that all the information you need is within reach.

Nothing annoys me more than forgetting the git add file in my Git repository. I tested, committed, and then pushed locally, only to find that I failed in the continuous integration phase. To make matters worse, I accidentally pushed to it on the main branch instead of the feature branch. At best, it fails because of branch protection, and I need to do something to push the changes to a branch. To make matters worse, I did not configure branch protection correctly and accidentally pushed directly to the main branch.

Wouldn't it be nice if this information could be obtained directly in the prompt?

There is even more useful information in the prompt. Although the name of the Python virtual environment is at the prompt, the Python version of the virtual environment is not.

You can carefully configure the PS1 environment variable to all relevant information. This can be long, annoying, and not easy to debug.

This is the problem that Starship was designed to solve.

Install Starship

The initial setup of Starship requires only two steps: install and configure your shell. Installation can be simple:

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

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

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

There are other installation methods, which are described on its website. You can set up a virtual machine or container in the step of building an image.

Configure Starship

The next step is to configure your shell to use it. To try once, assuming that shell is bash or zsh, run the following command:

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 rc file to make it permanent.

Custom Starship

The default installation assumes that you can install "geek fonts", such as Fantasque Sans Mono. In particular, you need a font with glyphs from the "private implementation" section of Unicode.

This is very effective in controlling the terminal, but sometimes the configuration of the terminal is not easy. For example, when using shell abstractions within some browsers, it may not be easy to configure fonts for browsers.

The biggest user of this code point is Git integration, which uses a special custom symbol to represent "branch". Disable it to configure starship.rs by using the file ~ / .config/starship.toml.

Disabling branch symbols is done by configuring the format variable in the git_branch section:

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

One of the benefits of starship.rs is that configuration changes take effect immediately. Save the file and press enter to see if the font meets your expectations.

You can also configure the colors of different parts of the prompt. For example, if the bright yellow of the Python section is a little ugly on a white background, you can configure it to blue:

[python] style = "blue bold"

Configuration support is available in many languages, including Go, .NET, and JavaScript. It also supports displaying the duration of the command (only for commands that take longer than the threshold), and so on.

Read this, the "Starship how to customize the shell prompt to achieve freedom of information" article has been introduced, want to master the knowledge of this article still need to do your own practice to understand, if you want to know more related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report