Your Shell with Zsh and Oh-My-Zsh

OpenSource For You - - FOR U & ME LET'S TRY -

Zshell (zsh) is a pow­er­ful in­ter­ac­tive lo­gin shell and com­mand in­ter­preter for shell script­ing. A big im­prove­ment over older shells, it has a lot of new fea­tures and the support of the Oh-My-Zsh frame­work that makes us­ing the ter­mi­nal fun.

Re­leased in 1990, the zsh shell is fairly new com­pared to its older coun­ter­part, the bash shell. Although more than a decade has passed since its re­lease, it is still very popular among pro­gram­mers and de­vel­op­ers who use the com­man­d­line in­ter­face on a daily ba­sis.

Why zsh is bet­ter than the rest

Most of what is men­tioned be­low can prob­a­bly be im­ple­mented or con­fig­ured in the bash shell as well; how­ever, it is much more pow­er­ful in the zsh shell.

Ad­vanced tab com­ple­tion

Tab com­ple­tion in zsh sup­ports the com­mand line op­tion for the auto com­ple­tion of com­mands. Press­ing the tab key twice en­ables the auto com­plete mode, and you can cy­cle through the op­tions us­ing the tab key.

You can also move through the files in a di­rec­tory with the tab key.

Zsh has tab com­ple­tion for the path of di­rec­to­ries or files in the com­mand line too.

Another great fea­ture is that you can switch paths by us­ing 1 to switch to the pre­vi­ous path, 2 to switch to the ‘pre­vi­ous, pre­vi­ous’ path and so on.

Real time high­light­ing and the­me­able prompts

To in­clude real time high­light­ing, clone the zsh-syn­tax­high­light­ing repos­i­tory from github (https://github.com/zshusers/zsh-syn­tax-high­light­ing). This makes the com­mand- line look stun­ning. In some ter­mi­nals, ex­ist­ing com­mands are high­lighted in green and those typed in­cor­rectly are high­lighted in red. Also, quoted text is high­lighted in yel­low. All this can be con­fig­ured fur­ther ac­cord­ing to your needs.

Prompts on zsh can be cus­tomised to be right-aligned, left-aligned or as multi-lined prompts.

Glob­bing

Wikipedia de­fines glob­bing as fol­lows: “In com­puter pro­gram­ming, in par­tic­u­lar in a UNIX-like en­vi­ron­ment, the term glob­bing is some­times used to re­fer to pat­tern match­ing based on wild­card char­ac­ters.” Shells be­fore zsh also of­fered glob­bing; how­ever, zsh of­fers ex­tended glob­bing. Ex­tra fea­tures can be en­abled if the EXTENDEDGLOB op­tion is set.

Here are some ex­am­ples of the ex­tended glob­bing of­fered by zsh. The ^ character is used to negate any pat­tern fol­low­ing it.

se­topt EXTENDEDGLOB

# En­ables ex­tended glob­bing in zsh.

ls *(.)

# Dis­plays all reg­u­lar files.

ls -d ^*.c

# Dis­plays all di­rec­to­ries and files that are not cpp files.

ls -d ^*.*

# Dis­plays di­rec­to­ries and files that have no ex­ten­sion.

ls ­d ^file

# Dis­plays ev­ery­thing in di­rec­tory ex­cept file called file.

ls -d *.^c

# Dis­plays files with ex­ten­sions ex­cept .c files.

An ex­pres­sion of the form <x-y> matches a range of in­te­gers. Also, files can be grouped in the search pat­tern.

% ls (foo|bar).*

bar.o foo.c foo.o

% ls *.(c|o|pro)

bar.o file.pro foo.c foo.o main.o q.c

To ex­clude a cer­tain file from the search, the ‘~’ character can be used.

% ls *.c

foo.c foob.c bar.c

% ls *.c~bar.c

foo.c foob.c

% ls *.c~f*

bar.c

Th­ese and sev­eral more ex­tended glob­bing fea­tures can help im­mensely while work­ing through large di­rec­to­ries

Case in­sen­si­tive match­ing

Zsh sup­ports pat­tern match­ing that is in­de­pen­dent of whether the let­ters of the al­pha­bet are up­per or lower case. Zsh first surfs through the di­rec­tory to find a match, and if one does not ex­ist, it car­ries out a case in­sen­si­tive search for the file or di­rec­tory.

Shar­ing of com­mand his­tory among run­ning shells

Run­ning shells share com­mand his­tory, thereby erad­i­cat­ing the dif­fi­culty of hav­ing to re­mem­ber the com­mands you typed ear­lier in another shell.

Aliases

Aliases are used to ab­bre­vi­ate com­mands and com­mand op­tions that are used very of­ten or for a com­bi­na­tion of com­mands. Most other shells have aliases but zsh sup­ports global aliases. Th­ese are aliases that are sub­sti­tuted any­where in the line. Global aliases can be used to ab­bre­vi­ate fre­quently-typed user­names, host­names, etc. Here are some ex­am­ples of aliases: alias ­g mr=’rm’

alias ­g TL=’| tail ­10’

alias ­g NUL=”> /dev/null 2>&1”

In­stalling zsh

To in­stall zsh in Ubuntu or De­bian-based dis­tros, type the fol­low­ing: sudo apt­get up­date && sudo apt­get in­stall zsh # in­stall zsh chsh ­s /bin/zsh # to make zsh your de­fault shell

To in­stall it on SUSE-based dis­tros, type: sudo zyp­per in­stall zsh

fin­ger yoda | grep zsh

Con­fig­ur­ing zsh

The .zshrc file looks some­thing like what is shown in Fig­ure 4.

Add your own aliases for com­mands you use fre­quently.

Cus­tomis­ing zsh with Oh-My-Zsh

Oh-My-Zsh is be­lieved to be an open source com­mu­nity-driven frame­work for man­ag­ing the zsh con­fig­u­ra­tion. Although zsh is pow­er­ful in com­par­i­son to other shells, its main at­trac­tion is the themes, plug­ins and other fea­tures that come with it.

To in­stall Oh-My-Zsh you need to clone the Oh-My-Zsh repos­i­tory from Github (https://github.com/rob­byrus­sell/ oh-my-zsh). A wide range of themes are avail­able so there is some­thing for every­body.

To clone the repos­i­tory from Github, use the fol­low­ing com­mand. This in­stalls Oh-My-Zsh in ~/.oh-my-zsh (a hid­den di­rec­tory in your home di­rec­tory). The de­fault path can be changed by set­ting the en­vi­ron­ment vari­able for zsh us­ing ex­port ZSH = /your/path

git clone https://github.com/rob­byrus­sell/oh­my­zsh. git

To in­stall Oh-My-Zsh via curl, type:

curl ­L http://in­stall.ohmyz.sh | sh

To in­stall it via wget, type:

wget —no­check­cer­tifi­cate http://in­stall.ohmyz.sh ­O ­| sh

To cus­tomise zsh, cre­ate a new zsh con­fig­u­ra­tion, i.e., a ~/.zshrc file by copying any of the ex­ist­ing tem­plates pro­vided:

cp ~/.oh­my­zsh/ tem­plates/zshrc.zsh­tem­plate ~/.zshrc

Restart your zsh ter­mi­nal to view the changes.

Plug­ins

To check out the nu­mer­ous plug­ins of­fered in Oh-My-Zsh, you can go to the plug­ins di­rec­tory in ~/.oh-my-zsh.

To en­able th­ese plug­ins, add them to the ~/.zshrc file and then source them. cd ~/.oh­my­zsh

vim ~/.zshrc

source ~/.zshrc

If you want to in­stall some plugin that is not present in the plug­ins di­rec­tory, you can clone the plugin from Github or in­stall it us­ing wget or curl and then source the plugin.

Themes

To view the themes in zsh go to the themes/ di­rec­tory. To change your theme, set ZSH_THEME in ~/.zshrc to the theme you de­sire and then source Oh-My-Zsh. If you do not want any theme en­abled, set ZSH_THEME = “”. If you can’t de­cide on a theme, you can set ZSH_THEME = “ran­dom”. This will change the theme ev­ery time you open a shell and you can de­cide upon the one that you find most suit­able for your needs.

To make your own theme, copy any one of the ex­ist­ing themes from the themes/ di­rec­tory to a new file with a “zshtheme” ex­ten­sion and make your changes to that. A cus­tomised theme is shown in Fig­ure 6. Here, the user name, rep­re­sented by %n, has been set to the colour green and the com­puter name, rep­re­sented by %m, has been set to the colour cyan. This is fol­lowed by the path rep­re­sented by %d. The prompt vari­able then looks like this... PROMPT=’ $fg[green]%n $fg[red]at $fg[cyan]%m­­>$fg[yel­low]%d: ‘

The prompt can be changed to in­cor­po­rate spac­ing, and git states, bat­tery charge, etc, by declar­ing func­tions that do the same.

For ex­am­ple, here, in­stead of print­ing the en­tire path in­clud­ing /home/darshana, we can de­fine a func­tion such that if PWD de­tects $HOME, it re­places the same with “~” func­tion get_pwd() {

echo “${PWD/$HOME/~}”

}

To view the sta­tus of the cur­rent Git repos­i­tory, the fol­low­ing code can be used:

func­tion git_promp­t_info() {

Fig­ure 1: Tab com­ple­tion for com­mand op­tions

Fig­ure 2: Tab com­ple­tion for files

Fig­ure 4: ~/.zshrc file

Fig­ure 5: Set­ting aliases in ~/.zshrc file

Fig­ure 3: View pre­vi­ous paths

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.