How stop zsh from eating space before pipe symbol

oh-my-zshzsh

When I type a space followed by the pipe symbol '|' on the zsh command line, zsh eats the space, placing the pipe symbol directly up against the prior word. How do I stop it from doing that? I'm not sure about any possible stylistic conventions, but I like a space on both sides of the pipe symbol for readability. I'm using oh-my-zsh pretty much out of the box.

Solution

Based on mpy's answer below, I used the following solution:

By default, ZLE_REMOVE_SUFFIX_CHARS is undefined in zsh/ohmyzsh. When undefined, it acts as though it was:

ZLE_REMOVE_SUFFIX_CHARS=$' \t\n;&|'

I added the following definition to my ~/.zshrc:

ZLE_REMOVE_SUFFIX_CHARS=$' \t\n;&'

(all except pipe) and presto, perfect! problem gone.

Best Answer

I suppose you mean that when you TAB complete a command / filename a space is added automatically, but after pressing | it vanishes again. Otherwise I can't reproduce that effect.

However, in that case the solution should be as simple as

ZLE_REMOVE_SUFFIX_CHARS=""

The explanation is a litly bit tricky, so I simply quote man zshparam

ZLE_REMOVE_SUFFIX_CHARS / ZLE_SPACE_SUFFIX_CHARS These parameters are used by the line editor. In certain circumstances suffixes (typically space or slash) added by the completion system will be removed automatically, either because the next editing command was not an insertable character, or because the character was marked as requiring the suffix to be removed.

These variables can contain the sets of characters that will cause the suffix to be removed. If ZLE_REMOVE_SUFFIX_CHARS is set, those characters will cause the suffix to be removed; if ZLE_SPACE_SUFFIX_CHARS is set, those characters will cause the suffix to be removed and replaced by a space.

If ZLE_REMOVE_SUFFIX_CHARS is not set, the default behaviour is equivalent to:

ZLE_REMOVE_SUFFIX_CHARS=$' \t\n;&|'

If ZLE_REMOVE_SUFFIX_CHARS is set but is empty, no characters have this behaviour. ZLE_SPACE_SUFFIX_CHARS takes precedence, so that the following:

ZLE_SPACE_SUFFIX_CHARS=$'&|'

causes the characters & and | to remove the suffix but to replace it with a space.

To illustrate the difference, suppose that the option AUTO_REMOVE_SLASH is in effect and the directory DIR has just been completed, with an appended /, following which the user types &. The default result is DIR&. With ZLE_REMOVE_SUFFIX_CHARS set but without including & the result is DIR/&. With ZLE_SPACE_SUFFIX_CHARS set to include & the result is DIR &.

Note that certain completions may provide their own suffix removal or replacement behaviour which overrides the values described here.

Related Question