Skip to content

Commit

Permalink
todays post
Browse files Browse the repository at this point in the history
Customize your Linux bash prompt with colors, symbols, time, and more. Learn to use PS1 variables, ANSI escape codes, and cursor positioning to create a personalized command line experience.
  • Loading branch information
spsanderson committed Nov 29, 2024
1 parent cb78ccc commit 96d6f64
Show file tree
Hide file tree
Showing 12 changed files with 2,542 additions and 2,192 deletions.
1,002 changes: 519 additions & 483 deletions docs/index.html

Large diffs are not rendered by default.

930 changes: 605 additions & 325 deletions docs/index.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/listings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"listing": "/index.html",
"items": [
"/posts/2024-11-29/index.html",
"/posts/2024-11-28/index.html",
"/posts/2024-11-27/index.html",
"/posts/2024-11-26/index.html",
Expand Down
1,309 changes: 1,308 additions & 1 deletion docs/posts/2024-11-29/index.html

Large diffs are not rendered by default.

Binary file added docs/posts/2024-11-29/todays_post.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 29 additions & 1 deletion docs/search.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1932,4 +1932,8 @@
<loc>https://www.spsanderson.com/steveondata/posts/2024-11-28/index.html</loc>
<lastmod>2024-11-28T13:11:41.101Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2024-11-29/index.html</loc>
<lastmod>2024-11-29T13:31:07.988Z</lastmod>
</url>
</urlset>
156 changes: 76 additions & 80 deletions posts/2024-11-29/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ categories: [code, linux]
toc: TRUE
description: "Customize your Linux bash prompt with colors, symbols, time, and more. Learn to use PS1 variables, ANSI escape codes, and cursor positioning to create a personalized command line experience."
keywords: [Programming, Linux bash prompt, Customizing bash prompt, PS1 environment variable, ANSI escape codes, Bash prompt commands, Bash prompt colors, Bash special characters, Prompt cursor movement, .bashrc prompt settings, Terminal customization, Customizing Linux command line prompt, Adding date and time to bash prompt, Colorizing bash prompt with ANSI codes, Bash prompt cursor positioning escape sequences, Creating a personalized Linux terminal experience]
draft: TRUE
---

# Introduction
Expand All @@ -17,7 +16,7 @@ The command line is an essential part of working with Linux, and the bash prompt

The appearance of your bash prompt is controlled by an environment variable called PS1 (short for "prompt string one"). By default, it usually contains information like your username, hostname, and current working directory. To see what your PS1 variable looks like, use the echo command:

```bash
``` bash
echo $PS1
```

Expand All @@ -27,39 +26,39 @@ The output will likely include a combination of plain text characters and specia

Before we dive into customizing the prompt, it's a good idea to backup your existing prompt string. You can do this by copying the value of PS1 into a new variable:

```bash
``` bash
ps1_old="$PS1"
```

Now, let's experiment with a few different prompt designs. For example, you can try an empty prompt:

```bash
``` bash
PS1=""
```

Or a minimal prompt with just a dollar sign:

```bash
``` bash
PS1="\$ "
```

You can even add a bell sound to your prompt:

```bash
``` bash
PS1="\[\a\]\$ "
```

Note the use of \[ and \] to wrap non-printing characters like \a. This helps bash correctly calculate the width of the prompt.

For a more informative prompt, try including the time and hostname:

```bash
``` bash
PS1="\A \h \$ "
```

And here's a variation that resembles the default prompt:

```bash
``` bash
PS1="<\u@\h \W>\$ "
```

Expand All @@ -71,54 +70,54 @@ Modern terminal emulators support color through the use of ANSI escape codes. Th

To set the text color, use the following format:

```bash
``` bash
\033[X;YYm
```
Where X is the character attribute (like bold) and YY is the color code. For example, to make the prompt text red, use:
```bash
``` bash
PS1="\[\033[0;31m\]<\u@\h \W>\$ "
```
But now everything you type after the prompt is also red! To fix this, add another escape code at the end to reset the color:
```bash
``` bash
PS1="\[\033[0;31m\]<\u@\h \W>\$\[\033[0m\] "
```
You can also change the background color using codes like \033[0;41m for red.
You can also change the background color using codes like \\033\[0;41m for red.
# Positioning the Cursor and Displaying Information
ANSI escape codes also allow you to move the cursor around the terminal screen. This is handy for displaying information in a different location, like a clock in the upper corner.
Here's an example prompt that draws a red bar at the top of the screen with a yellow clock:
```bash
``` bash
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "
```
Let's break down what each part does:
| Sequence | Action |
|----------|--------|
| \[ | Begin non-printing characters |
| \033[s | Save cursor position |
| \033[0;0H | Move cursor to upper left corner |
| \033[0;41m | Set background color to red |
| \033[K | Clear line from cursor to end |
| \033[1;33m | Set text color to yellow |
| \t | Display current time |
| \033[0m | Reset color |
| \033[u | Restore cursor position |
| \] | End non-printing characters |
| Sequence | Action |
|----------------------------|----------------------------------|
| \[ | Begin non-printing characters |
| \\033\[s | Save cursor position |
| \\033\[0;0H | Move cursor to upper left corner |
| \\033\[0;41m | Set background color to red |
| \\033\[K | Clear line from cursor to end |
| \\033\[1;33m | Set text color to yellow |
| \t \| Display current time | |
| \\033\[0m | Reset color |
| \\033\[u | Restore cursor position |
| \] | End non-printing characters |
# Making Your Custom Prompt Permanent
To make your prompt customizations stick, add them to your .bashrc file. Open the file in a text editor and insert these lines:
```bash
``` bash
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$ "
export PS1
```
Expand All @@ -131,65 +130,60 @@ Once you've mastered the basics, you can take your prompt to the next level with
When crafting your perfect prompt, keep these tips in mind:
- Be mindful of prompt length, especially if you often work in deep directory structures.
- Avoid using expensive operations that could slow down the prompt's rendering.
- Use colors judiciously to enhance rather than distract.
- Test your prompt in different terminal emulators to ensure compatibility.
- Be mindful of prompt length, especially if you often work in deep directory structures.
- Avoid using expensive operations that could slow down the prompt's rendering.
- Use colors judiciously to enhance rather than distract.
- Test your prompt in different terminal emulators to ensure compatibility.
# Troubleshooting Common Issues
If your prompt customization isn't working as expected, check for these common pitfalls:
- Forgetting to wrap non-printing sequences in \[ and \].
- Using an incompatible or incorrectly formatted ANSI escape code.
- Exceeding the maximum prompt length, causing wrapping or overlap.
- Forgetting to wrap non-printing sequences in \[ and \].
- Using an incompatible or incorrectly formatted ANSI escape code.
- Exceeding the maximum prompt length, causing wrapping or overlap.
When in doubt, consult the comprehensive Bash Prompt HOWTO or try a web-based prompt generator to get back on track.
# Quick Takeaways
- The bash prompt is highly customizable using the PS1 variable.
- Special backslash-escaped characters represent prompt elements like username, hostname, and time.
- ANSI escape codes enable color and cursor movement control.
- Thoughtful prompt customization can boost productivity and visual appeal.
- Permanent changes are made by editing the .bashrc file.
- The bash prompt is highly customizable using the PS1 variable.
- Special backslash-escaped characters represent prompt elements like username, hostname, and time.
- ANSI escape codes enable color and cursor movement control.
- Thoughtful prompt customization can boost productivity and visual appeal.
- Permanent changes are made by editing the .bashrc file.
# Conclusion
Customizing your bash prompt is a fun way to personalize your Linux terminal experience while learning valuable skills. By mastering prompt crafting, you'll gain a deeper understanding of how the shell works and be able to tailor it to your unique workflow. So go ahead and experiment – and don't forget to share your custom creations with the community!
# Frequently Asked Questions
1. **Can I use these prompt customization techniques on other shells like Zsh?**
Yes, most of these concepts translate well to other shells, though the exact syntax and feature set may differ. Consult your shell's documentation for specifics.
1. **Can I use these prompt customization techniques on other shells like Zsh?** Yes, most of these concepts translate well to other shells, though the exact syntax and feature set may differ. Consult your shell's documentation for specifics.
2. **Are there any tools or websites that help generate custom prompt strings?**
Absolutely! Search for "bash prompt generator" to find web-based tools that provide a GUI for building your prompt. Some even include presets for popular styles.
2. **Are there any tools or websites that help generate custom prompt strings?** Absolutely! Search for "bash prompt generator" to find web-based tools that provide a GUI for building your prompt. Some even include presets for popular styles.
3. **How can I display the current Git branch in my prompt?**
You'll need to write a shell function that calls git commands to extract the branch name, then include that function in your PS1 string. The Bash Prompt HOWTO has detailed examples of this technique.
3. **How can I display the current Git branch in my prompt?** You'll need to write a shell function that calls git commands to extract the branch name, then include that function in your PS1 string. The Bash Prompt HOWTO has detailed examples of this technique.
4. **Is it possible to have different prompts for different directories?**
Yes, you can use a shell script in your prompt to check the current directory and conditionally set PS1 to different values. This is handy for color-coding directories or highlighting when you're in version-controlled projects.
4. **Is it possible to have different prompts for different directories?** Yes, you can use a shell script in your prompt to check the current directory and conditionally set PS1 to different values. This is handy for color-coding directories or highlighting when you're in version-controlled projects.
5. **Can I use emojis or special symbols in my bash prompt?**
Modern terminal emulators do support Unicode characters, so you can include emojis and other symbols in your prompt. However, be aware that not all fonts render these characters consistently, so test thoroughly before committing to an emoji-based design.
5. **Can I use emojis or special symbols in my bash prompt?** Modern terminal emulators do support Unicode characters, so you can include emojis and other symbols in your prompt. However, be aware that not all fonts render these characters consistently, so test thoroughly before committing to an emoji-based design.
# Your Turn!
Now that you've learned the basics of customizing your bash prompt, it's time to put your skills to the test. Try creating a prompt that displays the following:
- Your username in green
- The @ symbol in white
- Your hostname in magenta
- The current working directory in blue
- A dollar sign ($) in red
- Your username in green
- The \@ symbol in white
- Your hostname in magenta
- The current working directory in blue
- A dollar sign (\$) in red
Hint: Use the \u, \h, \W, and \$ special characters along with the appropriate ANSI color codes. Don't forget to wrap the non-printing characters in \[ and \].
Solution:
```bash
``` bash
PS1="\[\033[0;32m\]\u\[\033[0;37m\]@\[\033[0;35m\]\h \[\033[0;34m\]\W\[\033[0;31m\]\$\[\033[0m\] "
```
Expand All @@ -201,16 +195,16 @@ Now that you're equipped to customize your bash prompt, I want to see what you c
# References
- Bash Prompt HOWTO: [http://tldp.org/HOWTO/Bash-Prompt-HOWTO/](http://tldp.org/HOWTO/Bash-Prompt-HOWTO/)
- Wikipedia - ANSI escape code: [https://en.wikipedia.org/wiki/ANSI_escape_code](https://en.wikipedia.org/wiki/ANSI_escape_code)
- Bash Reference Manual: [https://www.gnu.org/software/bash/manual/](https://www.gnu.org/software/bash/manual/)
- Bash Prompt HOWTO: <http://tldp.org/HOWTO/Bash-Prompt-HOWTO/>
- Wikipedia - ANSI escape code: <https://en.wikipedia.org/wiki/ANSI_escape_code>
- Bash Reference Manual: <https://www.gnu.org/software/bash/manual/>
I hope this guide has inspired you to take control of your bash prompt and make it your own. Happy customizing!
## Some Escape Codes Used In Shell Prompts
| Escape Code | Meaning |
|-------------|---------|
|------------------------------------------|------------------------------|
| `\a` | ASCII bell. This makes the computer beep when it is encountered. |
| `\d` | Current date in day, month, date format. For example, "Mon May 26." |
| `\h` | Hostname of the local machine minus the trailing domain name. |
Expand All @@ -231,44 +225,44 @@ I hope this guide has inspired you to take control of your bash prompt and make
| `\W` | Last part of the current working directory name. |
| `\!` | History number of the current command. |
| `\#` | Number of commands entered during this shell session. |
| `\$` | This displays a "$" character unless you have superuser privileges. In that case, it displays a "#" instead. |
| `\$` | This displays a "\$" character unless you have superuser privileges. In that case, it displays a "\#" instead. |
| `\[` | Signals the start of a series of one or more non-printing characters. This is used to embed non-printing control characters which manipulate the terminal emulator in some way, such as moving the cursor or changing text colors. |
| `\]` | Signals the end of a non-printing character sequence. |
## Colors!
Here is a markdown table of the escape sequences used to set text colors in shell prompts:
| Sequence | Text Color | Sequence | Text Color |
|----------|------------|----------|------------|
| `\033[0;30m` | Black | `\033[1;30m` | Dark Gray |
| `\033[0;31m` | Red | `\033[1;31m` | Light Red |
| `\033[0;32m` | Green | `\033[1;32m` | Light Green |
| `\033[0;33m` | Brown | `\033[1;33m` | Yellow |
| `\033[0;34m` | Blue | `\033[1;34m` | Light Blue |
| `\033[0;35m` | Purple | `\033[1;35m` | Light Purple |
| `\033[0;36m` | Cyan | `\033[1;36m` | Light Cyan |
| `\033[0;37m` | Light Grey | `\033[1;37m` | White |
| Sequence | Text Color | Sequence | Text Color |
|--------------|------------|--------------|--------------|
| `\033[0;30m` | Black | `\033[1;30m` | Dark Gray |
| `\033[0;31m` | Red | `\033[1;31m` | Light Red |
| `\033[0;32m` | Green | `\033[1;32m` | Light Green |
| `\033[0;33m` | Brown | `\033[1;33m` | Yellow |
| `\033[0;34m` | Blue | `\033[1;34m` | Light Blue |
| `\033[0;35m` | Purple | `\033[1;35m` | Light Purple |
| `\033[0;36m` | Cyan | `\033[1;36m` | Light Cyan |
| `\033[0;37m` | Light Grey | `\033[1;37m` | White |
And here is a table of the escape sequences used to set background colors in shell prompts:
| Sequence | Background Color |
|----------|------------------|
| `\033[0;40m` | Black |
| `\033[0;41m` | Red |
| `\033[0;42m` | Green |
| `\033[0;43m` | Brown |
| `\033[0;44m` | Blue |
| `\033[0;45m` | Purple |
| `\033[0;46m` | Cyan |
| `\033[0;47m` | Light Grey |
| Sequence | Background Color |
|--------------|------------------|
| `\033[0;40m` | Black |
| `\033[0;41m` | Red |
| `\033[0;42m` | Green |
| `\033[0;43m` | Brown |
| `\033[0;44m` | Blue |
| `\033[0;45m` | Purple |
| `\033[0;46m` | Cyan |
| `\033[0;47m` | Light Grey |
## Movement
Here are some escape codes that can be used to move the cursor around the terminal window:
| Escape Code | Action |
|-------------|--------|
|--------------------------------------------|----------------------------|
| `\033[l;cH` | Move the cursor to line l and column c |
| `\033[nA` | Move the cursor up n lines |
| `\033[nB` | Move the cursor down n lines |
Expand All @@ -283,6 +277,8 @@ Here are some escape codes that can be used to move the cursor around the termin
Happy Coding! 🚀
![Color Your Terminal](todays_post.png)
------------------------------------------------------------------------
*You can connect with me at any one of the below*:
Expand Down
Binary file added posts/2024-11-29/todays_post.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions site_libs/quarto-search/autocomplete.umd.js

This file was deleted.

9 changes: 0 additions & 9 deletions site_libs/quarto-search/fuse.min.js

This file was deleted.

Loading

0 comments on commit 96d6f64

Please sign in to comment.