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


70

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

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

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

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

无论如何,我是否将mac终端的行为更改为类似于linux终端?


1
终端应用程序的首选项中必须有一个屏幕。您可以更改行为。

1
Linux终端无法神奇地显示出完整的路径。这取决于您的设置。有些linux发行版将事物配置为默认显示完整路径,有些则没有。这完全取决于$ PS1变量。(请参阅阿斯姆斯(答案)。)
frabjous 2011年

Answers:


108

要让bash在提示时返回“ user @ hostname:path / to / directory $”,请在您的〜/ .bash_profile中添加以下行:

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

要么

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

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

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

source ~/.bash_profile

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

提示

  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

1
或者只需PS1在每个打开的窗口中输入该任务。顺便说一句,这export是不必要的。
丹尼斯·威廉姆森

6
实际上,如果不使用export,则将定义一个shell变量,而不是环境变量,因此PS1不会全局传递给所有进程。
Asmus

1
export com按照建议的方式工作并显示完整路径,但是一旦我重新启动终端,它只会显示当前目录(而不是完整路径)。我该如何永远保持下去。
JiteshW,2015年

1
这将是可能的解释是什么PS1\u@\H:\w$意味着什么?
杰斯

3
@Jas我已经相应更新了答案,不知道为什么我几年前没有这样做^^
Asmus,2016年


8

我使它看起来与Mac上的centOS终端非常相似。在终端上打开bash_profile

nano ~/.bash_profile

添加以下内容

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

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

username@host.local [/Applications/MAMP/htdocs]$ 


1

我只是将/ w更改为/ W以仅创建当前文件夹

在彩色终端

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '

无彩端子

    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$ '

我使用ubuntu 16.04并在〜/ .bashrc中更改文件


0

我用了这个命令。这个对我有用。

第一,

vi ~/.bash_profile

然后,在新行中添加这些单词。

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

最后,

`source ~/.bash_profile`
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.