Thursday, February 26, 2015

First Step In Z Shell (AKA “Give Me a Prompt or Give Me bash”)

published by Suby

Ironic, isn’t it, that for the prompt being maybe the coolest and flashiest thing that you always hear about Z shell (zsh), when you first switch from bash you get this:

Scrooge-and-Marley%

So it’s giving us the computer name (or hostname), and then uses a % as delimiter before our input begins (where bash uses a $). This, by itself and with nothing else to recommend it to our collective impressionability, is not exactly what we in the geosciences* call “earth-shattering stuff.”

This is more like what I normally saw as the prompt (on OS X) when I was on bash:

Scrooge-and-Marley:bookshelf ebenezer$

Let’s start simple and just try to replicate that on zsh for now.

Ye Olde prompt string


The relevant setting (or “parameter”), in both bash and zsh, is PS1 (zsh also allows PROMPT). Both shells have a nice array of special characters which we can use, escaped, to represent various bits of info which we want in our command prompt (the prompt string). My old OS X bash prompt used three, which are listed under PROMPTING in the man page for bash:

h    the hostname up to the first `.'
W    the basename of the current working directory,
     with $HOME abbreviated with a tilde
u    the username of the current user

In bash, these are escaped with a \, so this would have been the value of PS1 there:

\h:\W \u\$

Or, quoted as we could set it with export:

export PS1='\h:\W \u\$ '

So now to make that, but in zsh.

The new prompt string (New Look, Same Different Taste)


zsh has a huge manual, which (as it explains in man zsh—that acts as a sort of index) is broken out into different sections. The relevant one here is:

man zshparam

…except that it turns out it’s not. Well, it sort of is. Here’s what it says for PS1:

       PS1 <S>
              The primary prompt string, printed before a command is read.  It undergoes a special form of expansion before being displayed; see EXPANSION OF PROMPT SEQUENCES in  zsh-misc(1).  The default is `%m%# '.


So, to man zshmisc!

Under SIMPLE PROMPT ESCAPES, we have a list: the special characters which stand for informational bits in our prompt string. These are the three which correspond to our bash special characters:

       %m     The hostname up to the first `.'.  An integer may follow the `%' to specify how many components of the hostname are desired.  With a negative  integer,  trailing  components of the hostname are shown.

       %d
       /      Current working directory.  If an integer follows the `%', it specifies a number of trailing components of the current working directory to show; zero  means  the  whole path.  A negative integer specifies leading components, i.e. %-1d specifies the first component.


       %n     $USERNAME.

If you do not yet have a .zshrc, create one. This can be its first line:

PS1='%m:%1d %n$ '

We can see the % for escaping rather than \ (as bash uses), of course. Other than that, we have the hostname (%m), a literal colon (:), the first item in our current working directory path (%1d), a space, and then our username (%n) and a literal dollar sign ($).

The %d confused me for a bit (I am not a Nobel-winning rocket scientist). Using the example in the man page, I tried something like

PS1='%m:%-1d %n$ '

which gives us something like:

Scrooge-and-Marley:/Users ebenezer$

Yes, the first item is indeed the first. (Who would have thought? A Nobel-winning rocket scientist would have, for one.)

So there you have a familiar (if you’re a fellow switcher from bash) prompt. Obviously we’ve not really taken over the world here—this is just about (if you’re also on OS X) getting back to what you were used to—but we now know how to modify prompt strings, or at least one of them, in zsh. Stay tuned for more exciting updates in this space…

(I stole the title of this post from that of the excellent book by James Ward and Bruce Eckel, First Steps in Flex.)

* I am not in the geosciences.

6 comments:

  1. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.
    Java Training in Chennai
    .Net Training in Chennai
    PHP Training in Chennai

    ReplyDelete
  2. The expansion of internet and intelligence in business process lead the way to huge volume of data. It is important to maintain and process these data to be efficient in data handling. Hadoop Training in Chennai | Big Data Training in Chennai

    ReplyDelete
  3. Android 6 Weeks Summer Training In Delhi With Live Projects | 9510 860 860

    4/6 Weeks Summer Training in Delhi is going on by KVCH Delhi. If you are willing to start your career with Android Development, KVCH helps to strengthen your knowledge and assures 100% placement assistance in top MNCs.
    For more visit-https://kvch.in/delhi/android-live-project-based-6-weeks-training-delhi.html
    Or Contact: 09510 860 860

    ReplyDelete
  4. CIIT Noida provides Best MCA Courses in Noida based on the current

    IT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad.

    CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced

    level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.

    At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live

    projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to

    students that transform an average student into a corporate professional whom companies prefer to hire.

    Best MCA College in Noida

    ReplyDelete