2012-12-01 48 views
4

我想要自定義this oh-my-zsh主題。什麼是ZSH中的%〜(百分比波數)?

我在其中找到了這段代碼,它顯然打印了目錄名(如果我錯了,請糾正我)。

# Dir: current working directory 
prompt_dir() { 
    prompt_segment blue black '%~' 
} 

和prompt_segment被定義爲

# Begin a segment 
# Takes two arguments, background and foreground. Both can be omitted, 
# rendering default background/foreground. 
prompt_segment() { 
    local bg fg 
    [[ -n $1 ]] && bg="%K{$1}" || bg="%k" 
    [[ -n $2 ]] && fg="%F{$2}" || fg="%f" 
    if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then 
    echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} " 
    else 
    echo -n "%{$bg%}%{$fg%} " 
    fi 
    CURRENT_BG=$1 
    [[ -n $3 ]] && echo -n $3 
} 

這樣做的輸出並不總是公正的目錄路徑。如果我在ENV變量中存在的路徑中,它將用該變量替換路徑。

如果我在

/Users/abc/.oh-my-zsh/custom 

而且$ ZSH_CUSTOM是

/Users/abc/.oh-my-zsh/custom 

,我會在命令提示符下$ZSH_CUSTOM

所以我的問題是,1)什麼是%~正在從prompt_dir發送,2)是這片編碼正從當前工作目錄,以及3)我怎樣才能使它總是輸出的真實路徑。

+0

謝謝你問這個。我還試圖編輯agnoster主題,因爲目錄提示符變得很大。將'%〜'更改爲'%2d'完美無缺。 –

回答

8

man zshmiscEXPANSION OF PROMPT SEQUENCES部分:

%d 
/ Current working directory. If an integer follows the `%', it 
      specifies a number of trailing components of the current working 
      directory to show; zero means the whole path. A negative inte‐ 
      ger specifies leading components, i.e. %-1d specifies the first 
      component. 

    %~  As %d and %/, but if the current working directory has a named 
      directory as its prefix, that part is replaced by a `~' followed 
      by the name of the directory. If it starts with $HOME, that 
      part is replaced by a `~'. 
+0

優秀:)。還有一個願望 - 我可以用'〜'替換我的主目錄嗎?使用'%d'?我想有完整的路徑,減去'$ HOME'被'〜'替代。 – user1527166

+1

@ user1527166這不正是'%〜'做的嗎?大多數情況下,除非您經常通過命名目錄散列或特殊功能(不記得它的名字)來使用它們,否則你不會點擊「命名目錄作爲其前綴」的情況。 – ZyX

相關問題