【发布时间】:2012-11-19 18:32:37
【问题描述】:
我知道你可以通过编辑 ~/.bashrc 文件中的 PS1 变量来永久编辑 bash 提示符,我的看起来像这样:
PS1="\[\e[0;31m\]<HERP(._.)DERP>\[\e[0;0m\]";
但是你也可以在那里设置一个很小的图像吗?例如,如果我想在“HERP(._.)DERP”之前添加一个小美国国旗图标,我可以这样做吗?
【问题讨论】:
标签: linux bash shell fedora ps1
我知道你可以通过编辑 ~/.bashrc 文件中的 PS1 变量来永久编辑 bash 提示符,我的看起来像这样:
PS1="\[\e[0;31m\]<HERP(._.)DERP>\[\e[0;0m\]";
但是你也可以在那里设置一个很小的图像吗?例如,如果我想在“HERP(._.)DERP”之前添加一个小美国国旗图标,我可以这样做吗?
【问题讨论】:
标签: linux bash shell fedora ps1
实际上,是的,你可以。
在最近的 Bash 版本中,至少有 4 个(我可以在 4.2 和 4.3 中做到), 您可以使用十六进制渲染表情符号。
使用echo -e 标志。
粘贴emoji you looked up 并执行十六进制转储以查看其构成:
plasmarob ~ $ echo -n "??"| hexdump
0000000 f0 9f 87 ba f0 9f 87 b8
0000008
然后在第一行用 \x 转义每个十六进制对:
plasmarob ~ $ echo -e 'See? \xf0\x9f\x87\xba\xf0\x9f\x87\xb8'
See? ??
我实际上将我的修改为:
等离子〜⚡
所以是的,想出一个这样的并尝试将其添加到您的 .bashrc 或 .bash_profile。
编辑:SO 或浏览器渲染的某些内容可能已经改变,因为这篇文章中的标志现在呈现为“美国”字符。 YMMV,但我认为它仍然可以在指定的 bash 版本中工作。
【讨论】:
hexdump 的某些版本可能需要使用 -C 选项运行以尊重字节序。 unix.stackexchange.com/questions/55770/…
现在,如果您有可识别表情符号的字体,则可以添加表情符号。我想当问题最初发布时,这不是一个容易可行的选择
I wrote this blog post about it 几年前。
我不知道美国国旗,但export PS1="\360\237\232\251 > " 在您的提示中获得了一面旗帜。
我还编写了一个 shell 工具,使打印 echo 或 shell prompt 的转义更容易一些。它叫emo
【讨论】:
距离越近,白旗和黑旗越接近:
⚐ → U+2690 ; WHITE FLAG
⚑ → U+2691 ; BLACK FLAG
以上是 Unicode 字符。您还可以寻找带有标志的字体 - 我不知道。
【讨论】:
无法在 bash 提示符中添加图标(位图或矢量图形)。
【讨论】:
对不起,没有。终端不做图形。
有关您可以做什么的完整描述,请参阅 bash(1) 手册页的 PROMPTING 部分:
提示
当以交互方式执行时,bash 在准备好读取命令时显示主要提示 PS1,而在需要更多输入以完成命令时显示辅助提示 PS2。 Bash 允许通过插入一些反斜杠转义的特殊字符来自定义这些提示字符串,这些特殊字符被解码如下:
\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 (uses the value of the PROMPT_DIRTRIM variable) \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命令号和历史号通常是不同的:命令的历史号是它在历史列表中的位置,其中可能包括从历史文件中恢复的命令(见下面的HISTORY),而命令号是位置在当前 shell 会话期间执行的命令序列中。字符串解码后,通过参数扩展、命令替换、算术扩展和引号删除等方式进行扩展,具体取决于 promptvars shell 选项的值(参见下文 SHELL BUILTIN COMMANDS 下 shopt 命令的说明)。
\e、\[ 和 \] 转义序列值得特别注意。有了这些,您可以插入ANSI escape codes 来命令终端更改前景色、背景色、移动光标、擦除部分屏幕,以及做其他花哨的技巧。
也就是说,例如,您的提示如何改变颜色。 \[\e[0;31m\] 将前景色设置为红色,\[\e[0;0m\] 将其重置为默认值。
【讨论】:
【讨论】: