Terminal User Prompt Formatting

I was just experimenting with formatting the user prompt bash uses in the terminal. After reading up on available settings, this was the configuration I settled on:

[\e[0;34m\u\e[m@\h \e[0;32m\W\e[m]\$

This colours the user blue, and the working directory green. A sample output is as follows:

[user@host directory]$

The issue I had with this, is once I would enter a command that would surpass the boundary of the window, instead of wrapping to the next one it would overlap the same line. There were also issues with part of a previous command overlapping a new one. After looking at several solutions and posts, an Ask Ubuntu Answer gave me the solution I wanted. All I had to do was wrap the colour codes (\e[_m) in escaped square brackets. The modified (fixed) version is as follows:

[\[\e[0;34m\]\u\[\e[m\]@\h \[\e[0;32m\]\W\[\e[m\]]\$

Other suggestions I’ve seen, which do not appear to have been effective, include:

  • doubling up on backslashes (\ -> \\)
  • adding an extra square bracket after the m (\e[m\])
  • wrapping the expression in single/double quotes (changing them)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s