如何讓完整目錄路徑始終顯示在 mac 終端機(如 Linux 終端機)中

如何讓完整目錄路徑始終顯示在 mac 終端機(如 Linux 終端機)中

在我的 ubuntu 終端機中,我總是完整顯示目前目錄。像這樣:

blabla@blublu:~/music/my_album/classical/beethoven$

但在我的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$ '.

這只是另一種方式。

在此輸入影像描述

相關內容