Account Customisation (zsh): Difference between revisions

From Redbrick Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
Changing The Command Prompt
This page contains information on how to customise your .zsh shell account.


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
== Changing The Command Prompt ==


cambo@prodigy (~) % export PS1="[zsh 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:
[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
cambo@prodigy (~) % export PS1="[zsh prompt]>"
[zsh prompt]> export RPS1="[right side prompt]"
[zsh prompt]>                          [right side prompt]


export PS1="Your Left Side Command 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 RPS1="Your Right Side Command Prompt ==>"


and then source your .zshrc
export PS1="Your Left Side Command Prompt ==>"
  export RPS1="Your Right Side Command Prompt ==>"


source ~/.zshrc
and then source your .zshrc by typing:
Using zsh escape sequences
 
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:
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
%d
    Your current working directory relative to /home/
Your current working directory relative to /home/
%~
%~
    Your current working directory relative to /home/member/u/username
Your current working directory relative to /home/member/u/username
%B....%b
%B....%b
    Begin and end bold print
Begin and end bold print
%U....%u
%U....%u
    Begin and end underlined print
Begin and end underlined print
%M
%M
    server name - prodigy.redbrick.dcu.ie
server name - prodigy.redbrick.dcu.ie
%m
%m
    The part of the hostname up to the first . - prodigy
The part of the hostname up to the first . - prodigy
%n
%n
    Your Login Name
Your Login Name
%T
%T
    System time in HH:mm format
System time in HH:mm format
%*
%*
    System time in HH:mm:ss format
System time in HH:mm:ss format
%D
%D
    Today's Date in YY-MM-DD
Today's Date in YY-MM-DD
%l
%l
    Your current tty e.g. pts/100
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.
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 PS1="[%B%n%b @ %M]>"
[cambo @ prodigy]> export RPS1="[%* on %D]"
[cambo @ prodigy]> export RPS1="[%* on %D]"
[cambo @ prodigy]>          [12:00:00 on 02-05-15]
[cambo @ prodigy]>          [12:00:00 on 02-05-15]


== Adding colours to the command prompt ==
== Adding colours to the command prompt ==
Line 54: Line 57:
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.
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%}
%{\e[1;31m%}
    Red Text
Red text
%{\e[1;32m%}
%{\e[1;32m%}
    Green Text
Green text
%{\e[1;33m%}
%{\e[1;33m%}
    Yellow Text
Yellow text
%{\e[1;34m%}
%{\e[1;34m%}
    Blue Text
Blue text
%{\e[1;35m%}
%{\e[1;35m%}
    Pink Text
Pink text
%{\e[1;36m%}
%{\e[1;36m%}
    Cyan Text
Cyan text
%{\e[1;37m%}
%{\e[1;37m%}
    Grey Text
Grey text
%{\e[1;38m%}
%{\e[1;38m%}
    Grey Text
Grey text
%{\e[1;39m%}
%{\e[1;39m%}
    Grey Text
Grey text
%{\e[1;30m%}
%{\e[1;30m%}
    Dark Grey Text
Dark grey text
%{\e[0m%}
%{\e[0m%}
    Back To Normal
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
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]>"
cambo@prodigy (~) % export PS1="[$(print '%{\e[1;31m%}%n%{\e[0m%}') @ %M]>"


What this command means is:
What this command means is:


export PS1="
export PS1="
    export the variable
export the variable
[
[
    print the opening '['
print the opening '['
$(print '%{\e[1;31m%}
$(print '%{\e[1;31m%}
    print the escape for "RED" to the prompt
print the escape for "RED" to the prompt
%n
%n
    the escape for username to the prompt
the escape for username to the prompt
%{\e[0m%}')
%{\e[0m%}')
    print the escape to return to normal text
print the escape to return to normal text
@ %M]>"
@ %M]>"
    finish printing prompt message.
finish printing prompt message.


Tricks of the trade
== Pre-commands ==
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.
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
export PS1="[%n @ %M]>"    #normal left prompt with name and server


#create dynamic right prompt with date and message status
#create dynamic right prompt with date and message status
function precmd {
function precmd {
      export RPS1="(%D)[mesg `mesg`]"  
        export RPS1="(%D)[mesg `mesg`]"  
}
}


== Auto completion ==
== Auto completion ==
Line 112: Line 115:
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.
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`); }  
function userlist { reply=(`cat ~/.friends`); }  
#generates a list of usernames from your .friends file
#generates a list of usernames from your .friends file


compctl -K userlist hey  
compctl -K userlist hey  
#adds this list of names to auto completion for the command "hey <username>"
#adds this list of names to auto completion for the command "hey <username>"


compctl -K userlist ps -fU  
compctl -K userlist ps -fU  
#adds this list of names to auto completion for the command "ps -fU <username>"
#adds this list of names to auto completion for the command "ps -fU <username>"
set COMPLETE_ALIASES      #adds the alias
set COMPLETE_ALIASES      #adds the alias




Line 128: Line 131:
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.
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  
(~)[cambo@prodigy]>> ps -fU cambo  
  UID  PID  PPID  C    STIME  TTY    TIME CMD
  UID  PID  PPID  C    STIME  TTY    TIME CMD
cambo 25776 25774  0 10:01:56 pts/24    0:00 -zsh
cambo 25776 25774  0 10:01:56 pts/24    0:00 -zsh
cambo 19580 19569  0 23:35:58 pts/110  0:02 -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
root  5199 19580  0 10:08:16 pts/110  0:00 ps -fU cambo
(~)[cambo@prodigy]>> ARGV0='Custom Message' exec zsh
(~)[cambo@prodigy]>> ARGV0='Custom Message' exec zsh
(~)[cambo@prodigy]>>  ps -fU cambo
(~)[cambo@prodigy]>>  ps -fU cambo
  UID  PID  PPID  C    STIME  TTY    TIME CMD
  UID  PID  PPID  C    STIME  TTY    TIME CMD
cambo 25776 25774  0 10:01:56 pts/24    0:00 -zsh
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   
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
root  5199  19580  0 10:08:16 pts/110  0:00 ps -fU cambo
(~)[cambo@prodigy]>>
(~)[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.
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:
 
''''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:
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 .zshrc
    * source your .zlogin containing - ARGV0="" exec zsh
* source your .zlogin containing - ARGV0="" exec zsh
    * source your .zshrc again - because of the exec zsh in .zlogin
* 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.
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.


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.


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]].

Revision as of 05:10, 10 February 2007

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:

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 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 - 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.

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 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.

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