zsh-workers
 help / color / mirror / code / Atom feed
* starting <tab> completion / command history
@ 2011-08-11 21:11 Sebastian Tramp
  2011-08-12  2:08 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Tramp @ 2011-08-11 21:11 UTC (permalink / raw)
  To: zsh-workers

Hi zsh workers,

Is there a way to map a <tab> at the beginning of a command line to a
completion function?

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 idea is a little bit similar to the autojump command [1] which I
really love and imo such a tool does not exist yet (please point me to
such a project if you know something like that).

Best regards

Sebastian Tramp

  1. https://github.com/joelthelion/autojump

-- 
Sebastian Tramp - Department of Computer Science; University of Leipzig
WebID: http://sebastian.tramp.name  Tel. (Fax): +49 341 97 323-66 (-29)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: starting <tab> completion / command history
  2011-08-11 21:11 starting <tab> completion / command history Sebastian Tramp
@ 2011-08-12  2:08 ` Bart Schaefer
  2011-08-14 19:57   ` Sebastian Tramp
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2011-08-12  2:08 UTC (permalink / raw)
  To: zsh-workers

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: starting <tab> completion / command history
  2011-08-12  2:08 ` Bart Schaefer
@ 2011-08-14 19:57   ` Sebastian Tramp
  0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Tramp @ 2011-08-14 19:57 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Thu, Aug 11, 2011 at 07:08:09PM -0700, Bart Schaefer wrote:

Bart,

thank you very much for this snippet (and the notes!!).

It works exact as I wanted and I can now concentrate on how commands are
saved and which commands are displayed in which directories. big fat
kudo :)

seebi

> 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.

-- 
Sebastian Tramp
WebID: http://sebastian.tramp.name


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-08-14 19:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-11 21:11 starting <tab> completion / command history Sebastian Tramp
2011-08-12  2:08 ` Bart Schaefer
2011-08-14 19:57   ` Sebastian Tramp

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).