Search code examples
zshzsh-completion

history expansion in zsh


according to the doc, !! should expand to the last command when hit <tab> after it. However, this is not the case for my setup (I used default oh-my-zsh). Here is an example: $ echo 1 1 $ echo 2 2 $ !!<tab> $ echo 1

Moreover, !# does not expand to what are input in the current line. Instead, it expands to the last command $ echo 1 1 $ echo 2 2 $ echo 3 !#<tab> $ echo 3 echo 2

Is there any option controls this?


Solution

  • I had a look at the default oh-my-zsh completion settings on github and it look like parameter expansion is not enabled out of the box. According to oh-my-zsh documentation any overrides should go into custom/ directory, in files ending with *.zsh. Getting parameter expansion to work should be as simple as dropping there a file with something like this:

    zstyle ':completion:*' completer _expand _complete
    

    The completer function you're looking for is called _expand, here is what man zshcompsys has to say about it:

    _expand
    
    This completer function does not really perform completion, but instead 
    checks if the word on the command line is eligible for
    expansion and, if it is, gives detailed control over how this
    expansion is done. For this to happen, the completion system needs to
    be invoked with complete-word, not expand-or-complete (the default
    binding for TAB), as otherwise the string will be expanded by the
    shell's internal mechanism before the completion system is started.
    Note also this completer should be called before the _complete
    completer function.
    
    The tags used when generating expansions are all-expansions for the
    string containing all possible expansions, expansions when adding the
    possible expansions as single matches and original when adding the
    original string from the line. The order in which these strings are
    generated, if at all, can be controlled by the group-order and
    tag-order styles, as usual.
    
    The format string for all-expansions and for expansions may contain
    the sequence '%o' which will be replaced by the original string from
    the line.
    
    The kind of expansion to be tried is controlled by the substitute,
    glob and subst-globs-only styles.
    
    It is also possible to call _expand as a function, in which case the
    different modes may be selected with options: -s for substitute, -g
    for glob and -o for subst-globs-only.