在bash terminal中,使用上箭头来调用之前的命令时,如果命令较长,偶尔会出现命令混杂的情况。实际上这是由于terminal的提示文字使用了颜色导致的。

Prompt: 终端最前方显示的 用户名:Host $

修改PS1的内容

查看当前PS1的设置:

echo $PS1

修改为提示单独显示一行的样式:

export PS1="\n\[\[\033[01;33m\][\w]\[\033[00m\]\n\[\033[0;90m\]\$ "
# 另一种样式
export PS1="\n\[\[\033[01;34m\][\u@\h:\w]\[\033[00m\]\n\[\033[0;90m\]\$ "

将上述命令写入~/.bashrc即可。上述样式仅保留了当前路径\w,可以根据下文的设置参数来调整自己喜欢的样式。

颜色对应的code:

# 中间的01代表加粗
BYELLOW='\[\033[01;33m\]'
IBLACK='\[\033[0;90m\]'
PS_CLEAR='\[\033[0m\]'

Black: 30
Blue: 34
Cyan: 36
Green: 32
Purple: 35
Red: 31
White: 37
Yellow: 33

或者干脆去掉颜色:export PS1="\n\u@\h:\w\$ "

PS

  • PS1 – The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is \s-\v\$ .
  • PS2 – The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is >
  • PS3 – The value of this parameter is used as the prompt for the select command
  • PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +

可以允许的设置参数

  • \a : an ASCII bell character (07)
  • \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
  • \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
  • \e : an ASCII escape character (033)
  • \h : the hostname up to the first ‘.’
  • \H : the hostname
  • \j : the number of jobs currently managed by the shell
  • \l : the basename of the shell’s terminal device name
  • \n : newline
  • \r : carriage return
  • \s : the name of the shell, the basename of $0 (the portion following the final slash)
  • \t : the current time in 24-hour HH:MM:SS format
  • \T : the current time in 12-hour HH:MM:SS format
  • \@ : the current time in 12-hour am/pm format
  • \A : the current time in 24-hour HH:MM format
  • \u : the username of the current user
  • \v : the version of bash (e.g., 2.00)
  • \V : the release of bash, version + patch level (e.g., 2.00.0)
  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
  • ! : the history number of this command
  • # : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \nnn : the character corresponding to the octal number nnn
  • \ : a backslash
  • [ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • ] : end a sequence of non-printing characters

修改Title

修改标签页的title,将以下函数加入~/.bashrc:

function tabtitle() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$*\a\]"
  PS1=${ORIG}${TITLE}
}

在terminal里执行tabtitle title you want to set即可修改标签页的标题。

Reference

https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

https://apple.stackexchange.com/questions/34910/how-to-hide-computer-name-and-user-name-in-terminal-command-prompt

https://superuser.com/questions/382456/why-does-this-bash-prompt-sometimes-keep-part-of-previous-commands-when-scrollin

https://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/

https://www.howtogeek.com/307701/how-to-customize-and-colorize-your-bash-prompt/


文章版权归 FindHao 所有丨本站默认采用CC-BY-NC-SA 4.0协议进行授权|
转载必须包含本声明,并以超链接形式注明作者 FindHao 和本文原始地址:
https://findhao.net/easycoding/2592.html

Comments