In an earlier post, we discussed that a shell can be used for
static content generation, due to its programmability. But why did I even start
using tcsh
as my shell in the first place? Isn’t bash
the standard in the
Linux world?
Most of us start using Linux with the default shell, which in most
distributions is the ubiquitous bash
.
I prefer a single-line prompt; I also prefer to have a relatively same-sized
prompt rather than a prompt that jumps around wildly in length. Yet, I also
want to know where I am in the directory tree before I run a command, without
having to regularly run pwd
as a sanity check.
This set of conflicting requirements presents a bit of a dilemma:
- I want to know where I am in the directory tree
- showing the full path in the prompt will bounce its length significantly
- showing just the last directory name is insufficient and confusing (or disastrous, depending on the command you might run)
As a middle-ground solution, we can print the last N elements of the directory tree, but it may still be a fair number of characters, and hence too long.
What if we could print that on the right side of the prompt, instead of the usual left?
The bash solution
Well, bash
supports printing right-aligned text, e.g., see this example
from ArchLinux:
rightprompt()
{
printf "%*s" $COLUMNS "right prompt"
}
PS1='\[$(tput sc; rightprompt; tput rc)\]left prompt > '
which just gets us right-aligned text, but typing text next to it just makes it into a bit of a mess, as the shell doesn’t recognize the existence of the prompt and that it conflicts with your command, which leads to super confusing editing of the command you’re about to run.
Here’s what it looks like in bash
as the length of your command-line grows:
left prompt > right prompt
left prompt > if you keep typing a really, right prompt
left prompt > if you keep typing a really, really, right prompt
left prompt > if you keep typing a really, really, really long command, right prompt
left prompt > if you keep typing a really, really, really long command, it overwritesght prompt
That said, if you overwrite even a part of the right-side prompt, and then delete it with Backspace, the right-side prompt entirely disappears (and doesn’t return), but it’s a bit of a kludge, and something you’ll always have to keep in mind.
The tcsh solution
On the other hand, this is a built-in feature of tcsh
(no hacks or padding needed):
set prompt='%n@%m> '
set rprompt='%c5'
and you have a prompt that looks like the following:
user@hostname> ~/git/repo
Now you have a fixed-width shape of the left-side prompt, and the right side
will display up to 5 directories from your path (collapsing your home directory
to simply ~
for compactness), and best of all: when your command length is
long enough to overwrite the right-side prompt, tcsh
automatically hides the
right-side prompt entirely!
Here’s what it looks like in tcsh
as the length of your command-line grows:
user@hostname> ~/git/repo
user@hostname> if you keep typing a really, ~/git/repo
user@hostname> if you keep typing a really, really, ~/git/repo
user@hostname> if you keep typing a really, really, really long command, ~/git/repo
user@hostname> if you keep typing a really, really, really long command, it disappears
That’s what it means to have first-class support for such features!
Note that this, of course, doesn’t at all stop you from using #!/bin/bash
for
your scripts for interoperability with others, but it’s a great usability
improvement over bash
.
However, using a shell that’s incompatible with bash
syntax, and which is
only popular with a minority of Linux users, you will note that many scripts,
plugins, shell extensions, auto-completion scripts, etc. will no longer work
for you out-of-the-box, so this trade-off may or may not be worthwhile for you.
All I can say is, caveat emptor.
Yet another shell?
In a future post, we’ll look into another shell that may be of interest, that supports not only right-hand prompts, but will bring together Vim and Emacs users in peace and harmony—stay tuned!
Addendum
The future post mentioned above is live!