Zsh the Bash alternative/enhancement

What do you have and what do you want?

Moderators: b1o, jkerr82508

User avatar
viking60
Über-Berserk
Posts: 9301
Joined: 14 Mar 2010, 16:34

Zsh the Bash alternative/enhancement

Postby viking60 » 19 Jul 2014, 23:15

I found out that Zsh has a lot of dedicated followers that are very enthusiastic about it.

Zsh is a UNIX command interpreter like Bash and other shells.
Zsh also claims to have better scripting but I would never make my scripts for zsh only, because it does not come as a default when you install Linux or Windows for that matter.
It is the default on MAC though so there is quite a big potential.

So why do people replace Bash with Zsh?

I had to find out so I simply installed it from my CLI

Code: Select all

sudo pacman -S zsh

Nothing to it and since manuals are for chicken - I simply typed zsh in my terminal.
That lead me to a first install routine where I picked everything that was recommended basically - and that was it.
Easy! And since I had no clue what to do next I had to do a little reading :oops:
So I found out that I had to do a

Code: Select all

chsh -s $(which zsh)

to switch my user from bash to zsh.
Login out or restarting and loging back in and the bash should be replaced with zsh.
It was pretty obvious since the propmt had changed a bit
You can also do a

Code: Select all

echo $SHELL

To get a confirmation of the obvious.

The important file in zsh is ~/.zshrc which makes sense since the important file is .bashrc for Bash. It was set up for me by that first run prompting so I had a fully functional zsh.

One would think that I would have to read a lot of books and that none of my usual Terminal-habits where usable anymore, but that is not the case. The bash commands do work and I could pretty much go on like before.

And this is the great advantage of Zsh it does not fubar your precious habits but it adds some functionality.
Many of the functions that are highly praised on the internet are equally possible in Bash so it's "fantasticness" is exaggerated.
But one or two nice extras can be added.

Since I always put my aliases in a separate file (called .alias) I added this to
~/.zshrc

Code: Select all

if [ -f ~/.alias ]; then
    . ~/.alias
fi

and restarted the shell with

Code: Select all

source ~/.zshrc

Now all my aliases did work +1 and they do show up when I type alias.

If you have all your aliases in .bashrc then you can simply copy them into .zshrc and they will work like before.

I added some plugins that enables me to write the directory name and jumps to it:
Not cd Pictures but simply Pictures - lazynes is an art - I tell you +1

And I listed the available prompts with

Code: Select all

prompt -l

and picked fade.
You can check out any of the prompts listed by typing

Code: Select all

prompt redhat
replace redhat with the one you like. The Redhat prompt is probably the classical prompt that you had in bash.

So far it was not that different from bash.
Now the thing that impressed me a bit was the way I could autocomplete commands like systemctl or pacman.
I typed

Code: Select all

pacman -

Imagine I am a newbie and know that I have to type "pacman -" but do not know what switches to use. Here i can hit <TAB> and pick the option I need with <TAB> and hit enter.
Image
If I type

Code: Select all

systemctl

and hit TAB I get all 149 commands that can be used with systemctl. Now that is a bit heavy and it works in bash too by hitting TAB twice.
But when I have some idea and write something like this:

Code: Select all

systemctl sta

and hit TAB then I have narrowed it down to two alternatives:
:A

Code: Select all

start   -- Start (activate) one or more units
status  -- Show runtime status of one or more units


This does not work in bash. This zsh function works nicely with yum in Centos, and urpmi too (try "urpmi -" + <TAB>).

So here is my .zshrc you can simply copy it and put it in your ~/ and start zsh as described above and you are good to go without the first install routine (all my examples here are based on this .zshrc).
You may put your aliases in the file ~/.alias but you don't have to.
~/.zshrc

Code: Select all

# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
bindkey -e
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '~/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall
autoload -U promptinit
promptinit
prompt fade
#autoload -U compinit
#compinit
zstyle ':completion:*' menu select
setopt completealiases
setopt HIST_IGNORE_DUPS
DIRSTACKFILE="$HOME/.cache/zsh/dirs"
if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then
  dirstack=( ${(f)"$(< $DIRSTACKFILE)"} )
  [[ -d $dirstack[1] ]] && cd $dirstack[1]
fi
chpwd() {
  print -l $PWD ${(u)dirstack} >$DIRSTACKFILE
}

DIRSTACKSIZE=20

setopt autopushd pushdsilent pushdtohome

## Remove duplicate entries
setopt pushdignoredups
## jumping to directory by writing its name
setopt auto_cd
## This reverts the +/- operators.
setopt pushdminus
#leser alias fra filen .alias
if [ -f ~/.alias ]; then
    . ~/.alias
fi

I also added the dirstack function where you can type

Code: Select all

dirs -v

you get your dir history and jump directly to your directory by typing cd and the number - this can be a time-saver.
Image
Now try to hit <TAB> on

Code: Select all

cd /u/s/i

It will autocomplete every part one at a time until you are in /usr/share/icons or info or whatever you are looking for...
Try it - If you don't like it you can go back to bash.
A slight warning here though :T
Set your shell back to bash before you remove zsh:

Code: Select all

chsh -s /bin/bash berserk

This is how it goes in Arch based distros - make sure you have managed this before you remove zsh from your distro.

Restart your computer before your remove zsh and check that bash is back in your terminal - then you are good to remove zsh.

But since Bash is back why not keep zsh in case you want to play around with it again? In which case you would do a

Code: Select all

chsh -s $(which zsh)
again (and reboot).
...
So is this a "must have" a "need"?
Depending on your needs I am inclined to say no - but it is nice.

I have used this procedure on Centos 6.5, Manjaro and Openmandriva so it should be pretty universal.
On some commands I have gotten the error
~/.cache/zsh/dirs are missing.
That is easily fixed by creating zsh manually:

Code: Select all

cd .cachce

Code: Select all

mkdir zsh
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9301
Joined: 14 Mar 2010, 16:34

Re: Zsh the Bash alternative/enhancement

Postby viking60 » 21 Jul 2014, 01:50

Some additional functions
If you want zsh to autocorrect mistyped commands you can add

Code: Select all

setopt correct

at the bottom of your ~.zshrc file this will auto-correct mistyped commands

Example
~/ sl ~
zsh: correct 'sl' to 'ls' [nyae]?

If you want it to be corrected to ls then type y the a stands for abort and the e stands for edit as in "I typed crap let me edit that" and there are no prizes to guess what the n stands for :-D
ynae is a bit strange though so I like to edit the prompt to make it more meaningfull so I add this to .zshrc

Code: Select all

export SPROMPT="Correct %R to %r? (Yes, No, Abort, Edit) "

So this makes more sense:
:A
Image
Image

It is important that the shell asks you about this since the suggestions may not always be super-smart. The internet is full of questions on how to turn it off (I like it though).

Try it out and decide for yourself .
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9301
Joined: 14 Mar 2010, 16:34

Re: Zsh the Bash alternative/enhancement

Postby viking60 » 09 Jan 2015, 12:30

To get a date and timestamp on your history you can enter

Code: Select all

history -Ed

I have made an alias.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"


Return to “Software”