【问题标题】:How can I permanently change the color of terminal's shell prompt and create new empty line after a command?如何永久更改终端 shell 提示的颜色并在命令后创建新的空行?
【发布时间】:2020-01-29 01:23:06
【问题描述】:

在我尝试进行更改之前,我有PS1="\n$PS1"。我目前在.bash_profile 中的别名后面有PS1="\e[0;34m[\u@ \W]\$ \e[m",但这并没有给我想要的行为。当我正在编写命令并且它在我完成之前到达行尾时,终端不会移动到下一行。相反,它会回到我正在处理的行的开头,并在我继续输入时在视觉上覆盖 bash 提示和之后的任何内容。当终端到达上一行的末尾以及在每个命令之前/之后创建一个空行时,如何让终端移动到新行?

我试图将 bash 提示颜色永久更改为蓝色,并在运行命令后创建一个新的空行以使终端更易于阅读。

谢谢

刚刚意识到我正在查看一个在 Linux 上更改 shell 提示颜色的指南,而它本应适用于 Mac。

【问题讨论】:

  • 改用Ask DifferentSuper User。这个网站是针对编程相关的问题,而不是一般的计算机或操作系统支持。

标签: bash terminal


【解决方案1】:

请看一下this 这是我的命令提示符,带有颜色和 git 支持

#!/bin/bash

case $1 in '--remove' | '-r') sed '/[oOº]_[oOº]/d' -i ~/.bashrc; exit 0;; esac

cat >> ~/.bashrc << 'EOF'
#---------------------------------------------{ Info bar }----------------------------------------------+ o_o
line_simbol='-'                                                                                       # | O_O
faces=(O_o o_O o_o O_O º_o º_O O_º o_º º_º); facesN=${#faces[@]} # Face types                         # | o_º
declare -A paint=( # Colors     |   Effects                                                           # | O_O
                [green]='\e[32m' [norm]='\e[0m'                                                       # | O_o
                [yelow]='\e[33m' [bold]='\e[1m'                                                       # | O_o
                 [blue]='\e[94m'  [dim]='\e[2m')                                                      # | o_o
#-------------------------------------------------------------------------------------------------------+ º_O
face() { printf "${paint[yelow]}${faces[$((RANDOM % $facesN))]}${paint[norm]}"; } # Random face       # | º_º
info() {                                                                                              # | o_º
    [[ -d .git ]] && {  # If in git project folder add git status to info bar output                  # | O_o
        git_clr=('GIT' $(git -c color.ui=always status -sb)) # Colored output 4 info                  # | o_O
        git_tst=('GIT' $(git                    status -sb)) # Simple  output 4 test                  # | o_o
    }                                                                                                 # | o_o
                                                                                                      # | o_O
    nametest="{ $HOSTNAME }"                                                                          # | O_o
    sign="{ ${paint[norm]}$HOSTNAME${paint[green]} }"                                                 # | o_o
    printf -v line  "%${COLUMNS}s"; line=${line// /$line_simbol}                                      # | O_o
    printf -v line2 "%$[($COLUMNS-${#nametest})/2]s"; line2=${line2// /$line_simbol}                  # | O_O
    signtest="$line2{ $HOSTNAME }$line2"; [[ ${#signtest} -lt $COLUMNS ]] && sign+=$line_simbol       # | o_o
                                                                                                      # | o_o
    D=$(printf "%(%a %d %b %T)T")                             # Date & time                           # | O_o
    T=" O_o $PWD  ${git_tst[*]} $D o_O "                      # Test string                           # | o_o
    S=$[$COLUMNS-${#T}]; [[ $S -lt 0 ]] && S=0                # Count spaces                          # | o_º
    date="${paint[dim]}$D${paint[norm]}"                      # Date & time                           # | O_o
    line="${paint[green]}$line${paint[norm]}\n"               # Create line                           # | O_o
    home="${paint[bold]}${paint[blue]}$PWD${paint[norm]}"     # Home dir info                         # | o_o
    sign="${paint[green]}$line2$sign$line2${paint[norm]}\n"   # Hostname sign                         # | O_o
           #------+-----+-------+------+------------+-----+-------+--------+                          # | o_o
           # Line | O_o |homedir|Spaces| Git status | Date|  o_O  |  Line  |                          # | o_O
           #------+-----+-------+------+------------+-----+-------+--------+                          # | O_O
    printf "$sign $(face) $home %${S}s ${git_clr[*]} $date $(face) \n$line" # Final info string       # | O_o
}                                                                                                     # | O_o
#-------------------------------------------------------------------------------------------------------+ o_o
PS1='${debian_chroot:+($debian_chroot)}\n$(info)\n$ '                                                 # | º_O
                                                                                                      # | o_º
case "$TERM" in xterm*|rxvt*)                                                                         # | O_o
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)} ${faces[$[RANDOM % $facesN]]} \w\a\]$PS1";;        # | O_º
esac                                                                                                  # | o_O
#-------------------------------------------------------------------------------------------------------+ O_o
EOF

【讨论】:

  • 感谢您的回复!我对终端很陌生。我应该将所有内容复制/粘贴到我的 bashrc 文件中吗?
  • @tableguarder 整个脚本是一个安装程序,它会将所有内容添加到您的~/.bashrc 中,如果您使用--remove 选项运行它,则将其删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2014-08-12
  • 2023-03-18
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
相关资源
最近更新 更多