zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Re: starting <tab> completion / command history
Date: Thu, 11 Aug 2011 19:08:09 -0700	[thread overview]
Message-ID: <110811190809.ZM6937@torch.brasslantern.com> (raw)
In-Reply-To: <20110811211150.GA21221@soljaris7.fritz.box>

On Aug 11, 11:11pm, Sebastian Tramp wrote:
}
} Is there a way to map a <tab> at the beginning of a command line to a
} completion function?

What you need to do is bind tab to a non-completion zle widget so you
can examine the cursor position, then invoke the appropriate completion
widget depending on where you are.

    overload-tab () {
      if (( CURSOR < 1 ))
      then zle your-new-widget
      else zle expand-or-complete
      fi
    }
    zle -N overload-tab
    bindkey $'\t' overload-tab

Next of course you need to define your-new-widget.

} I want to use it to complete full command lines which I save as a
} history for each directory so that I can re-use commonly executed
} commands (incl. parameters) for a specific directory.

The problem with this scheme is that completion is EXTREMELY word-
oriented.  As soon as you introduce a command line containing spaces
and/or quotes and/or punctuation, completion is likely to become very
confused.

I've actually implemented something basically like this and the only
way I got it to work reasonably reliably was (a) bind it to something
other than TAB and (b) force it directly into menu selection so you
have to choose the command by navigation.  It's pretty simple and
looks like this:

-- 8< -- snip -- 8< --
#compdef -k menu-complete ^X:
zmodload -i zsh/complist
_cmdselect() {
local -a commands
commands=(${(f)"$(< ~/.cmdselect)"})
compadd -Qa commands
MENUSELECT=0
compstate[insert]=menu
}
_cmdselect "$@"
-- 8< -- snip -- 8< --

Note the #compdef line to automatically bind this to ctrl-x colon.
Also note the use of compadd -Q to insert the selection without turning
into a monolithic quoted word.


  reply	other threads:[~2011-08-12  2:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-11 21:11 Sebastian Tramp
2011-08-12  2:08 ` Bart Schaefer [this message]
2011-08-14 19:57   ` Sebastian Tramp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=110811190809.ZM6937@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).