如何让完整的目录路径始终显示在 Mac 终端(如 Linux 终端)中

如何让完整的目录路径始终显示在 Mac 终端(如 Linux 终端)中

在我的 ubuntu 终端中,我始终会完整显示当前目录。就像这样:

blabla@blublu:~/音乐/我的专辑/古典/贝多芬$

但在我的 Mac(Mac OS X 10.6.5)终端上没有显示完整路径,它是这样的:

blabas-MacBook-Pro:古典贝多芬$

有没有什么办法可以改变 mac 终端的行为,使其像 linux 终端一样?

答案1

为了让 bash 返回“user@hostname:path/to/directory$”作为提示符,请将以下行添加到您的 ~/.bash_profile 中:

export PS1='\u@\H:\w$'

或者

export PS1='\u@\H:\w$ '

如果你喜欢在 $ 和命令之间留一个空格

为了使更改立即生效,请在每个打开的窗口中运行以下命令(或重新启动终端):

source ~/.bash_profile

编辑:可以在 bash 手册页的“PROMPTING”段落中找到可用字符串的列表(man bash):

提示

  When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command.  Bash allows these prompt strings  to  be  customized  by
   inserting a number of backslash-escaped special characters that are decoded as follows:
          \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

答案2

我把它做得跟我 Mac 上的 centOS 终端很相似。在终端上打开 bash_profile

nano ~/.bash_profile

添加以下内容

# Show always fullpath on terminal
export PS1='\u@\H [\w]$ '

重新启动终端,然后它将看起来像这样

[email protected] [/Applications/MAMP/htdocs]$ 

答案3

我不确定 Mac 是否支持,但在 Ubuntu 上改变了 Gnome 终端提示

PS1="\a\n\n\e[31;1m\u@\h on \d at \@\n\e[33;1m\w\e[0m\n$ "

答案4

如果您不想编辑,~/.bash_profile您也可以只配置您的终端配置文件。单击Terminal > Prefences并从那里转到Profiles选项卡,然后转到 选项Shell卡。启用[x] Run command并输入export PS1='\u@\H:\w$ '

这只是另一种做法。

在此处输入图片描述

相关内容