Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: insert-last-word makes space disappear

On Fri, 13 Aug 2004, Jesper Holmberg wrote:

> What happens is that the combination of menu-complete and 
> insert-last-word (bound to alt-.) sometimes makes a vital space 
> disappear on the command line.

I suspect that you don't really have alt-. bound to insert-last-word,
or more precisely that you have replaced the builtin insert-last-word
widget with a user-defined widget such as smart-insert-last-word from
the Functions/Zle directory.

Automatic removal of spaces (auto-suffixes in general, really) is a tricky 
business.  The rule (as nearly as it can be stated) is supposed to be that 
the suffix gets removed if the _next_ widget is any one that either does 
_not_ perform an insertion, or that inserts the suffix.  What this means 
is that completion puts the suffix into the buffer and stashes away the 
fact that a suffix was added and what it was, and then it's up to zle (not 
any particular widget, but the editor engine itself) to adjust the suffix
properly _after_ the next keystroke has been read but _before_ the widget
for that keystroke is called.

This is handled for builtin widgets by flagging in the widget table those 
widgets that perform insertions -- but there's no provision for flagging a 
user-defined widget one way or the other.  This is dealt with by deferring 
the suffix decision until either (1) "zle any-builtin-widget-name-here" is 
called, in which case the flag for that builtin applies, or (2) one of the 
buffer variables is modified, in which case the suffix is normally kept.  
Most of the time, this results in the right behavior.

In the case of smart-insert-last-word, unfortunately, the first thing that 
happens is a call to "zle .up-history" so that it can fetch the last word 
from the previous history entry.  Case (1) applies; the suffix removal 
rule for a motion like up-history is to remove the suffix; and thus you
get the behavior you saw.

The workaround for this is to make smart-insert-last-word (or whatever the 
function for your user-defined widget is) forcibly keep the suffix, by 
e.g. placing LBUFFER+='' or an equivalent assignment before the call to 
"zle .up-history".

An alternate solution would be to rewrite smart-insert-last-word to use
(and thus require) the zsh/parameter module and the $history variable,
rather than using zle history motion widgets.

The best long-term solution would be to extend the user-defined widget
creation routines to flag widgets that should keep (or erase) a previous
auto-suffix, so that the internal implementation of the widget doesn't

Messages sorted by: Reverse Date, Date, Thread, Author