2015-10-09 106 views
2

我正在使用tcsh,我正在尋找一種方式響應式工作目錄,我至少想要顯示最後一個文件夾名稱而不是獲取完整路徑。如何更改我的tcsh提示以顯示我當前的工作目錄?

想象一下,我當前的工作目錄是:

[[email protected]/home/us/Desktop/my/projects] 

然後我想顯示這樣的提示:

[[email protected] projects] 

目前我使用的是.cshrc文件中的文件我已經寫了這樣的:

alias setprompt 'set prompt="[[email protected]`hostname` `pwd`:~] $"' 
    alias setprompt 'set prompt="${LightGreen}[${yellow}suman${Light}${LightPurple}@%m ${LightGreen}:~] ${yellow}$ ${end} "' 

我沒有PS1變量也不知道它做什麼,我想知道.cshrc文件和.bashrc文件之間的區別。

+0

我發現,當我在那有許多深層次的目錄正在工作,名字很長,在屏幕的RHS的提示是沒有幫助的。就我個人而言,我拒絕將工作目錄放入我的提示中。但通過一切手段去尋找關於這個話題的其他問題。 –

+0

可能重複[Bash:自定義PS1與漂亮的工作目錄路徑](http://stackoverflow.com/questions/5687446/bash-custom-ps1-with-nice-working-directory-path) –

+0

'.cshrc'文件由C shell使用; '.bashrc'文件被Bash使用。你可以看看,在許多其他問題中,[Bash:自定義PS1具有良好的工作目錄路徑](https://stackoverflow.com/questions/5687446/bash-custom-ps1-with-nice-working-directory-path) 。 –

回答

-1

編輯:我以爲問題是關於bash,因爲它 被標記爲那樣。

.cshrc是csh或tcsh的配置文件,wheras .bashrc是bash的配置文件。即使它們有類似的用途,bash和csh也只是不同的程序,所以配置一個不一定適用於另一個。

所以,你需要做的是把這樣一行到你的.bashrc

PS1='...\w$ ' 

其中\w是一個神奇的代碼將被擴展爲當前的工作目錄,你可以查找bash的手冊頁在「提示」部分。

一個更完整的例子,從Cygwin的bash包的默認設置拍攝,可以是這樣的:

PS1='\[\e]0;\w\a\]\n\[\e[32m\]\[email protected]\h \[\e[33m\]\w\[\e[0m\]\n\$ ' 

這將擴大到像這樣的提示(但顏色,uuuh):

[email protected] currentdir $ 
+0

'並且'\ W'對應於最後一步在CWD。 – choroba

+0

對不起,我只有.cshrc文件,我cretaed一個.bashrc文件,我做了你所說的,即使當我來源.bashrc文件它給出了以下錯誤PS1:命令未找到 – pawankalyan

+0

但你確定你正在運行bash,而不是csh之類的?如果有疑問,運行'ps -p $$'並檢查Command列是否實際引用了bash。 – matz

6

您可以使用:

set prompt = '[%[email protected]%m %c]$ ' 

%n輸入用戶名,%m,主機名爲最後一個目錄部分的第一個.%c。有大量這樣的替換可用,你可以在tcsh(1)找到它們的列表,爲方便起見,下面重複。

使用`pwd`將無法​​正常工作,因爲cshrc文件上shelll啓動只讀一次,而不是在每一個目錄的變化。

我想知道.cshrc文件和.bashrc文件之間的區別。

cshrc文件由cshtcsh使用的,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. 
+0

沒錯,''.chsrc''中的'set prompt'中'pwd'不起作用,但是在打印提示符前每次都可以使用'precmd'來設置提示符。 (例如,請參閱https://stackoverflow.com/questions/9973266/customize-csh-prompt-for-git-repo/13090820#13090820) – Randall

相關問題