Customise your prompt

From Global Shellz Wiki
Jump to: navigation, search

Contents

Overview

When you first log in to your shell, one of the first things you'll notice is that your prompt looks a bit different from what you may be used to. I have provided this guide and reference to help you change it to how you like it.

Playing around in your terminal

The variable PS1 holds the default prompt text and styling, PS2 contains the secondary prompt and etcetera. To view the current prompt layout, type in:

 [user@sr2 ~]$ echo $PS1 

Which will give you the output:

\[\033[00;32m\][\[\033[00;32m\]\u\[\033[00;32m\]@\[\033[00;32m\]\h \w\[\033[00;32m\]]\$\[\033[00;97m\]

This may look daunting, but its quite simple really. Lets break it down.

  • \[ - Start a sequence of non-printing characters. This can be used to insert control sequences into your prompt and stops it looking malformed as you're typing. \] ends the sequence.
  • \033[00;32m - \033 or \e are the escapes used to add colour and formatting to your prompt. you can add as many codes between the [ and the m as long as you use a semi-colon to separate them. This particular set uses code 00 to restore default formatting and then code 32 to make the text green. There is a list of each code and the formatting it provides in the Formatting section below.
  • \u prints the username of the current user
  • \h prints the hostname up to the first period (.) in the hostname.
  • \w prints the current working tree from your home folder. For Example, ~/public_html
  • \$ is just a normal dollar sign. Dollar signs in BASH are used to call variables, as you saw earlier.

Play around with the escapes a bit which are detailed below, when you want to see what it looks like, simply export it to PS1

[user@sr2 ~]$ export PS1='\[\e[0;7;1m\] \u \[\e[0m\] @ \[\e[41;1m\] \H \[\e[0m\] -> \[\e[44;1m\] \w \[\e[0;1;97m\] $ '
 user  @  sr2  ->  ~/public_html  $

If you combine 01 with 30 to 37, you get the 90 to 97 colours in bold instead.

Saving what you've got

When you find a setup you like, open ~/.bashrc in your favourite editor and scroll to the bottom. Comment out the existing export PS1 line out by placing a pound sign (#) in front of it and adding your own export PS1='your prompt code' underneath it.

#export PS1='\[\033[00;32m\][\[\033[00;32m\]\u\[\033[00;32m\]@\[\033[00;32m\]\h \w\[\033[00;32m\]]\$\[\033[00;97m\] '
export PS1='\[\e[1;94m\]-(\[\e[0;97m\]\u@\h\[\e[0;94;1m\])-(\[\e[0;97m\]Jobs: \j\[\e[0;94;1m\])-(\[\e[0;97m\]\D{%a %d %B %y}\[\e[0;94;1m\])-(\[\e[0;97m\]\A\[\e[0;1;94m\])-(\[\e[0;97m\]\w\[\e[0;94;01m\])-\[\e[0;97m\]\n$  '

Reference

Formatting

Misc
00 Normal 01 Bold White 02 Grey 04 Underline 07 Silver background with Transparent Text 09 Strikethrough
Text Colours
30 Black 31 Red 32 Green 33 Orange 34 Blue 35 Purple 36 Cyan 37 Silver
90 Dark Grey 91 Light Pink 92 Light Green 93 Yellow 94 Royal Blue 95 Light Purple 96 Cyan 97 White Text
Background Colours
40 Black Background 41 Red Background 42 Green Background 43 Orange Background 44 Blue Background 45 Purple Background 46 Cyan Background 47 Light Silver Background
100 Dark Grey 101 Light Pink 102 Light Green 103 Yellow 104 Royal Blue 105 Light Purple 106 Cyan 107 White

Escapes

This is straight from the man page for BASH(1)

\a     an ASCII bell character (07)
\d     the  date  in "Weekday Month Date" format (e.g., "Tue May
      26")
\D{format}
      the format is passed to strftime(3)  and  the  result  is
      inserted  into the prompt string; an empty format results
      in a locale-specific time representation.  The braces are
      required
\e     an ASCII escape character (033)
\h     the hostname up to the first `.'
\H     the hostname
\j     the number of jobs currently managed by the shell
\l     the basename of the shell's terminal device name
\n     newline
\r     carriage return
\s     the  name  of  the shell, the basename of $0 (the portion
      following the final slash)
\t     the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\A     the current time in 24-hour HH:MM format
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V     the release of bash, version + patch level (e.g., 2.00.0)
\w     the current working  directory,  with  $HOME  abbreviated
      with  a tilde (uses the value of the PROMPT_DIRTRIM vari-
      able)
\W     the basename of the current working directory, with $HOME
      abbreviated with a tilde
\!     the history number of this command
\#     the command number of this command
\$     if the effective UID is 0, a #, otherwise a $
\nnn   the character corresponding to the octal number nnn
\\     a backslash
\[     begin  a sequence of non-printing characters, which could
      be used to embed a terminal  control  sequence  into  the
      prompt
\]     end a sequence of non-printing characters

Some Examples

\[\e[0;34;1m\]-(\[\e[0;1m\]\u\[\e[34m\]@\[\e[0;1m\]\h\[\e[34;1m\])-(\[\e[0;1m\]Jobs: \j\[\e[34m\])-(\[\e[0;1m\]\D{%a %d %B %y}\[\e[34m\])-(\[\e[0;1m\]\A\[\e[34;1m\])-(\[\e[0;1m\]\w\[\e[34m\])-\[\e[0;1m\]\n$
-(user@sr2)-(Jobs: 0)-(Thu 18 August 11)-(13:00)-(~)-
$
\[\e[0;31m\](\[\e[0m\]\u@\h\[\e[31m)\e[0m \W $ 
(user@sr2) public_html $
\u@\h - \@ - \w $
user@sr2 - 13:00 - ~/public_html $
\[\e]0;\u@\h: \w\a\]\T ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ 
07:42:36 user@sr2:~/public_html$
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox