如何更改我的tcsh提示以显示我当前的工作目录?

paw*_*yan 4 csh tcsh

我正在使用tcsh,我正在寻找一种方式响应工作目录,我至少想显示最后一个文件夹名称,而不是获取完整路径.

想象一下我当前的工作目录是:

  [user@hostname/home/us/Desktop/my/projects]
Run Code Online (Sandbox Code Playgroud)

然后我想显示如下提示:

    [user@hostname projects] 
Run Code Online (Sandbox Code Playgroud)

目前我正在使用.cshrc我写的文件中的文件:

  alias setprompt 'set prompt="[suman@`hostname` `pwd`:~] $"'
  alias setprompt 'set prompt="${LightGreen}[${yellow}suman${Light}${LightPurple}@%m ${LightGreen}:~] ${yellow}$ ${end} "'
Run Code Online (Sandbox Code Playgroud)

我也没有PS1变量,也不知道它的作用,我想知道.cshrc文件和.bashrc文件之间的区别.

Mar*_*oij 16

您可以使用:

set prompt = '[%n@%m %c]$ '
Run Code Online (Sandbox Code Playgroud)

%n用户名,%m主机名,第一个.%c最后一个目录部分.有大量这样的替换可用,你可以找到它们的列表,tcsh(1)为方便起见,下面重复.

使用`pwd`不起作用,因为cshrc文件在shelll启动时只读取一次,而不是在每次目录更改时读取.

我想知道.cshrc文件和.bashrc文件之间的区别.

cshrc文件由csh和使用tcsh,bashrc文件由bash.使用.虽然它们服务于同一目的,但它们是不同的程序(如~/.mozilla/firefox~/.config/chromium).


           %/  The current working directory.
           %~  The current working directory, but with one's  home  direc?
               tory  represented  by `~' and other users' home directories
               represented  by  `~user'  as  per  Filename   substitution.
               `~user'  substitution happens only if the shell has already
               used `~user' in a pathname in the current session.
           %c[[0]n], %.[[0]n]
               The trailing component of the current working directory, or
               n  trailing  components if a digit n is given.  If n begins
               with `0', the number  of  skipped  components  precede  the
               trailing  component(s)  in the format `/<skipped>trailing'.
               If the ellipsis shell variable is set,  skipped  components
               are  represented  by  an  ellipsis  so  the  whole  becomes
               `...trailing'.  `~' substitution is done as in `%~'  above,
               but  the  `~'  component  is ignored when counting trailing
               components.
           %C  Like %c, but without `~' substitution.
           %h, %!, !
               The current history event number.
           %M  The full hostname.
           %m  The hostname up to the first `.'.
           %S (%s)
               Start (stop) standout mode.
           %B (%b)
               Start (stop) boldfacing mode.
           %U (%u)
               Start (stop) underline mode.
           %t, %@
               The time of day in 12-hour AM/PM format.
           %T  Like `%t', but in 24-hour format (but see  the  ampm  shell
               variable).
           %p  The  `precise'  time  of  day in 12-hour AM/PM format, with
               seconds.
           %P  Like `%p', but in 24-hour format (but see  the  ampm  shell
               variable).
           \c  c is parsed as in bindkey.
           ^c  c is parsed as in bindkey.
           %%  A single `%'.
           %n  The user name.
           %N  The effective user name.
           %j  The number of jobs.
           %d  The weekday in `Day' format.
           %D  The day in `dd' format.
           %w  The month in `Mon' format.
           %W  The month in `mm' format.
           %y  The year in `yy' format.
           %Y  The year in `yyyy' format.
           %l  The shell's tty.
           %L  Clears  from the end of the prompt to end of the display or
               the end of the line.
           %$  Expands the shell or environment variable name  immediately
               after the `$'.
           %#  `>'  (or the first character of the promptchars shell vari?
               able) for normal users, `#' (or  the  second  character  of
               promptchars) for the superuser.
           %{string%}
               Includes string as a literal escape sequence.  It should be
               used only to change terminal attributes and should not move
               the  cursor  location.  This cannot be the last sequence in
               prompt.
           %?  The return code of the command  executed  just  before  the
               prompt.
           %R  In prompt2, the status of the parser.  In prompt3, the cor?
               rected string.  In history, the history string.

           `%B', `%S', `%U' and `%{string%}' are available in only  eight-
           bit-clean shells; see the version shell variable.

           The  bold,  standout  and underline sequences are often used to
           distinguish a superuser shell.  For example,

               > set prompt = "%m [%h] %B[%@]%b [%/] you rang? "
               tut [37] [2:54pm] [/usr/accts/sys] you rang? _

           If `%t', `%@', `%T', `%p', or `%P' is used, and noding  is  not
           set,  then print `DING!' on the change of hour (i.e, `:00' min?
           utes) instead of the actual time.

           Set by default to `%# ' in interactive shells.
Run Code Online (Sandbox Code Playgroud)


mat*_*atz -1

编辑:我以为问题是关于 bash 的,因为它是这样标记的。

.cshrc是 csh 或 tcsh 的配置文件,而.bashrc是 bash 的配置文件。即使 bash 和 csh 的用途相似,但它们只是不同的程序,因此其中一个的配置不一定适用于另一个。

所以你需要做的就是将这样的行放入你的 .bashrc 中

PS1='...\w$ '
Run Code Online (Sandbox Code Playgroud)

其中\w是将扩展到当前工作目录的神奇代码,您可以在 bash 手册页的“提示”部分中查找。

一个更完整的示例取自 Cygwin 的 bash 包的默认设置,如下所示:

PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
Run Code Online (Sandbox Code Playgroud)

它将扩展为这样的提示(但带有颜色,uuuh):

user@machine currentdir $ 
Run Code Online (Sandbox Code Playgroud)