Customizing Zsh (Learning)

I don’t set my theme in zsh. I use starship for it. I source starship in my zshrc. Starship has a separate configuration and supports those special characters you see called nerd fonts.

Those are commands to enhance autocompletions and define keybindings for them.

How can I understand what is written in the aforementioned script ?

Google, duckduckgo, bing are your best friends. Copy the commands you want to learn paste it in the search box and go
:man_technologist:

function theme_precmd {
  local TERMWIDTH=$(( COLUMNS - ${ZLE_RPROMPT_INDENT:-1} ))

Grabs the current terminals max width and writes it into the TERMWIDTH variable.

  PR_FILLBAR=""
  PR_PWDLEN=""

Creates two variables that are empty and may be used later

  local promptsize=${#${(%):---(%n@%m:%l)---()--}}

Sets the width for the local prompt when the user is not processing Ruby code or doing a PWD (Print Working Direstory). Then writes it into the promptsize variable.

  local rubypromptsize=${#${(%)$(ruby_prompt_info)}}

Sets the width for the prompt when the user is processing Ruby code. Then writes it into the rubypromptsize variable.

  local pwdsize=${#${(%):-%~}}

Sets the width for the prompt when the user is running the pwd command. Then writes it into the pwdsize variable.

  # Truncate the path if it's too long.
  if (( promptsize + rubypromptsize + pwdsize > TERMWIDTH )); then
    (( PR_PWDLEN = TERMWIDTH - promptsize ))

If the prompt is too long, finds out the amount of characters it is bigger than the terminal width

  elif [[ "${langinfo[CODESET]}" = UTF-8 ]]; then
    PR_FILLBAR="\${(l:$(( TERMWIDTH - (promptsize + rubypromptsize + pwdsize) ))::${PR_HBAR}:)}"

It then checks if the user is using a UTF-8 based language and cuts the bar into multiple lines based on the width of the terminal, which is not really great as it doesn’t take into account where the line is being cut, so it could be mid word.

  else
    PR_FILLBAR="${PR_SHIFT_IN}\${(l:$(( TERMWIDTH - (promptsize + rubypromptsize + pwdsize) ))::${altchar[q]:--}:)}${PR_SHIFT_OUT}"
  fi
}

If the user is not using a UTF-8 language it just cuts the code at the altchar(q) character.

2 Likes

Used this little helper to make my prompt https://zsh-prompt-generator.site/

Nowadays I use starship too.

2 Likes

Hi,

I was like you and had custimized my zsh prompt. It was fun to learn but not really practical (for me). So this is,why i switched to Starship.

These are my notes for my install where i did not use oh my zsh:

How to setup ZSH in Arch Linux

(1) Verify what shell is currently installed (default is bash).

$ echo $SHELL

(2) Install ZSH

sudo pacman -S zsh

(3) Install ZSH plugins
sudo pacman -S zsh-autosuggestions zsh-completions zsh-syntax-highlighting starship
yay -S nerd-fonts-jetbrains-mono
Note: Could substitute to package ‘nerd-fonts-hack’.
(4) Add the Nerd Font of your choice to your terminal (e.g Alacritty)

(a) Edit Alacritty configuration file and add your preferred Nerd Font.
sudo nano .config/alacritty/alacritty.yml

(b) Update the preferred font

Font configuration

font:
normal:
family: JetBrainsMono Nerd Font
style: Regular
bold:
family: JetBrainsMono Nerd Font
style: bold

Italic font face

italic:
family: JetBrainsMono Nerd Font
style: italic
(5) Configure ZSH basic User Configuration:

From terminal run ‘zsh’ which will start the wizard.

(6) Change the Current User to use ZSH Shell

–Must Log out and back in to get the change

chsh -s /usr/bin/zsh

(7) Setup ZSH Plugins and Starship Prompt

(a) Edit the .zshrc file

sudo nano ~/.zshrc
(b) Use the following .zshrc content in your configuration file:

Lines configured by zsh-newuser-install

HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
bindkey -e

#Path to Functions and Plugins
export ZSH=“/usr/share/zsh”
fpath=($ZSH/functions $ZSH/site-functions $fpath)

#Source ZSH Plugins
source $ZSH/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source $ZSH/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 2>/dev/null

End of lines configured by zsh-newuser-install

The following lines were added by compinstall

zstyle :compinstall filename ‘/home/rsruser/.zshrc’

autoload -Uz compinit; compinit

End of lines added by compinstall

This will set Starship as default prompt theme

eval “$(starship init zsh)”

(b) Enable starship on the BASH shell

(b1) Edit the ~/.bashrc fiel

sudo nano ~/.bashrc

(b2) Add the following line at the end of the file:

eval “$(starship init bash)”

Source:

https://wiki.archlinux.org/title/Zsh

Plug-ins:

1 Like

Thanks for your suggestions, for entire day today I was scratching my mind in order to learn the methodologies involved, and was in trouble as I was seeing these lines for first time.Though I managed to make a simple looking prompt as you can see in above posts.

With the various suggestions from your side, I think I can try to learn it a more.

And also thanks to @NeoFax , for providing the meaning of the lines.

I use grml-zsh-config package and modified the ‘.zshrc.local’ file. You can download these files without install the package. Just type the following: ‘wget -O .zshrc https://git.grml.org/f/grml-etc-core/etc/zsh/zshrc’ and ‘wget -O .zshrc.local https://git.grml.org/f/grml-etc-core/etc/skel/.zshrc

1 Like

While it’s fine to do it like that, I would recommend against it (this is just my personal preference).

Just configure your terminal emulator to run Zsh.

This way, you can keep Bash as your user’s default shell, even though you’ll use it very, very rarely. The benefit is that if you need to use the TTY, you’ll still have Bash by default, which is simpler and just easier for troubleshooting. Also with SSH.

1 Like

Is this method good ?

image

1 Like

Yeah, that’s literally what I said in the post above:


You can check it like this (the current user’s default shell is Bash, but the currently running interactive shell is Zsh):

~🐸 echo $SHELL
/bin/bash
~🐸 echo $0
/bin/zsh

Like I said, that’s just my personal preference, I find it the most convenient. You’re, of course, free to chsh into Zsh, if you really want to… I wouldn’t, though.

2 Likes

Some tough time is going on with understanding, but I am trying…

It takes time, don’t try to force it :wink:

The more you use a shell, the more proficient you’ll be with it. And it also doesn’t hurt to read about it, but it is necessary to apply in practice what you read, otherwise you’ll forget it.

1 Like

What are the issues with zsh over ssh? I use it all the time.

Who said there were any issues?

It’s a personal preference, I’d like it to be predictable. Bash is predictable.

You said you wouldn’t recommend using zsh over tty or ssh. I was just curious about the reason why.

I have changed the default shell to zsh not only the terminal emulator.

Like I said, it’s a personal preference.

I like to customise my Zsh much more than Bash.

Now, some fancy Zsh features like autosuggestions work rather poorly in the TTY, at least the way I have them configured, because of the limited colour palette in the TTY. In general, if I’m using the TTY, I’m using it because I can’t use my terminal emulator of choice – almost certainly I’m in troubleshooting mode and I don’t want to be distracted by a shell that is too fancy (even though, normally, that would be convenient).

Same thing with SSH, if it is not predictable from where I am accessing it, I may have limited terminal features. I could be using the TTY in one computer to SSH into another.

Finally, I like to keep my .zshrc branchless. I could have something like

if [[ $TERM = "xterm-256color" ]]; then
  # use this setting
else 
  # use that setting
fi

but I don’t like doing that, branching just slows down the loading of .zshrc, makes it bigger and more difficult to read, and keeping and maintaining elif branches that I’m going to use in less than 1% of cases is just not something I want to do.

On the other hand, Bash is super predictable. It’s a powerful shell on its own, not much inferior to Zsh, except that it lacks ZLE and all its fancy features. I find Bash completely sufficient for TTY and I like its predictability with SSH.

I still use Zsh in more than 99% of all interactive shell sessions even if it is not my user’s default shell. And since the terminal is my preferred way of interacting with my computers, that’s a lot of hours I spend in Zsh.

1 Like

Presently, I am only aware of doing system maintenance via terminal. Therefore, what what you mean when you say

?

And are the things which I can do about which you had guided to me ?

That is definitely possible.

monocle_72

Sorry, I didn’t got this.