Zsh: Oh My Zsh – customise prompt / PS1

TLDR

vim +92 ~/.oh-my-zsh/custom/themes/agnoster.zsh-theme

and change prompt from

prompt_segment black default "%(!.%{%F{yellow}%}.)%n@%m"

to

prompt_segment black default "%(!.%{%F{yellow}%}.)text-you-want-to-appear"

then source it with:

. ~/.zshrc

 

DETAILS

I put my custom theme in:

~/.oh-my-zsh/custom/themes/

e.g.

~/.oh-my-zsh/custom/themes/agnoster.zsh-theme

 

Editing the PS1 means changing the PROMPTvariable which looks like this:

PROMPT='%{%f%b%k%}$(build_prompt) '

 

From man zshmisc

%{…%}
Include a string as a literal escape sequence. The string within the braces
should not change the cursor position. Brace pairs can nest.

A positive numeric argument between the % and the { is treated as described for
%G below.

And

%f%b%k

%F (%f)
Start (stop) using a different foreground colour, if supported by the terminal.
The colour may be specified two ways: either as a numeric argument, as normal,
or by a sequence in braces following the %F, for example %F{red}. In the lat-
ter case the values allowed are as described for the fg zle_highlight
attribute; see Character Highlighting in zshzle(1). This means that numeric
colours are allowed in the second format also.

%B (%b)
Start (stop) boldface mode

%K (%k)
Start (stop) using a different bacKground colour. The syntax is identical to
that for %F and %f.

 

and

I wanted to remove my username which would point to this:

and

%n@%m

is

%n $USERNAME.

%m The hostname up to the first .'. An integer may follow the %’ to specify how
many components of the hostname are desired. With a negative integer, trailing
components of the hostname are shown.

https://stackoverflow.com/questions/35281630/how-do-i-change-my-ps1-on-a-macbook-for-oh-my-zsh

Zsh: horizontal line – hr

add to your .zshrc

https://superuser.com/questions/845744/how-to-draw-a-line-between-commands-in-zsh-shell

 

 

Oh My Zsh

Thought I’d give this a go.

From https://ohmyz.sh/

  1. install zsh (on the Mac this is already installed)
  2. set iTerm2 (https://www.iterm2.com/downloads.html) to use this (chsh -s $(which zsh) and in iTerm2 > Preferences > Profiles > Default > General, set the Command to /bin/zsh instead of Login shell. Note – you can also do this via System Preferences: https://stackoverflow.com/a/44547726/343204
  3. Install Oh My Zsh. i.e. sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" –  https://ohmyz.sh/
  4.  If you’re using the agnoster theme, then be aware you’ll need to use Patched fonts (not a big thing but just make sure you install both ASCII and non-ASCII fonts)  – https://github.com/powerline/fonts. i.e. git clone https://github.com/powerline/fonts.git; cd fonts; ./install.sh and select them as the default fonts in iTerm2 > Preferences > Profiles > Text (change both Font and Non-ASCII Font to 12pt Meslo LG S Regular for Powerline)
  5. The most time-consuming stuff was customisation. Copy chosen theme to ~/.oh-my-zsh/custom/themes/agnoster.zsh-theme and customise. i.e. cp ~/.oh-my-zsh/themes/agnoster.zsh-theme ~/.oh-my-zsh/custom/themes/ and set as active in ~/.zshrc (i.e. ZSH_THEME=”agnoster”)
  6. Set aliases and autojump. E.g. I have:

# Aliases
alias c=”clear”
alias l=”ls”
alias sfs=”php bin/console server:run”
alias hr=”printf ‘%*s\n’ \”${COLUMNS:-$(tput cols)}\” ” | tr ‘ ‘ -”
alias h=”history”
alias tf=”terraform”

[[ -s $(brew –prefix)/etc/profile.d/autojump.sh ]] && . $(brew –prefix)/etc/profile.d/autojump.sh

 

Customisation Notes:

E.g. PROMPT:


# Dir: current working directory
prompt_dir() {
prompt_segment blue black &quot; <code>basename ${PWD/#$HOME/'~'}</code> &quot;
}

OR

prompt_segment blue black '%c'

https://github.com/robbyrussell/oh-my-zsh/issues/5479

E.g. no user@host in command prompt:

add


<code>DEFAULT_USER=$USER</code>

in your zshrc file.

https://github.com/robbyrussell/oh-my-zsh/issues/5512

 

See also

https://stackoverflow.com/questions/35281630/how-do-i-change-my-ps1-on-a-macbook-for-oh-my-zsh

https://github.com/robbyrussell/oh-my-zsh/wiki/Customization

And on the agnoster theme:

  1. red cross means last command had a non-zero exit code: https://github.com/agnoster/agnoster-zsh-theme/issues/54

And a Zsh Cheat Sheet:

https://github.com/robbyrussell/oh-my-zsh/wiki/Cheatsheet

E.g.

git shortcuts such as:

gst: git status

ga: git add

gcam: git commit -m

gp: git push

gpsup:git push –set-upstream origin $(current_branch)