4 ms·
I use bash, but really like the RPROMPT idea from zsh, so mimick that as best as I can. It turns out that bash has a PROMPT_COMMAND variable which gets called
by xelxebar 8y ago
I use bash, but really like the RPROMPT idea from zsh, so mimick that as best as I can.
It turns out that bash has a PROMPT_COMMAND variable which gets called before writing out PS1, so I use this to write my own rprompt. The main idea is just to use tput to draw a thing on the right and restore the cursor position afterward.
As is, I have something like below right-justified at each prompt:
user@host:pwd (git-branch)
It even degrades nicely if horizontal space starts getting cramped. Actually, the code is (I believe) just posix shell, so I define relevant functions in .profile and have .bashrc and .mkshrc use them as appropriate. Since it's a bit long, here are some pastebins that should be drop-in workable:
- profile: http://ix.io/1yiC/sh http://ix.io/1yiC/sh
- bashrc: http://ix.io/1yiA/bash http://ix.io/1yiA/bash
- mkshrc: http://ix.io/1yiB/ksh http://ix.io/1yiB/ksh
There are a couple caveats. Bash clears the whole line when deleting characters, which causes the rprompt to be cleared as well. On the other hand, mksh works as expected. Also, if you enter really long commands into the prompt, they will eventually bump into and overwrite the rprompt, instead of clearing it wholesale. I've not found this too much of a problem though.
In the code above, the rprompt() function defines the actual contents of the prompt and performs the graceful degredation. The rprompt_draw() function does the actual drawing and prompt_draw() links these two together to be used by bash, mksh, etc.
Feel free to ask my any questions or hate on my code.
- enriquto 8y agoI like to put useful information in the prompt, but yours looks too cluttered for my liking. I particularly dislike the fact that most of the information of your prompt is nearly constant, thus very redundant. Putting the CWD inside the prompt is useless. You can just run pwd yourself if you are lost, but most of the time you already know where you are. Similarly for the username and the hostname. If you only deal with two or three servers, you can color code your prompt symbol to those, and obtain a much shorter and cuter prompt. Useful information that is still not in your prompt: - status of the last run command: $? - number of jobs in the background, if any I have this, with some colors thrown in: PS1='$(if [ \j != "0" ] ; then echo "[\j]"; fi)$?\$ '
- xelxebar 8y agoThat's all quite easy to change in the rprompt() function. I tried hard to factor everything out, so the prompt content specification itself is clear and obvious. Basically, for the reasons you mention, I dislike having that info set in PS1, but I find it considerably less distracting when right-justified. Having it visible, though, makes it easy to ascertain the execution context of commands in the terminal history buffer. The status of last run command I have on my PS1. If it exits 0, then it's just a simple $ otherwise, it's: errno$ I do like your idea of showing current background jobs, but I find myself relying more on tmux windows than background jobs, so I suspect that number would be mostly constant for myself anyway.
- enriquto 8y ago> I do like your idea of showing current background jobs, but I find myself relying more on tmux windows than background jobs, so I suspect that number would be mostly constant for myself anyway. Sure! the idea is that background jobs are very rare, and their existence should be clearly indicated in the prompt (in bright red). Usually, there are zero jobs and the prompt is silent about that.