Custom shell prompt tips and tricks?

3 years ago by wolf to c/linux

Recently I stumbled over an article, about how to customize your shell prompt. What really surprised me, is that it lacked one of the most basic tips I learned nearly 20 years back: Always display a timestamp in the prompt, to be able to check how long a process is running or when it ended. (Don't need it daily, but every so often it saves my butt. ;-)) The other trick is to always have a colorful prompt, to easily discern where output from programs start/stop. In total my PS1 looks like this (with GIT status at the end): [\e[32m]\u[\e[m]@[\e[35m]\h[\e[m] [\e[36m]\A[\e[m] [\e[37m][[\e[m][\e[31m]\w[\e[m][\e[37m]][\e[m]$(__git_ps1 "(%s)")$

My question is, what customization, tips and tricks do you have for the shell prompt?

wmassingham 7 points 3 years ago

I make it green for an ssh session, and red when I'm root. That's it, nothing fancy.

path: 0 5187427, hotness: undefined, score: 7, children: 2
wolf 3 points 3 years ago

Damn it! That is such an obvious great idea, I feel like an idiot! Thank you very much! :-)

Any advice/guide how to change the color for ssh sessions?

path: 0 5187427 5187986, hotness: undefined, score: 3, children: 1
nfultz 4 points 3 years ago

Check for the ssh env vars. For example, I use PS1="${SSH_CONNECTION:+\u@\h:}\W\$" to hide the hostname when not on SSH, you could do something similar with the control codes for color.

path: 0 5187427 5187986 5195449, hotness: undefined, score: 4, children: 0
Knusper 5 points 3 years ago path: 0 5202049, hotness: undefined, score: 5, children: 1
NoisyFlake 2 points 3 years ago path: 0 5202049 5206223, hotness: undefined, score: 2, children: 0
multicorn 4 points 3 years ago

If you like customizing your shell, there are really cool things one can do with zsh.

I have mine set up with suggestions to complete the name of the program, or even command line options for it.

path: 0 5187822, hotness: undefined, score: 4, children: 1
wolf 4 points 3 years ago

zsh ... it is totally awesome, I saw a lot of crazy autocomplete stuff by people using it. I stick to bash mostly because it is simply installed everywhere and good enough for my needs. (With some help like autojump for bash.)

path: 0 5187822 5187928, hotness: undefined, score: 4, children: 0
vojel 3 points 3 years ago

I got different colors for Kubernetes clusters. Like green for testing cluster, yellow for development and red for production. Always taking a Quick Look before I do something

path: 0 5195141, hotness: undefined, score: 3, children: 2
Kobaltauge 1 point 3 years ago

This sounds awesome. Do you change the color based on the selected cluster? Do you set an environment variable?

path: 0 5195141 5326068, hotness: undefined, score: 1, children: 1
vojel 2 points 3 years ago

Yes this works with powerlevel10k theme for oh my zsh.

path: 0 5195141 5326068 5438352, hotness: undefined, score: 2, children: 0
ProtonBadger 3 points 3 years ago

Most prompt customizers have an option for showing how long last command ran and whether it succeeded/failed or simply prompt timestamp, it's often default. I use Tide, there's also Starship and a number of others. You can also roll your own ofcourse.

path: 0 5190107, hotness: undefined, score: 3, children: 0
12510198 2 points 3 years ago

A person in this thread already recommended having different colors for different conditions like ssh and running as root, I havent seen anyone mention this specifically but you can determine if the current working directory is writable with something like [ -w "$(pwd)" ] and set the color to red or print a symbol if it doesnt return true.

Also I recommend putting all the code and logic for your shell prompt in a shell function, and using a substitution shell to put it into the PS1 variable like this:

__shellprompt ()
{
    if [ "$(id -u)" = 0 ]; then
        local PROMPT_EMBLEM='#'
    else
        local PROMPT_EMBLEM='$'
    fi
    printf "%s" "$(whoami)@$(uname -n):$(pwd)"
    printf "\n%c " "$PROMPT_EMBLEM"
}
PS1='$(__shellprompt)'

Now this is just a really barebones example, there is a whole lot more you can do like passing in the last exit code through the argv of your shellprompt function like this PS1='$(__shellprompt $?)' and like print it out if its non-zero so you wont have to like echo $? to see if the last command failed, but you should be able to still do this. In my testing, running the shell prompt function in the subsitiution shell didnt effect the $? variable.

In my first comment on another thread about shell prompts, I posted my full shellprompt, it is slightly outdated (I just changed hostname to uname -n), if you cant find it feel free to send a message or just ask, and I will send you the code.

path: 0 5209916, hotness: undefined, score: 2, children: 3
wolf 2 points 3 years ago

Wow, thank you very much! :-)

This example is very enlightening. I was kind of aware that one could run shell functions and even use a GIT function in my prompt, but I never thought it through and your example brings the point home.

I'll waste most probably a few hours to find my perfect prompt function!

(Mandatory xkcd link Nerd Sniping)

path: 0 5209916 5244819, hotness: undefined, score: 2, children: 2
12510198 2 points 3 years ago

Im glad I was able to help!

Something that should be noted when adding colors to your shell prompt function is adding the non printable characters that keep the terminal from buggin out, this caused me a massive headache until I figured it out. When putting it in the PS1 variable directly you will put \[ to begin a color sequence and \] to end one, but printf will print a literal [ and ] so instead you will have to use \001 to start and \002 to end, I also recommend changing \e to \033 or \x1B to make things a bit more portable. For a quick example \[\e[1;31m\] would become \001\033[1;31m\002. Without these characters the terminal will like glitch out when you type a long command and then go back to the front.

If you are like messing around or trying to learn a new programming language, you can try like porting your shell prompt to that language, Ive ported mine to C and set it using the same subsitution shell method, I thought it was a fun lil challenge.

But anyways, I hope you have fun customizing your shell prompt!

path: 0 5209916 5244819 5253009, hotness: undefined, score: 2, children: 1
wolf 2 points 3 years ago

Thanks, of course the color escapes are the first thing I ran into yesterday, when I played around with prompt functions. ;-)

Porting your prompt command to another language is a very nice and practical little project, perhaps I will give it a go with Go. (Pun intended ;-))

Have a great week!

path: 0 5209916 5244819 5253009 5259122, hotness: undefined, score: 2, children: 0
nold 2 points 3 years ago

Powerline!

path: 0 5201255, hotness: undefined, score: 2, children: 0
agent_flounder 2 points 3 years ago

I customized mine to show git branch when in a git project directory.

path: 0 5193155, hotness: undefined, score: 2, children: 0
GustavoM 2 points 3 years ago

Use PS1="▌\t▐\n\w→" to display your local time each time you press enter. And make aliases of lengthy commands such as alias internettest="curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -" (You need to install python to run this.)

path: 0 5194136, hotness: undefined, score: 2, children: 0
Presi300 1 point 3 years ago
path: 0 5327298, hotness: undefined, score: 1, children: 0
linux
linux

@lemmy.ml

login for more options
68145
9471
5816

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

  • Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
  • No misinformation
  • No NSFW content
  • No hate speech, bigotry, etc

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

go to feed...