⬆️ ⬇️

zsh: tips & tricks

zsh logo About zsh more than once wrote on Habré. However, both those topics and my personal observations show that most people familiar with zsh use it as a bash with advanced autocomplete. And that (autocomputer) is not used to its fullest.





I do not want to describe how to paint the console in all the colors of the rainbow or make promt, showing everything. I want to show that zsh's capabilities are much broader, and using it as the main shell can make your work a little more beautiful.





Aliases



In zsh, they are of four types:



')

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


About the first two types were written more than once. The last two are more interesting.





Global aliases


alias -gg="| grep" alias -gl="| less" 


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 difficult. But even this example shows a profit for those who have a lot of trouble and work with pipes.



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





Aliases can be temporarily turned off / on by

 disable -a aliasname enable -a aliasname 


Also, if the alias reassigns any command, you can reach it.
 \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 run
 cd ~hashname 
or at all
 ~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 really powerful in zsh .

In addition to being able to complement the options of many commands (and it’s easy to write your own complement), zsh allows you to twist it as you please:



 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 complement 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... 


Deciding whether to use a case-insensitive complex I leave to you. Although I personally get from him a lot more good than harm.





Hooks



Hooks are functions that are called upon a specific event.





I actively use 3 of them:





  1. chpwd - as you can guess from the name, called when changing the current directory.
  2. preexec - called before executing any command.
  3. precmd - called before promt is displayed.


The first one I use to restore all consoles in those directories in which I was before the reboot / logout.

I use the second and third hooks, in particular, to notify about processes running for longer than a specified time: I started the project build, for example, and switched to other tasks when the build ends in the notification center when the readiness notification arrives.





Finally a bit of 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.

Not affected modules, which is enough.





As a teaching tool, in addition to native mans and guide, I recommend to study







If the topic is interesting, we will continue.





Enjoy all the coding!

Source: https://habr.com/ru/post/164597/



All Articles