How to keep the trailing slash after choosing a directory from the zsh completion options

autocompletezsh

In zsh, when I press tab 2 times to enter the "completion menu", select a directory and press enter, it first adds the chosen directory with a trailing slash to the command, but as soon as I press space to add more arguments, it removes it.

i.e. "rsync foo/" gets changed to "rsync foo " after pressing space.

Is there a way to disable this behaviour?

It forces me to always add slashes manually for commands like rsync in particular.

Best Answer

To retain the / added after completing directories or symbolic links to directories, turn off the option auto_remove_slash which is on by default.

setopt no_auto_remove_slash

For many commands, retaining the trailing slash makes no difference for directories, but causes the command to act on the target directory instead of the symbolic link if the argument is a symbolic link to a directory. For an rsync source argument, a trailing slash causes the directory to be merged into the destination rather than copied to a subdirectory.