Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is %~ (percent tilde) in ZSH?

Tags:

shell

macos

zsh

I'm trying to customize this oh-my-zsh theme.

I found this piece of code in it, which apparently prints the dir name (correct me if I'm wrong).

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

and prompt_segment is defined as

# 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
}

The output of this isn't always just the directory path. If I'm in a path which is also present in an ENV variable, it replaces the path with that variable.

If I'm in

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

And $ZSH_CUSTOM is

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

I just get $ZSH_CUSTOM in the command prompt.

So my question is, 1) what's the %~ being sent from prompt_dir, 2) where is this piece of coding getting the current working directory from, and 3) how can I make it always output the real path.

like image 267
user1527166 Avatar asked Dec 01 '12 15:12

user1527166


1 Answers

See EXPANSION OF PROMPT SEQUENCES section of man zshmisc:

   %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 `~'.
like image 139
ZyX Avatar answered Nov 09 '22 01:11

ZyX