如何在 Ubuntu 终端中仅显示基本目录?

如何在 Ubuntu 终端中仅显示基本目录?

在 Ubuntu 10.04 终端中,当我遍历某个较长的目录结构时,会显示整个路径,这会导致终端上出现大量空间浪费,光标会移到右侧。这让人很恼火。有什么方法可以只显示基本目录吗?

现在 - ops@ops:/media/Main320/final_code/code/office/c/practice/parsers/proj1$

需要 - ops@ops:~/proj1$

我确信这是某些配置更改或某些配置文件首选项更改。任何一个...

答案1

更改 Bash 提示符很容易。只需为 PS1 分配一个新值:

PS1="myprompt : "

现在新的提示将如下所示

我的提示符:

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
* \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

作为示例,让我们创建一个显示今天的日期和主机名的提示字符串:

PS1="\d \h $ "

输出就像

9月4日星期日 ubuntu $

当你对提示字符串感到满意时,你可以将其设置为默认提示,即使在重新启动后也可以通过在 .bashrc 中设置 PS1 变量

答案2

正如@nkr1pt所说,你想设置PS1

PS1='\u@\h \W \$'

还要检查设置环境变量PROMPT_COMMAND,这是每次 bash 打印提示时运行的命令。我的设置为:

PROMPT_COMMAND='echo -ne "\e]0;$USER@${HOSTNAME%%.*}: $(pwd -P)\a"'

显然,USER 和 HOSTNAME 已在其他地方设置。这会将完整路径放入您的术语标题中。

答案3

Bash 4 有一个名为PROMPT_DIRTRIM

user@host:~$ echo $PS1
\u@\h:\w\$
user@host:~$ cd /usr/share/doc/bash/examples
dennis@emperor:/usr/share/doc/bash/examples$ PROMPT_DIRTRIM=2
dennis@emperor:.../bash/examples$

设置要显示的目录元素的最小数量。

答案4

您可以使用符号链接

ln -s /media/Main320/final_code/code/office/c/practice/parsers/proj1 ~/proj1
cd ~/proj1

相关内容