zsh: tips & tricks

  • Tutorial

zsh logoAbout zsh more than once wrote on a habr However, both those topics and my personal observations show that most people familiar with zsh use it as bash with an advanced auto-complete. And that one (autocomplete) is not used to its fullest.



I don’t want to describe how to color the console in all the colors of the rainbow or to Promt, showing everything in the world. I want to show that the possibilities of zsh are much wider, and using it as the main shell can make your work a little more beautiful.



Aliases


In zsh, they come in four forms:



  1. standard ( -r flag ) - behave exactly the same as in bash
  2. suffixes ( -s flag ) - allow you to associate files by extension with a specific application
  3. global ( -g flag ) - unlike the standard ones, the search is carried out along the entire line, and not only where the command can be
  4. hashes are sort of directory aliases

They wrote about the first two species more than once. The last two are more interesting.



Global aliases

alias -g g="| grep"
alias -g l="| less"

will allow instead
cat where | grep what | less
do
cat where g what l

The example is exaggerated, and in reality, aliases can (and should) be more complicated. But even this example shows a profit for those who have to deal a lot and work with pipes.

Without specifying a flag when creating an alias, it behaves both as standard and as global. But I recommend flagging to avoid ambiguity.



Aliases can be temporarily turned off / on with

disable -a aliasname
enable -a aliasname

Also, if an alias reassigns a command, you can reach it too.
\aliasname
#or
=aliasname

Hashes

hash -d apl=/private/var/log/apache2
hash -d p1=/srv/www/project1/html

Now, to go to the directory designated as a hash, just execute
cd ~hashname
or even
~hashname
if you use the autocd option .

Of course, hashes can be used in any other operations related to file paths:

cat ~hashname/file
cp ~hashname/file /path/somewhere

Autocomplete


He is truly powerful in zsh .
In addition to the fact that he knows how to complement the options of many commands (and it’s not difficult to write his own compliment), zsh allows you to twist them as you like:


zstyle ':completion:*:processes' command 'ps -ax' 
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;32'
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:kill:*'   force-list always
zstyle ':completion:*:processes-names' command 'ps -e -o comm='
zstyle ':completion:*:*:killall:*' menu yes select
zstyle ':completion:*:killall:*'   force-list always

This config will give us a compliment for the kill and killall commands :

DevMan% killall F
Finder    ForkLift

DevMan% kill Sa
 502 ??         2:49.10 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_53261                                                  
 823 ??        14:29.44 /System/Library/StagedFrameworks/Safari/WebKit2....
1509 ??         4:28.21 /Applications/Xmarks for Safari.app...

The decision on whether to use a case-insensitive compliment is left to you. Although personally I get much more benefit from it than harm.



Hooks


Hooks are functions that are called on a specific event.



I actively use 3 of them:



  1. chpwd - as you might guess from the name, called when the current directory is changed .
  2. preexec - called before executing any command.
  3. precmd - called before the output of the promt.

I use the first one to restore all consoles in those directories in which I was before reboot / logout.
I use the second and third hooks, in particular, to notify about processes that take longer than a specified time: I started, for example, assembling the project and switched to other tasks, when the assembly is finished, a notification of readiness will arrive at the notification center.



Finally, a little zsh magic


ls *(.)          # показать только файлы
ls *(.om[1])     # показать самый свежий файл
ls -l *(.m0)     # показать файлы измененные сегодня
ls **/style.css  # найти style.css по всем дискам
ls -l *(.Lk+50)  # показать все файлы более 50 Кб
ls **/*(.Lm+5)   # показать файлы более 5 Мб по всем дискам
ls *.^log        # показать все файлы, кроме .log
vi *(.om[1])     # открыть самый свежий файл
# многое из магии требует setopt EXTENDED_GLOB

This is an insignificant example of what zsh is capable of .
The modules, which are enough, were not affected.



As a teaching aid, in addition to native mana and a guide, I recommend to study




If the topic is interesting, continue.



Enjoy all the coding!


Also popular now: