Account Customisation (zsh)

From RedBrick Wiki
Jump to: navigation, search

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:

Property Escape Sequence
Your current working directory relative to /home/  %d
Your current working directory relative to /home/member/u/username  %~
Begin and end bold print  %B....%b
Begin and end underlined print  %U....%u
server name, eg -  %M
The part of the hostname up to the first . - murphy  %m
Your Login Name  %n
System time in HH:mm format  %T
System time in HH:mm:ss format  %*
Today's Date in YY-MM-DD  %D
Your current tty e.g. pts/100  %l

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.

Colour Escape Sequence
Red text  %{\e[1;31m%}
Green text  %{\e[1;32m%}
Yellow text  %{\e[1;33m%}
Blue text  %{\e[1;34m%}
Pink text  %{\e[1;35m%}
Cyan text  %{\e[1;36m%}
Grey text  %{\e[1;37m%}
Grey text  %{\e[1;38m%}
Grey text  %{\e[1;39m%}
Dark grey text  %{\e[1;30m%}
Back to normal  %{\e[0m%}

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


the escape for username to the prompt


print the escape to return to normal text

@ %M]>"

finish printing prompt message.


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 r<-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)

Removing the help message

Everytime you sign into redbrick, you get the help message.
If you're tired of seeing this all you need to do is add a file called .nohelp in your home directory. You can use the touch command to create a blank file.

receive@murphy (~) % touch .nohelp

Customised zsh messages

You may have seen when you "spy" on another user's processes that they have a customised zsh message in the process list. More and more people do this as an added customisation (and annoyance ;)) to their account.

receive@murphy (~) % ps -fU receive
receive   2120  2118  0 Feb09 pts/212  00:00:01 -zsh
receive  16581  2120  0 02:00 pts/212  00:00:00 ps -fU receive
receive@murphy (~) % ARGV0='Custom Message' exec zsh
receive@murphy (~) % ps -fU receive
receive   2120  2118  0 Feb09 pts/212  00:00:02 Custom Message
receive  22095  2120  0 02:06 pts/212  00:00:00 ps -fU receive

The command ARGV0='Custom Message' exec zsh changes the default message from -zsh to whatever you want. If you want this to be set everytime you login to redbrick, you can place the command in a file called .zlogin in your home directory. This file automatically gets sourced, when you exec a login session on the system.

Be Careful:

If you are going to use a .zlogin file with the ARGV0 variable exported to something else, then you should take any commands from your .zshrc file, which print information to the screen and move them into your .zlogin instead. The reason for this is that when you log in, the system will:

  • source your .zshrc
  • source your .zlogin containing - ARGV0="" exec zsh
  • source your .zshrc again - because of the exec zsh in .zlogin

If there were any commands in your .zshrc that printed to the screen, rbusers for example, then they would end up printing twice. So for any programs which you want to print to the command prompt at login. You should place them in your .zlogin.

Also be aware that resetting ARGV0 from its unset state will annoy some programs, most notably mv and cp, which will complain that the command is not of the right format. To get around this, do unset ARGV0, and then (if you must) reset the ARGV0 to its original value by either running ARGV0='Custom Message' exec zsh or sourcing your .zlogin file.

If you have any problems or mess something up and need help, just talk to Helpdesk.