One of my favorite Mac OS X Terminal tips involves adding the last running process into the terminal title using the following code in your .bashrc.
trap 'printf "\033]0; `history 1 | cut -b8-` \007"' DEBUG
Now I'm looking to customize my Bash prompt from the default into something more functional. What are some useful shell prompt examples?
I have a script
~/bin/setprompt
that is executed by my.bashrc
, which contains:This script sets the prompt to the host name followed by
:)
if the last command was successful and:(
if the last command failed.I use a simple two-liner:
or
if root. People look at me like I'm crazy for using a two-line prompt, but I hate only seeing the last part of the current path, and I work with paths that are too long to put the whole thing on the same line as the input area.
This, however, is the most awesomest one I've ever seen:
Give it a shot. It gives good information, but it will actually cause the load on your boxes to increase just by hitting enter at a prompt!
I'm a fan of the default Cygwin bash prompt:
or without colors:
looks like:
Here's mine (scroll to the right to see more interesting bit):
Here's the result:
When I'm inside a Git repository:
I choose to use multiple line breaks and the "horizontal ruler" to make it easier to scan buffers, and so long path names don't push the cursor too far to the right.
I like including the time in mine. Gives me sort of a built in
time
around my commands:Though after seeing a few of these, it makes me want to make mine a little smarter!
Similar to chaos's answer, I kind of like
which gives me
By including the user/host on the first line, I can easily copy that entire path line when I am using SCP, or rsync to send some files to that directory.
In ksh:
In my .profile, I set the following var (so this var is only set once per session ever, no matter how many 'su's I do:
In my .kshrc I have:
I become root with the following alias, rather than "su" (kroot is a root user with a ksh shell instead of sh (solaris) or bash (linux):
I have also defined these two functions:
and finally at the end of .kshrc I have:
All of this gives me by default automatic setting of the end of my prompt to indicate whether or not I am root (and in turn if I have a parent that was root), current working directory, hostname, and exit status of last process. sprompt drops the hostname, shrinks the path down to only the basename, and removes spaces:
The root/me aliases are bourne from pre-sudo administration on Solaris and old habits have died hard. I doubt anyone would implement the exact same thing these days. The prompt-changing functions and the exist status continue to be very valuable to me.
I have a prompt that is simple, but uses colors to give some extra info:
The color of the hostname (drewble, in this case) is different on each machine - when I get a new machine that I work on, I choose an arbitrary color for that machine. That way, when I have a lot of terminals open, it's easy to tell which machine they're SSHed into and reduces mistakes I make by being on the wrong machine. Additionally, when I do
sudo -s
, I put the hostname in all caps and color the colon red, again, this reduces mistakes by making it clear you're acting as root. The path (my home directory,~
in this case) is in bright green, which helps separate lines of output; if something creates a lot of output, it's easy to scroll up and see where the output started.It isn't a shell prompt, but it is a customization that might fit the spirit of what you are asking.
I set the following environment variable:
This way, if I have multiple terminals open via screen or whatever, I won't lose history when they clobber each others changes.