Zsh termination according to parameter

I am a pretty happy zsh user and especially appreciate its completion subsystem. Recently, I have been trying to improve some completion commands, especially the ones that I often use (e.g. rm). In zsh, the following command:

rm <TAB> 

makes zsh complete with the current directory files, as expected. I am trying to make zsh the following option:

 rm -r <TAB> 

with directories only.

Any zsh guru who knows how to refine parameters to take into account the (-r) parameter

+6
source share
1 answer

This can be achieved by editing the zsh completion function for rm to the desired behavior.

Z-shell completion functions are defined somewhere in your system. On OSX, it is under /usr/local/zsh/5.0.2/share/zsh/functions . Linux probably has something similar.

The termination function for rm is defined in a file named _rm . This is pretty small. (If you're interested, the termination for rmdir is defined in _directories ).

To set the termination for rm -r , we need to change the line that looks something like this:

 '(-r -R --recursive)'{-r,-R,--recursive}'[remove directories and their contents recursively]' 

We add to the end of this line inside the quote:

 :*:file:_directories 

I'm sure this tells zsh that the completion, when the -r argument is given, should consist of files , and the way we get those files is to use the _directories function _directories which only returns directories).

(I'm not 100% sure, because zsh is a beast, I still hug my little mind).

Here's a usage example:

enter image description here


Here's a spread of changes to make it clearer :)

 < '(-r -R --recursive)'{-r,-R,--recursive}'[remove directories and their contents recursively]:*:file:_directories' --- > '(-r -R --recursive)'{-r,-R,--recursive}'[remove directories and their contents recursively]' 
+5
source

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


All Articles