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 terminal display configuration by ubuntu

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly shows you "ubuntu how to customize the terminal display configuration", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "ubuntu how to customize the terminal display configuration" this article.

Bashrc configuration

Every time we log in to a shell terminal, there is some fixed display message in front of it:

Root@ubuntu:#

These display information can actually be configured.

We open the .bashrc file in the user's home directory and find something similar to the following about PS1:

If ["$color_prompt" = yes]; then PS1='$ {debian_chroot:+ ($debian_chroot)}\ [\ 033 [01Trac32m\]\ u@\ h\ [\ 033 [00m\]:\ [033 [01trans34m\]\ W\ [\ 033 [00m\]\ $'else PS1='$ {debian_chroot:+ ($debian_chroot)}\ u@\ h:\ W\ $' fi

This is used to configure your terminal display.

For example:

$echo-e "\ 033 [31m red color\ 033 [0m" red color

And what do the other symbols mean?

\ d: represents date,\ H: full host name\ h: only take the first name of the host\ t: display time is in 24-hour format, such as: HH:MM:SS\ T: display time is 12-hour format\ A: display time is 24-hour format: HH:MM\ u: user name\ v: version information of BASH\ w: full working directory name\ W: list the last directory\ $: prompt character If it is root, the prompt is: #, and the average user is: $

So to put it bluntly, it is to configure the information that your terminal wants according to your own needs.

At this time, let's take a look at one of the previous simple configurations:

'${debian_chroot:+ ($debian_chroot)}\ u@\ h:\ W\ $'

We can ignore $debian_chroot here because it is the value of a variable and we can completely customize it.

You can see that there are:

\ u user name\ h hostname\ W Last directory name\ $prompt character

The relevant contents obtained through the command are as follows:

$whoamiroot$ hostnameubuntu$ basename `pwd` official account programming Zhuji

So what my terminal finally shows is:

Root@ubuntu: official account programming Zhuji #

Generally speaking, the default configuration is fine. What you often need to change is that it shows the full path by default. Here, you just need to replace\ w with\ W.

Then execute the command:

Source / .bashrc

Just let the configuration take effect.

Let's sort out the process again:

Modify PS1 display items as needed

Execute source .bashrc to make it effective

Note here that the configuration files are loaded sequentially during Linux startup

The bashrc under a user's home directory only affects the current user. If you want the system-wide modification to take effect, you can modify / etc/bash.bashrc (system differences may be different).

How to display the current git branch

In fact, you should have basically understood by now that it is nothing more than configuring the information of the current branch of git in PS1.

How to get the current git branch

We all know:

$git branch master* testBranch

You can view the branch information, where * indicates the current branch, so we can get it through character processing.

Or directly through the command:

$git rev-parse-abbrev-ref HEADtestBranch

Get it.

Configure branch information in PS1

Considering that there may be no git project in some directories, you need to determine whether it is empty or not, and pay attention to dealing with standard errors. So write the part that gets the git branch information as a shell function:

Git_branch () {branch= `git rev-parse-- abbrev-ref HEAD 2 > / dev/ null`if ["${branch}"! = ""] then if ["${branch}" = "(no branch)"] then branch= "(`git rev-parse-- short head.)" Fi echo "($branch)" fi}

Add it to the end of the .bashrc file, and then modify the contents of PS1:

PS1='$ {debian_chroot:+ ($debian_chroot)}\ u@\ h:\ W$ (git_branch)\ $'PS1='$ {debian_chroot:+ ($debian_chroot)}\ [\ 033 [01 32m\]\ u@\ h\ [\ 033 [00m\]:\ [\ 033 [01tram34m\]\ W$ (git_branch)\ [\ 033 [00m\]\ $'

Save after modification, execute:

$source ~ / .bashrc

You can customize the current colors according to your own needs. Let's take a look at the previous effect, do you think it's not so difficult to configure?

The above is all the contents of the article "how to customize the terminal display configuration of ubuntu". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Servers

Wechat

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

12
Report