Search code examples
zshoh-my-zsh

Why is `print -P "%%"` behaviour different within library function?


On my terminal:

$ print -P "%%"
%

The equivalent code within a Spaceship prompt function:

spaceship_extension() {
  unset PROMPT_PERCENT 
  unset PROMPT_SUBST
  print -P "00%%\n"

  set PROMPT_PERCENT 
  unset PROMPT_SUBST
  print -P "01%%\n"
  
  unset PROMPT_PERCENT 
  set PROMPT_SUBST
  print -P "10%%\n"
  
  set PROMPT_PERCENT
  set PROMPT_SUBST
  print -P "11%%\n"
}

Output:

00
01
10
11

According to the Prompt Expansion man page, those are the only relevant environment variables. Does anyone have any idea what's going on here?

Edit: Removed exports that were obviating the setting and resetting of the environment variables.


Solution

  • First problem is that export has no effect on either option; you are just setting the export attribute on a set of names in each case.

    Second, set and unset operate on names, not shell options. You want setopt and unsetopt.

    % setopt PROMPT_PERCENT
    % print -P '00%%\n'
    00%
    
    % unsetopt PROMPT_PERCENT
    % print -P '00%%\n'
    00%%
    

    (In practice, unsetting PROMPT_PERCENT may affect your actual prompt; I used % here as a placeholder for the prompt, not an accurate representation of what you prompt may look like after unsetting the option.)