Custom shell prompt tips and tricks?

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?

Presi300, (edited )
@Presi300@lemmy.world avatar

Step 1. Install fish shell

Step 2. Type fish_config

Step 3. Profit

12510198,

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:


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

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.

wolf,

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)

12510198,

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 `

wolf,

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!

Knusper, (edited )

I can recommend Starship.

NoisyFlake,

Definitely! Much more user-friendly and expandable than configuring PS1 manually.

This is what mine looks like.

nold,
@nold@lemmy.ml avatar

Powerline!

vojel,
@vojel@feddit.de avatar

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

Kobaltauge,

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

vojel,
@vojel@feddit.de avatar

Yes this works with powerlevel10k theme for oh my zsh.

GustavoM, (edited )
@GustavoM@lemmy.world avatar

Use PS1=“▌t▐nw→” 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.)

agent_flounder,
@agent_flounder@lemmy.world avatar

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

ProtonBadger, (edited )

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.

multicorn,

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.

wolf,

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.)

wmassingham,

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

wolf,

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?

nfultz,

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.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #