FUN WITH LINUX

The Rule Of Three (Part2)

25 October 2015

A sysadmin needs his tools. I really need at least 3 tools to be productive:

  • Terminal-Emulator (or screen/tmux if I am working without X)
  • Shell
  • Editor

This is my Rule Of Three. Those 3 tools can make my life much easier. Therefore I have to choose those tools wisely. And of course I have to know them very well, so I can get all the benefits of them. The last time I wrote about rxvt(Terminal-Emulator). In this article I want to introduce one of my “favourite” shells: zsh

I have to say that I like the bash too. It is my rootshell and I am very used to the bash because I am using it for many years. Zsh on the other side is very customizable and has cool scripting-features, that’s why it became my standard usershell.

Customization

What’s really great about zsh is that it is highly customizable. The problem with “highly customizable” is mostly that things start to get complex. Luckily, there are frameworks to make this task more easy. One of them is oh-my-zsh. It’s loaded via your .zshrc and comes with lot’s of plugins for autocompletion and themes. Just try it, it’s great!

Autoload long commands in you editor

If you just typed in a very long command in your shell and you want this command copied in your favourite editor($EDITOR) then just type <CTRL>+X <CTRL>+E

Builtin Pager

zsh has a builtin pager. If you want to read a (text)file just type:

< sometextfile.txt

Most bash-user know “ <STRG>+<r>” for recursively search the history. If you want to search the history for a command you can use the <UP> key. Let’s assume we are looking for one of our last ssh commands, then we can simply type ssh <UP>

Tab completion

Autocomplete cd

One of the cool things in zsh is tab completion. If you type “cd <TAB>” it looks like this:

You can navigate using <TAB> or any <ARROW-KEY> through this menu, or just type the first letter of one directory and press <TAB> again…

Smart completion of a path

You can also shortcut a path. Let’s assume you have a path like this:

arch/openrisc/kernel

Then you can just type:

a/o/k

..hit <TAB> and zsh will autocomplete your command, or ask you if there are more than one directories with the same letter.

Autocomplete commands

Zsh can even autocomplete commands and command-line-arguments. Autocompletion is highly configurable but also quite complex. I would recommend to use oh-my-zsh for this.

Some Command-completes I really like are:

  • This comes with oh-my-zsh: git <TAB> or git co<TAB>
  • Get a list of your /etc/hosts: ssh root@<TAB>
  • Get a list of arguments of vim: vim -<TAB>
  • Get a list of processes to kill: kill <TAB>

Sophisticated Globbing

Globbing means searching files in your filesystem by using wildcards. This will search in your working directory and execute ls with all files(and directories) beginning with z :

➜  Documentation  ls z*
zorro.txt

zh_CN:
arm                  filesystems      magic-number.txt         stable_kernel_rules.txt
arm64                gpio.txt         oops-tracing.txt         SubmittingDrivers
basic_profiling.txt  HOWTO            SecurityBugs             SubmittingPatches
CodingStyle          io_ordering.txt  sparse.txt               video4linux
email-clients.txt    IRQ.txt          stable_api_nonsense.txt  volatile-considered-harmful.txt

With zsh you can also glob recursively

➜ linux-4.2.4 ls **/*.pl
arch/arm/crypto/bsaes-armv7.pl
arch/arm/crypto/sha256-armv4.pl
arch/arm/crypto/sha512-armv4.pl
Documentation/filesystems/cifs/winucase_convert.pl
Documentation/trace/postprocess/trace-pagealloc-postprocess.pl
Documentation/trace/postprocess/trace-vmscan-postprocess.pl
Documentation/video4linux/extract_xc3028.pl
drivers/crypto/vmx/aesp8-ppc.pl
drivers/crypto/vmx/ghashp8-ppc.pl
drivers/crypto/vmx/ppc-xlate.pl
drivers/scsi/script_asm.pl
drivers/usb/serial/ezusb_convert.pl
scripts/bootgraph.pl
scripts/checkincludes.pl
scripts/checkpatch.pl
scripts/checkstack.pl
scripts/checkversion.pl
scripts/export_report.pl
scripts/get_maintainer.pl
scripts/headerdep.pl
scripts/headers_check.pl
scripts/kconfig/streamline_config.pl
scripts/markup_oops.pl
scripts/namespace.pl
scripts/profile2linkerlist.pl
scripts/recordmcount.pl
tools/perf/scripts/perl/check-perf-trace.pl
tools/perf/scripts/perl/failed-syscalls.pl
tools/perf/scripts/perl/rw-by-file.pl
tools/perf/scripts/perl/rw-by-pid.pl
tools/perf/scripts/perl/rwtop.pl
tools/perf/scripts/perl/wakeup-latency.pl
tools/testing/ktest/compare-ktest-sample.pl
tools/testing/ktest/ktest.pl

Replace find

Sometimes we need to find some files and execute commands using the search-results as arguments. For example find all files with permissions 444 and move them to /tmp:

find . -perm -444 -exec mv {} /tmp \;

We can do this in zsh using the following command:

mv **/*(rAR) /tmp

r means “u+r”, A means “g+r” and R means “o+r”.

Finding files owned by root:

ls **/*(u:root:)

Finding files owned by root and with user-executable set:

ls -l **/*(u:root:x) 

A complete list of available expansion-commands can be found in the manual page:

man zshexp

Scripting

Zsh has lots of modules which can be loaded, it supports arrays and has lots of nice builtin features. A good point to get some overview are zsh-lovers.

Make zsh bash-compatible

By default zsh is not compatible with bash. if you need it bash-compatible just type:

emulate sh

Builtin Tetris

Last but not least: zsh has it’s builtin tetris!!! Type the following commands:

autoload -U tetris
zle -N tetris
bindkey ^h tetris

.. and hit <CTRL>+h

Conclusion

Zsh is fully customizable and very handy. Whenever I open its manual pages, i’ll find new cool features. Zsh improves my workflows, that’s why I love using it.

[ Linux  Sysadmin  Bash  RuleOfThree  urxvt  TerminalEmulator  Shell  Zsh  ]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 Unported License.

Copyright 2015-present Hoti