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

Re: zsh completion help sought



Phil Pennock <phil.pennock@xxxxxxxxxxx> wrote:
> (1) I use $_<tab> frequently, rather than history expansion, when I'm
>     doing multiple operations on one file; I do want it expanded before
>     I press return, both as a sanity check and so that I can repeat a
>     command using command history.  How do I set a style/function so
>     that if the current word is $_ and the cursor is at the end, to
>     ignore all those evil no-namespace-workaround-hack functions and
>     just expand the $_ variable itself?

You do know about the non-completion binding ESC-. (widget
insert-last-word) to recover the last word of the previous line (repeat to
go to earlier lines; there's a contributed widget to allow previous words
on the line you've landed on, too)?

In general, it *ought* to be something like this:

# Ensure <TAB> is using expansion through compsys
bindkey '^i' complete-word
# Put the expand completer before the complete completer
zstyle ':completion:*' completer _expand _complete
# Force expand to expand exactly typed parameters.
zstyle ':completion:*:expand:*' accept-exact true

Unfortunately, though you (well, I, with a rather more complicated set of
settings) get $_ expanded this way, you still run into a namespace
problem and get whatever $_ was set to inside the completion function,
which isn't what you want.

If you're wedded to an idea of this kind, you could use an accept-line hack
to get the last thing on the line assigned to a different parameter, say
a:

accept-line() {
  # for the execution of this line, $a is what we remembered from the old one
  typeset -g a=$newa
  # split the line we're about to execute into shell words
  local -a line
  line=(${(z)BUFFER})
  # remember the last shell word for next time
  typeset -g newa=$line[-1]

  # do normal end-of-line processing
  zle .accept-line
}
zle -N accept-line

Putting this all together, your trick ought to work with $a instead of $_.
(I did get this to work, but I can't be sure you have all the same
ingredients I do.  You don't want to see my entire set of styles, believe
me.)

> (2) As a similar sanity check before pressing return, I like to
>     tab-expand a glob to cast an eye over the list and catch my more
>     egregrious mistakes.  How do I keep menu-completion turned on for
>     tab expansions of a bare suffix but move straight to glob expansion
>     if the end of the current word is a '*' ?  (ie, the item you get in
>     the new system when tab cycles far enough through the options to
>     show you all the options, just before showing you the bare '*')?

I'm not sure I follow all of that, but the key step in getting expansion to
insert all expansions is:

zstyle ':completion:*:expand:*' tag-order all-expansions

This says that the choice from the result of the expand completer which is
tagged as all-expansions is to be preferred over others.

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com



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