You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Account_Customisation_(zsh).md 8.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. This page contains information on how to customise your .zsh shell
  2. account.
  3. ## Changing The Command Prompt
  4. Right, you\'ve seen you friends with colour command prompts showing them
  5. who is heying them with lots of other bells and whistles and you wanna
  6. be part of the \"cool\" gang. Unfortunately you\'ve just gotten your
  7. account and it\'s\... kinda plain. Well then you\'ve come to the right
  8. place. The first thing we\'re going to do is show you how to set your
  9. own command prompt message. This is very easy to do, simply exporting a
  10. system variable as follows:
  11. ```
  12. receive@murphy (~) % export PS1="[zsh prompt]>"
  13. [zsh prompt]> export RPS1="[right side prompt]"
  14. [zsh prompt]> [right side prompt]`
  15. ```
  16. All we have done here is exported the two variables for the left side of
  17. the command prompt (PS1) and the right side of the command prompt
  18. (RPS1). You can add these two commands to your .zshrc so that your
  19. command prompts will be set like so every time you log in. Just place
  20. these lines in your .zshrc:
  21. ```
  22. export PS1="Your Left Side Command Prompt ==>"
  23. export RPS1="Your Right Side Command Prompt ==>"
  24. ```
  25. and then source your .zshrc by typing:
  26. `source .zshrc`
  27. ## Using zsh escape sequences
  28. zsh has some system escape sequences which can be used in your command
  29. prompt also. These sequences are special characters which relate to
  30. system properties. They can be included in your command prompts to make
  31. your prompt more dynamic. Some of the common escape sequences are:
  32. Property Escape Sequence
  33. ---
  34. Your current working directory relative to /home/ \%d
  35. Your current working directory relative to /home/member/u/username \%\~
  36. Begin and end bold print \%B\....%b
  37. Begin and end underlined print \%U\....%u
  38. server name, eg - \%M
  39. The part of the hostname up to the first . - murphy \%m
  40. Your Login Name \%n
  41. System time in HH:mm format \%T
  42. System time in HH:mm:ss format \%\*
  43. Today\'s Date in YY-MM-DD \%D
  44. Your current tty e.g. pts/100 \%l
  45. Including these in your command prompt exports allows you to have a
  46. clock on your command prompt which updates every time you press return,
  47. and you can see your current working directory to save you typing
  48. \"pwd\" every time you forget where you are. e.g.
  49. ```
  50. receive@murphy (~) % export PS1="[%B%n%b @ %M]>"
  51. [receive @ murphy]> export RPS1="[%* on %D]"
  52. [receive @ murphy]> [12:00:00 on 02-05-15]
  53. ```
  54. ## Adding colours to the command prompt
  55. Its still looking kinda plain isn\'t it? Maybe we could add some colours
  56. to it to see what we can do. zsh has another special set of escape
  57. characters specifically for printing colours at the prompt and we can
  58. also add these sequences to our PS1/RPS1 variables.
  59. At this point it gets a little more complicated. But with a little
  60. perseverance you can have your own customised and coloured account. The
  61. following are the escape sequences for creating the coloured text.
  62. Colour Escape Sequence
  63. ---
  64. Red text \%{\\e\[1;31m%}
  65. Green text \%{\\e\[1;32m%}
  66. Yellow text \%{\\e\[1;33m%}
  67. Blue text \%{\\e\[1;34m%}
  68. Pink text \%{\\e\[1;35m%}
  69. Cyan text \%{\\e\[1;36m%}
  70. Grey text \%{\\e\[1;37m%}
  71. Grey text \%{\\e\[1;38m%}
  72. Grey text \%{\\e\[1;39m%}
  73. Dark grey text \%{\\e\[1;30m%}
  74. Back to normal \%{\\e\[0m%}
  75. Here\'s the tricky part. These colour escapes sequences have to be
  76. printed to the prompt within the export command. Here\'s the syntax for
  77. making a red word in the left command prompt:
  78. `receive@murphy (~) % export PS1="[$(print '%{\e[1;31m%}%n%{\e[0m%}') @ %M]>"`
  79. What this command means is:
  80. `export PS1="`
  81. export the variable
  82. `[`
  83. print the opening \'\[\'
  84. `$(print '%{\e[1;31m%}`
  85. print the escape for \"RED\" to the prompt
  86. `%n`
  87. the escape for username to the prompt
  88. `%{\e[0m%}')`
  89. print the escape to return to normal text
  90. `@ %M]>"`
  91. finish printing prompt message.
  92. ## Pre-commands
  93. This is just a sample of how you can make your account \"look\" better.
  94. there are plenty more tricks which can be added to your account, to
  95. customize it for what you need. For example, placing a precmd in your
  96. .zshrc can dynamically generate the command prompt, every time you press
  97. the enter key. Why would you do this, you might ask? Won\'t it slow down
  98. my prompt and run a process every time I hit the return button for a new
  99. line? (Yes, but\...) Well let\'s say you wanted to know, at a glance,
  100. whether your mesg status was \'n\' or \'y\'. A precmd could generate a
  101. message as part of your command prompt, every time you pressed enter, to
  102. let you know your message status. Placing the following lines in your
  103. .zshrc file will accomplish just that.
  104. `export PS1="[%n @ %M]>" #normal left prompt with name and server`
  105. `#create dynamic right prompt with date and message status`\
  106. `function precmd {`\
  107. `` export RPS1="(%D)[mesg `mesg`]" ``\
  108. `}`
  109. ## Auto completion
  110. You may have noticed that zsh supports command auto-completion (as do
  111. most Bourne-compatible shells). For example if you type \"cd pu\" and
  112. press \<-TAB-\> it will automatically complete the command for you to
  113. \"cd public_html\". If you also have a directory called \"pubs\" then
  114. auto-complete will give you a list of all possible completions to what
  115. you have already typed. You can specify your own auto-completions to the
  116. command prompt in your .zshrc also. Maybe you\'re just lazy and don\'t
  117. want to have to type your friend\'s username when you\'re spying on
  118. him/her. In this case you could set up and auto completion list from
  119. your .friends file. e.g.
  120. `` function userlist { reply=(`cat ~/.friends`); } ``\
  121. `#generates a list of usernames from your .friends file`\
  122. ``\
  123. `compctl -K userlist hey`\
  124. `#adds this list of names to auto completion for the command "hey`<username>`"`
  125. `compctl -K userlist ps -fU`\
  126. `#adds this list of names to auto completion for the command "ps -fU`<username>`"`\
  127. `set COMPLETE_ALIASES #adds the alias`
  128. So say I\'m the only person in your .friends file whose username begins
  129. with \"c\". Typing \"ps -fU r\<-TAB-\>\" will auto-complete to \"ps -fU
  130. receive\" (NB\* this will also work for an alias of that command if you
  131. have one set). This can be convenient if you have a list of commands
  132. that you use on a regular basis for which aliases would be unsuitable.
  133. zsh message (exporting ARGV0)
  134. ## Removing the help message
  135. Everytime you sign into redbrick, you get the help message.\
  136. If you\'re tired of seeing this all you need to do is add a file called
  137. .nohelp in your home directory. You can use the touch command to create
  138. a blank file.
  139. `receive@murphy (~) % touch .nohelp`
  140. ## Customised zsh messages
  141. You may have seen when you \"spy\" on another user\'s processes that
  142. they have a customised zsh message in the process list. More and more
  143. people do this as an added customisation (and annoyance ;)) to their
  144. account.
  145. `receive@murphy (~) % ps -fU receive`\
  147. `receive 2120 2118 0 Feb09 pts/212 00:00:01 -zsh`\
  148. `receive 16581 2120 0 02:00 pts/212 00:00:00 ps -fU receive`
  149. `receive@murphy (~) % ARGV0='Custom Message' exec zsh`
  150. `receive@murphy (~) % ps -fU receive`\
  152. `receive 2120 2118 0 Feb09 pts/212 00:00:02 Custom Message`\
  153. `receive 22095 2120 0 02:06 pts/212 00:00:00 ps -fU receive`
  154. The command ARGV0=\'Custom Message\' exec zsh changes the default
  155. message from -zsh to whatever you want. If you want this to be set
  156. everytime you login to redbrick, you can place the command in a file
  157. called .zlogin in your home directory. This file automatically gets
  158. sourced, when you exec a login session on the system.
  159. **Be Careful:**
  160. If you are going to use a .zlogin file with the ARGV0 variable exported
  161. to something else, then you should take any commands from your .zshrc
  162. file, which print information to the screen and move them into your
  163. .zlogin instead. The reason for this is that when you log in, the system
  164. will:
  165. * source your .zshrc
  166. * source your .zlogin containing - ARGV0=\"\" exec zsh
  167. * source your .zshrc again - because of the exec zsh in .zlogin
  168. If there were any commands in your .zshrc that printed to the screen,
  169. rbusers for example, then they would end up printing twice. So for any
  170. programs which you want to print to the command prompt at login. You
  171. should place them in your .zlogin.
  172. Also be aware that resetting ARGV0 from its unset state will annoy some
  173. programs, most notably mv and cp, which will complain that the command
  174. is not of the right format. To get around this, do unset ARGV0, and then
  175. (if you must) reset the ARGV0 to its original value by either running
  176. ARGV0=\'Custom Message\' exec zsh or sourcing your .zlogin file.
  177. If you have any problems or mess something up and need help, just talk
  178. to [Helpdesk](/Helpdesk "wikilink").
  179. [Category:Helpdesk](/Category:Helpdesk "wikilink")