Account Customisation (zsh)

From Redbrick Wiki

This page contains information on how to customise your .zsh shell account.

Changing The Command Prompt

Right, you've seen you friends with colour command prompts showing them who is heying them with lots of other bells and whistles and you wanna be part of the "cool" gang. Unfortunately you've just gotten your account and it's... kinda plain. Well then you've come to the right place. The first thing we're going to do is show you how to set your own command prompt message. This is very easy to do, simply exporting a system variable as follows:

receive@murphy (~) % export PS1="[zsh prompt]>"
[zsh prompt]> export RPS1="[right side prompt]"
[zsh prompt]>                           [right side prompt]

All we have done here is exported the two variables for the left side of the command prompt (PS1) and the right side of the command prompt (RPS1). You can add these two commands to your .zshrc so that your command prompts will be set like so every time you log in. Just place these lines in your .zshrc:

export PS1="Your Left Side Command Prompt ==>"
export RPS1="Your Right Side Command Prompt ==>"

and then source your .zshrc by typing:

source .zshrc

Using zsh escape sequences

zsh has some system escape sequences which can be used in your command prompt also. These sequences are special characters which relate to system properties. They can be included in your command prompts to make your prompt more dynamic. Some of the common escape sequences are:

%d

Your current working directory relative to /home/

%~

Your current working directory relative to /home/member/u/username

%B....%b

Begin and end bold print

%U....%u

Begin and end underlined print

%M

server name - murphy.redbrick.dcu.ie

%m

The part of the hostname up to the first . - murphy

%n

Your Login Name

%T

System time in HH:mm format

%*

System time in HH:mm:ss format

%D

Today's Date in YY-MM-DD

%l

Your current tty e.g. pts/100

Including these in your command prompt exports allows you to have a clock on your command prompt which updates every time you press return, and you can see your current working directory to save you typing "pwd" every time you forget where you are. e.g.

receive@murphy (~) % export PS1="[%B%n%b @ %M]>"
[receive @ murphy]> export RPS1="[%* on %D]"
[receive @ murphy]>           [12:00:00 on 02-05-15]

Adding colours to the command prompt

Its still looking kinda plain isn't it? Maybe we could add some colours to it to see what we can do. zsh has another special set of escape characters specifically for printing colours at the prompt and we can also add these sequences to our PS1/RPS1 variables.

At this point it gets a little more complicated. But with a little perseverance you can have your own customised and coloured account. The following are the escape sequences for creating the coloured text.

%{\e[1;31m%}

Red text

%{\e[1;32m%}

Green text

%{\e[1;33m%}

Yellow text

%{\e[1;34m%}

Blue text

%{\e[1;35m%}

Pink text

%{\e[1;36m%}

Cyan text

%{\e[1;37m%}

Grey text

%{\e[1;38m%}

Grey text

%{\e[1;39m%}

Grey text

%{\e[1;30m%}

Dark grey text

%{\e[0m%}

Back to normal

Here's the tricky part. These colour escapes sequences have to be printed to the prompt within the export command. Here's the syntax for making a red word in the left command prompt:

receive@murphy (~) % export PS1="[$(print '%{\e[1;31m%}%n%{\e[0m%}') @ %M]>"

What this command means is:

export PS1="

export the variable

[

print the opening '['

$(print '%{\e[1;31m%}

print the escape for "RED" to the prompt

%n

the escape for username to the prompt

%{\e[0m%}')

print the escape to return to normal text

@ %M]>"

finish printing prompt message.

Pre-commands

This is just a sample of how you can make your account "look" better. there are plenty more tricks which can be added to your account, to customize it for what you need. For example, placing a precmd in your .zshrc can dynamically generate the command prompt, every time you press the enter key. Why would you do this, you might ask? Won't it slow down my prompt and run a process every time I hit the return button for a new line? (Yes, but...) Well let's say you wanted to know, at a glance, whether your mesg status was 'n' or 'y'. A precmd could generate a message as part of your command prompt, every time you pressed enter, to let you know your message status. Placing the following lines in your .zshrc file will accomplish just that.

export PS1="[%n @ %M]>"     #normal left prompt with name and server
#create dynamic right prompt with date and message status
function precmd	{
       export RPS1="(%D)[mesg `mesg`]" 
}

Auto completion

You may have noticed that zsh supports command auto-completion (as do most Bourne-compatible shells). For example if you type "cd pu" and press <-TAB-> it will automatically complete the command for you to "cd public_html". If you also have a directory called "pubs" then auto-complete will give you a list of all possible completions to what you have already typed. You can specify your own auto-completions to the command prompt in your .zshrc also. Maybe you're just lazy and don't want to have to type your friend's username when you're spying on him/her. In this case you could set up and auto completion list from your .friends file. e.g.

function userlist { reply=(`cat ~/.friends`); } 
#generates a list of usernames from your .friends file
 
compctl -K userlist hey 
#adds this list of names to auto completion for the command "hey <username>"
compctl -K userlist ps -fU 
#adds this list of names to auto completion for the command "ps -fU <username>"
set COMPLETE_ALIASES      #adds the alias


So say I'm the only person in your .friends file whose username begins with "c". Typing "ps -fU c<-TAB->" will auto-complete to "ps -fU receive" (NB* this will also work for an alias of that command if you have one set). This can be convenient if you have a list of commands that you use on a regular basis for which aliases would be unsuitable. zsh message (exporting ARGV0)