How to customize your Linux Terminal Prompt for a better workflow

Configurare noua (How To)

Situatie

Solutie

How to Customize Your Bash Prompt

For this article, we’re going to focus on customizing the Bourne-Again Shell, better-known as Bash. While some of the approaches here are similar to what you would do for other shells, they typically use somewhat different syntax, so your results will vary. Make sure to consult the appropriate man pages or documentation if you’re using another shell.Customizing your terminal involves setting the PS1 environment variable. You can set this temporarily inside your terminal, simply by typing something similar to the following:

PS1=”Your wish is my command: “

Of course, this isn’t exactly useful, so you probably want your terminal to give you some proper information. This is where you’ll need to do some thinking about what information you want to display, and how you want to format it.

We’ve got a list of essential special characters to know for Bash, which should help you get started. For more in-depth information, you’ll need to consult the prompt section of the Bash manual.

As an example, try something like the following:

PS1=”\u@\H \W: “

In the above example, \u prints your username, while \H prints your computer’s hostname and \W shows the working directory. This isn’t more information that you’ll find in your typical Bash prompt, but it shows how easy it can be to format information in your prompt.

Colors and Special Characters

Adding colors to the Bash prompt isn’t as simple as it could be, unfortunately. You’ll need to take some already unwieldy ANSI color codes and slightly transpose them to format correctly in the prompt. To make things somewhat easier, here’s a list of commonly used colors:

  • Black: \033[30m
  • Red: \033[31m
  • Green: \033[32m
  • Yellow: \033[33m
  • Blue: \033[34m
  • Purple: \033[35m
  • Cyan: \033[36m
  • White: \033[37m

To colorize the above output, you’d use something like this:

PS1=”\033[31m\u@\033[34m\H \033[36m\W: “

The output is on the garish side, and may be tough to read depending on your background, but it works as an example of how to customize your colors. You can use similar ANSI escape characters to print special characters to the terminal.

Making Your Prompt Permanent

After testing your prompt, it’s simple to keep your changes once you’ve settled on something you like. Simply add the prompt to the .bashrc file in your home directory. For example, to add the above colorized prompt to your Bash prompt, add the following to ~/.bashrc:

export PS1=”\033[31m\u@\033[34m\H \033[36m\W: “

Of course, there are also easier ways to customize your prompt.

Shortcuts to a Shinier Prompt

Okay, so by now you’re able to get started with some basic customizations, but what about the over-the-top configurations you’ve seen all over the internet? These are essentially just the same techniques we’ve used so far, but using special fonts known as Nerd Fonts that use Unicode characters to display icons.While you could start using these yourself, you could also use an existing shell framework. One of the more popular examples is Oh-My-Zsh, which, as the name implies, is only available for the Z shell, also known as zsh.

Since we’ve been focusing on Bash for this guide, this won’t work for us.Fortunately, there are other options. Of of the best options at the time of this writing is Starship, which is a customizable prompt for any shell, including Bash. It’s written in rust for speed, and the instructions for getting started are simple. It’s even included in the repositories for certain Linux distributions like Arch, Void, and openSUSE.

Another popular option is Oh My Posh. Chances are, if there is something you wish you could do with Starship, you can with Oh My Posh. The downside is that this is a much deeper dive when it comes to learning the ins and outs of the system. That said, if you really want to get into tweaking your prompt, this offers the most flexibility.

What About Other Shells?

While we’re mainly focusing on the Bash prompt for this article, it’s not your only choice for the Linux terminal. For example, on macOS, Zsh has been the default shell for several major versions at this point. It has a few features that make it worth a look over Bash as well, depending on what you’re looking for.To start, Zsh offers tab-completion. This is more powerful than the autocompletion you get with Bash, and it can be especially handy if you don’t remember your commands that well. Zsh also has a few features that can make customizing your prompt simpler without using a framework.Another option is the Friendly Interactive Shell, also known as Fish. This makes both Bash and Zsh look positively spartan out of the box, as it’s absolutely feature packed right after you install it. This shell has a bunch of great features, but its main claim to fame is its auto-suggestions.

Unlike tab completion, Fish will start automatically populating commands you type with common options. If there is a certain file you open or a certain directory you move to frequently, you’ll see this suggested as soon as you start to type cd, for example. To ignore the suggestion, just keep typing the command you were going to type.This feature gets more useful as you use the shell more. While you can it to Zsh via plugins, it’s built directly into fish.To try out other shells, take a look at our guide to changing your shell with chsh.

Tip solutie

Permanent

Voteaza

(2 din 6 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?