zsh-users
 help / color / mirror / code / Atom feed
* Aliases (abbreviations) for command completion?
@ 2021-07-22  8:51 Peter Slížik
  2021-07-22 17:50 ` Grant Taylor
  2021-07-22 21:03 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: Peter Slížik @ 2021-07-22  8:51 UTC (permalink / raw)
  To: zsh

[-- Attachment #1: Type: text/plain, Size: 514 bytes --]

Hello,

is it possible to define something like "aliases" (apologies for the lack
of a better term) for command completion?

If the completed command consists of multiple words, I would like to be
able to type the first letters of individual words and have this
abbreviation expanded to the whole command name (taking into account that
the abbreviation is not a prefix of the completed text).

For example:

ome<Tab> => outcomingmessageeditor
cs<Tab> => changestatus

Best regards,
Peter Slížik

[-- Attachment #2: Type: text/html, Size: 732 bytes --]

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

* Re: Aliases (abbreviations) for command completion?
  2021-07-22  8:51 Aliases (abbreviations) for command completion? Peter Slížik
@ 2021-07-22 17:50 ` Grant Taylor
  2021-07-22 21:03 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Grant Taylor @ 2021-07-22 17:50 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 2459 bytes --]

On 7/22/21 2:51 AM, Peter Slížik wrote:
> Hello,

Hi,

> is it possible to define something like "aliases" (apologies for the 
> lack of a better term) for command completion?

I believe that you can define aliases of shorter commands in place of 
longer commands.  But I'm not sure how that would interact with command 
completion as such.

> If the completed command consists of multiple words, I would like to be 

> able to type the first letters of individual words and have this 
> abbreviation expanded to the whole command name (taking into account 
> that the abbreviation is not a prefix of the completed text).

First words ($0) can use normal aliases.  Subsequent words (>= $1) will 

need global aliases.

> For example:
> 
> ome<Tab> => outcomingmessageeditor
> cs<Tab> => changestatus

Those are all single words ($0) to me.

Your request seems to be very similar to something that I asked about a 
while ago.  TL;DR:  I tried overloading space as a completion character 
(in addition to tab) and had some strange side effects and ended up 
abandoning it.

I was wanting something akin to what Cisco IOS (classic) and Juniper 
Junos do.  Wherein "sh ru" gets treated as if it was "show run" because 
of the unabiguity in that there were no other commands that start with 
"sh" or parameters thereto that start with "ru".  Thus Cisco IOS was 
able to deduce that the command is "show run".

Juniper Junos does something slightly different.  Junos does treat the 
space very much like the completion key.  If I type "sh" followed by a 
space, it interactively expands command to be "show ".  When I type 
"conf" followed by a space, it expands it to be "configuration".

Despite being different, both the Cisco IOS and Juniper Junos method 
achieve similar effects.  I've not yet found a way to accomplish this 
(without negative side effects) in Zsh.

Aside:  My brain sort of likes the Cisco IOS method for brevity / 
characters sent (think old serial / modem connections).  But I strongly 
prefer the full command a la Juniper Junos for documentation / training 
purposes.  You need to know that "sh ru" is "show run" or that "conf t" 
is "configure terminal".  It's harder to learn something from the 
abbreviation than it is to learn from the full command.

I don't know if this is what you're asking about, or just coincidentally 
similar.



-- 
Grant. . . .
unix || die


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4013 bytes --]

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

* Re: Aliases (abbreviations) for command completion?
  2021-07-22  8:51 Aliases (abbreviations) for command completion? Peter Slížik
  2021-07-22 17:50 ` Grant Taylor
@ 2021-07-22 21:03 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2021-07-22 21:03 UTC (permalink / raw)
  To: Peter Slížik; +Cc: zsh

On Thu, Jul 22, 2021 at 1:52 AM Peter Slížik <peter.slizik@gmail.com> wrote:
>
> is it possible to define something like "aliases" (apologies for the lack of a better term) for command completion?

So the problem statement, paraphrased, is:
  Without using the "alias" builtin to create command shorthands,
provide for command completion to expand a short string to an
arbitrary longer one.

Is that correct?

> If the completed command consists of multiple words, I would like to be able to type the first letters of individual words

You don't mean space-separated shell syntax "words" here, I take it.

This is going to be nearly impossible unless you predefine specific
abbreviations, because determining "the first letters" without some
kind of delimiter (even as simple as camel-casing) is a hard problem.

> and have this abbreviation expanded to the whole command name (taking into account that the abbreviation is not a prefix of the completed text).

The way to do this (in general) is to use "compadd -U", which says to
ignore (erase) the word on the command line and substitute the
completion.  The tricky bit here is that if your abbreviation is also
a prefix of some other command name, the result becomes ambiguous, in
which case completion is likely to delete a trailing portion of the
string on the line and offer you the menu (e.g., in your first
example, "ome" might become "o" and a listing of commands).

To avoid creating the ambiguity, you need to define a completer
function (to be referenced in zstyle :completion::::: completer ...)
and make that the first function in the completer zstyle.  Because the
completer zstyle is looked up with incomplete context (:-command-: is
not yet known), your function will have to check internally whether
((CURRENT==1)) and return nonzero when not.  Otherwise, the function
should look up, using ${words[CURRENT]} as the key, what the
completion should be, and then either pass that result to "compadd
-U", or return nonzero if there is no result.  A zero return from this
function will cause its replacement to be used in preference to
anything that might be found by later completers in the list.

Exactly what "look up" means, is up to you.  You could use a case/esac
in the function, or an associative array variable, or even a zstyle.

In the event that you want to discover potential ambiguity, there are
some other approaches, but I won't go into that unless there is
interest.


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

end of thread, other threads:[~2021-07-22 21:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-22  8:51 Aliases (abbreviations) for command completion? Peter Slížik
2021-07-22 17:50 ` Grant Taylor
2021-07-22 21:03 ` Bart Schaefer

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