removing strange characters from a zsh prompt

I installed zsh 5.3 and am using it.

Now in .zshrc I have done the following –

autoload -Uz compinit promptinit
compinit
promptinit
# End of lines added by compinstall

Now I cycled through some of the prompts and liked one big_fade

The big_fade looks like this –

۲��[email protected]���۲�� Mon Dec 26 02:24:01am
/usr/share/zsh/functions/Prompts>

The builtin code of the prompt is at /usr/share/zsh/functions/Prompts/prompt_bigfade_setup

/usr/share/zsh/functions/Prompts> cat prompt_bigfade_setup
# Generic large colour fade-bar prompt theme from bashprompt
# Created by James Manning <[email protected]>
# Changed by Spidey 08/06
# Converted to zsh prompt theme by <[email protected]>

prompt_bigfade_help () {
  cat <<EOH
This prompt is color-scheme-able.  You can invoke it thus:

  prompt bigfade [<fade-bar> [<userhost> [<date> [<cwd>]]]]

where the parameters are the colors for the fade-bar, [email protected] text,
date text, and current working directory respectively.  The default
colors are blue, white, white, and yellow.  This theme works best with
a dark background.


Recommended fonts for this theme: either UTF-8, or nexus or vga or similar.
If you don't have any of these, the 8-bit characters will probably look
stupid.
EOH
}

prompt_bigfade_setup () {
  local fadebar=${1:-'blue'}
  local userhost=${2:-'white'}
  local date=${3:-'white'}
  local cwd=${4:-'yellow'}

  local -A schars
  autoload -Uz prompt_special_chars
  prompt_special_chars

  PS1="%B%F{$fadebar}$schars[333]$schars[262]$schars[261]$schars[260]%B%F{$userhost}%K{$fadebar}%[email protected]%m%b%k%f%F{$fadebar}%K{black}$schars[260]$schars[261]$schars[262]$schars[333]%b%f%k%F{$fadebar}%K{black}$schars[333]$schars[262]$schars[261]$schars[260]%B%F{$date}%K{black} %D{%a %b %d} %D{%I:%M:%S%P}$prompt_newline%B%F{$cwd}%K{black}%d>%b%f%k "
  PS2="%B%F{$fadebar}$schars[333]$schars[262]$schars[261]$schars[260]%b%F{$fadebar}%K{black}$schars[260]$schars[261]$schars[262]$schars[333]%F{$fadebar}%K{black}$schars[333]$schars[262]$schars[261]$schars[260]%B%F{$fadebar}>%b%f%k "

  prompt_opts=(cr subst percent)
}

prompt_bigfade_preview () {
  if (( ! $#* )); then
    prompt_preview_theme bigfade
    print
    prompt_preview_theme bigfade red white grey white
  else
    prompt_preview_theme bigfade "[email protected]"
  fi
}

prompt_bigfade_setup "[email protected]"

Now while all this is good, I want to do some changes to the prompt. For instance I don’t want the special symbols ‘۲��’ .

Also, at the very end the prompt ends with > . I want to end it as $ (similar to bash) . Can somebody help?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Recommended fonts for this theme: either UTF-8, or nexus or vga or similar.
If you don't have any of these, the 8-bit characters will probably look stupid.

You read this comment in the script, did you not? ☺

The prompt_special_chars module is setting up an array variable named schars that contains the output sequences for various characters that then get used in your prompt as you can see. They are line drawing and block characters, mainly. The prompt_special_chars module is right there next to the one that you are already looking at.

Your terminal (emulator) is apparently correctly configured to recognize the UTF-8 encoded output that prompt_special_chars puts into these strings. (This is a good thing, given that the Debian people, now in December 2016, are resurrecting the proposal to outright do away with any terminal emulators in Debian that aren’t UTF-8.) But it lacks the font support for the relevant glyphs, and is substituting a replacement character because your fonts do not have the actual line-drawing and box characters.

So as the comment said, set your terminal emulator up with fonts that incorporate these Unicode line-drawing and block characters. Look for a font that has the Windows Glyph List 4 (WGL4) repertoire, for instance.

If you don’t like the block characters, then a prompt design where that is the primary point is not for you. You are essentially saying that you like the prompt design except that you dislike the distinctive thing that it does, which is a contradiction. The block characters are used for the “colour fading” effect that is in the name “bigfade”.

Solution 2

@jdeBP – thank you, you provided the first clues.

This is the output I was getting –

strange characters in zsh prompt

Then I looked up if the font was not there –

> aptitude search xfonts-nexus
i   xfonts-nexus                                                       - Nexus font for X 

I realized that the font is there, then the only possibility left is the console i.e. .zsh has not been told it needs to use utf-8 and this was confirmed with –

> locale
LANG=en_IN
LANGUAGE=en_IN:en
LC_CTYPE="en_IN"
LC_NUMERIC="en_IN"
LC_TIME="en_IN"
LC_COLLATE="en_IN"
LC_MONETARY="en_IN"
LC_MESSAGES="en_IN"
LC_PAPER="en_IN"
LC_NAME="en_IN"
LC_ADDRESS="en_IN"
LC_TELEPHONE="en_IN"
LC_MEASUREMENT="en_IN"
LC_IDENTIFICATION="en_IN"
LC_ALL=

After realizing it, I knew what I needed to do, from How can I enable UTF-8 support in the Linux console? @GAD3R’s answer.

I just added those bits –

/home/shirish> cat ~/.zsh/.zshrc | grep UTF-8 
export LC_ALL=en_IN.UTF-8
export LANG=en_IN.UTF-8
export LANGUAGE=en_IN.UTF-8

sourced those bits for the prompt –

/home/shirish> source ~/.zsh/.zshrc

and got the correct prompt –

prompt with nice colors

saw that locale is also nice,correct now.

/home/shirish> locale
LANG=en_IN.UTF-8
LANGUAGE=en_IN.UTF-8
LC_CTYPE="en_IN.UTF-8"
LC_NUMERIC="en_IN.UTF-8"
LC_TIME="en_IN.UTF-8"
LC_COLLATE="en_IN.UTF-8"
LC_MONETARY="en_IN.UTF-8"
LC_MESSAGES="en_IN.UTF-8"
LC_PAPER="en_IN.UTF-8"
LC_NAME="en_IN.UTF-8"
LC_ADDRESS="en_IN.UTF-8"
LC_TELEPHONE="en_IN.UTF-8"
LC_MEASUREMENT="en_IN.UTF-8"
LC_IDENTIFICATION="en_IN.UTF-8"
LC_ALL=en_IN.UTF-8

(rant)-Thanks to you came to know about the proposal to do away with terminal emulators in Debian which don’t support utf-8 . I’ll be reading the whole thread but would actually say, it’s not just the terminal, every app. should be utf-8 and moreover, the default font for Debian should be utf-8. Although the last part cannot happen unless other things are in place first. I shouldn’t have had to put in UTF-8 credentials in my .zshrc, they should be part of the default experience. (/rant)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply