Account Customisation (zsh)

From Redbrick Wiki
Revision as of 04:55, 10 February 2007 by Undone (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How do I improve my redbrick account look? (zsh)

[ Author : Eoin (cambo) : 2002 ] Changing The Command Prompt

Right, you've seen you friends with color 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

cambo@prodigy (~) % 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 everytime 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

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 - prodigy.redbrick.dcu.ie

%m

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

%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 everytime you press return, and you can see your current working directory to save you typing "pwd" everytime you forget where you are. e.g.

cambo@prodigy (~) % export PS1="[%B%n%b @ %M]>" [cambo @ prodigy]> export RPS1="[%* on %D]" [cambo @ prodigy]> [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 customized 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

cambo@prodigy (~) % 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.

Tricks of the trade Pre-commands

This is just a sample of how you can make your account "LOOK" better. there are plenty more trix 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, everytime 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

  1. create dynamic right prompt with date and message status

function precmd {

      export RPS1="(%D)[mesg `mesg`]" 

}

Auto completeion

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`); }

  1. generates a list of usernames from your .friends file

compctl -K userlist hey

  1. adds this list of names to auto completion for the command "hey <username>"

compctl -K userlist ps -fU

  1. 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 cambo" (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)

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

(~)[cambo@prodigy]>> ps -fU cambo

 UID   PID  PPID  C    STIME   TTY     TIME CMD

cambo 25776 25774 0 10:01:56 pts/24 0:00 -zsh cambo 19580 19569 0 23:35:58 pts/110 0:02 -zsh root 5199 19580 0 10:08:16 pts/110 0:00 ps -fU cambo (~)[cambo@prodigy]>> ARGV0='Custom Message' exec zsh (~)[cambo@prodigy]>> ps -fU cambo

 UID   PID  PPID  C    STIME   TTY     TIME CMD

cambo 25776 25774 0 10:01:56 pts/24 0:00 -zsh cambo 19580 19569 0 23:35:58 pts/110 0:02 Custom Message root 5199 19580 0 10:08:16 pts/110 0:00 ps -fU cambo (~)[cambo@prodigy]>>

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.