zsh-users
 help / color / mirror / code / Atom feed
* Context-aware file name completion with preferences
@ 2010-05-01 13:19 Mikael Puhakka
  2010-05-01 20:27 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Puhakka @ 2010-05-01 13:19 UTC (permalink / raw)
  To: zsh-users

Hello dear zsh'ers

I switched from bash a ~month ago, and I've liked about everything zsh
has to offer. Relaxed flexibility, slick form and fast, what the heck.

There's only one greater issue that I haven't resolved yet. It's not a
harmful problem but I'd prefer to get it right for the sake of
usability.

I mean context aware file name completioning with set preferences:
which files are preferred in completion list, which aren't. I don't
want to exclude anything out; that would be too easy and somewhat
intolerant for naming mistakes. A few examples: consider a directory
with typical LaTeX document:

Doc1.tex
Doc1.aux
Doc1.dvi
Doc1.log
...

Of these files, I'd edit the .tex file most often: I would like zsh to
prefer .tex files to anything else from these files. They still should
appear in the completion cycle, but not first. In a similar manner I
would set file type preferences for "c", "cpp", "java" (speaking of
the devil, I hate the way bash always completes foo.class before
foo.java).

Is this doable? How about an opposite? There would be many files
readable by the editor, but I don't want to edit, say aux files very
often, if ever.

How to deal with this kind of demand? I hope zsh is up to the task :)

--Mikael


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

* Re: Context-aware file name completion with preferences
  2010-05-01 13:19 Context-aware file name completion with preferences Mikael Puhakka
@ 2010-05-01 20:27 ` Bart Schaefer
  2010-05-01 20:51   ` Mikael Puhakka
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2010-05-01 20:27 UTC (permalink / raw)
  To: Mikael Puhakka; +Cc: zsh-users

On Sat, May 1, 2010 at 6:19 AM, Mikael Puhakka <mr.progo@gmail.com> wrote:
>
> Doc1.tex
> Doc1.aux
> Doc1.dvi
> Doc1.log
> ...
>
> Of these files, I'd edit the .tex file most often: I would like zsh to
> prefer .tex files to anything else from these files. They still should
> appear in the completion cycle, but not first.

Assuming you've enabled the shell function completion system with
"compinit", you want to add some zstyle commands to your startup
files.

In particular, you're looking for the group-name, group-order, and
tag-order styles.  You use group-name to organize sets of matches,
group-order to determine the display order of those sets, and
tag-order to determine whether particular sets are offered (or not).

Well, actually most often you set group-name to the empty string and
allow zsh to name the groups for you.

zstyle ':completion:*' group-name ''

in the particular case of file completion you can use the
file-patterns style to organize sets of file names.  A common setting
is something like this:

zstyle ':completion:*' file-patterns '%p:globbed-files
*(-/):directories' '*:all-files'

This means to offer globbed files and directories in the first set of
completions, and everything else if there are no globbed files or
directories among the possible matches.  (A globbed-file is just one
whose name can be generated from a wildcard pattern that you may have
typed on the command line.)

You can extend this:

zstyle ':completion:*' file-patterns '*.(c|cpp|java|tex|txt):editable-files' \
     '%p:globbed-files *(-/):directories' '*:all-files'

Note placement of quotes and spaces; a space inside a quoted string
separates groups that are displayed together, while a space between
quoted strings separates groups that are displayed sequentially, e.g.
you'll get editable files if there are any, then globbed files or
directories, and finally anything.

It's been a long time since I plugged this, but if you really want to
learn about this in detail you should pick up a copy of "From Bash to
Z Shell: Conquering the Command Line" from www.apress.com.


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

* Re: Context-aware file name completion with preferences
  2010-05-01 20:27 ` Bart Schaefer
@ 2010-05-01 20:51   ` Mikael Puhakka
  2010-05-02  9:16     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Puhakka @ 2010-05-01 20:51 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Whoah! Thanks for a thorough explanation on how this works! The book
looks tasty and doesn't cost a fortune, I'll order it if you recommend
it. Seems to me that you know your way with zsh. Is there any other
book recommendation for mastering zsh?

--Mikael

On Sat, May 1, 2010 at 23:27, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sat, May 1, 2010 at 6:19 AM, Mikael Puhakka <mr.progo@gmail.com> wrote:
>>
>> Doc1.tex
>> Doc1.aux
>> Doc1.dvi
>> Doc1.log
>> ...
>>
>> Of these files, I'd edit the .tex file most often: I would like zsh to
>> prefer .tex files to anything else from these files. They still should
>> appear in the completion cycle, but not first.
>
> Assuming you've enabled the shell function completion system with
> "compinit", you want to add some zstyle commands to your startup
> files.
>
> In particular, you're looking for the group-name, group-order, and
> tag-order styles.  You use group-name to organize sets of matches,
> group-order to determine the display order of those sets, and
> tag-order to determine whether particular sets are offered (or not).
>
> Well, actually most often you set group-name to the empty string and
> allow zsh to name the groups for you.
>
> zstyle ':completion:*' group-name ''
>
> in the particular case of file completion you can use the
> file-patterns style to organize sets of file names.  A common setting
> is something like this:
>
> zstyle ':completion:*' file-patterns '%p:globbed-files
> *(-/):directories' '*:all-files'
>
> This means to offer globbed files and directories in the first set of
> completions, and everything else if there are no globbed files or
> directories among the possible matches.  (A globbed-file is just one
> whose name can be generated from a wildcard pattern that you may have
> typed on the command line.)
>
> You can extend this:
>
> zstyle ':completion:*' file-patterns '*.(c|cpp|java|tex|txt):editable-files' \
>     '%p:globbed-files *(-/):directories' '*:all-files'
>
> Note placement of quotes and spaces; a space inside a quoted string
> separates groups that are displayed together, while a space between
> quoted strings separates groups that are displayed sequentially, e.g.
> you'll get editable files if there are any, then globbed files or
> directories, and finally anything.
>
> It's been a long time since I plugged this, but if you really want to
> learn about this in detail you should pick up a copy of "From Bash to
> Z Shell: Conquering the Command Line" from www.apress.com.
>


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

* Re: Context-aware file name completion with preferences
  2010-05-01 20:51   ` Mikael Puhakka
@ 2010-05-02  9:16     ` Bart Schaefer
  2010-05-02 10:21       ` Mikael Puhakka
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2010-05-02  9:16 UTC (permalink / raw)
  To: Mikael Puhakka; +Cc: zsh-users

On Sat, May 1, 2010 at 1:51 PM, Mikael Puhakka <mr.progo@gmail.com> wrote:
> Whoah! Thanks for a thorough explanation on how this works!

You're welcome, but I can't really pretend that explanation was all
that thorough.  There's rather a lot of details I'm not going to try
to write out ...

> Is there any other book recommendation for mastering zsh?

No other book, but the online user guide may be helpful (don't let the
2003 date on the front page worry you, it has been updated a bit more
recently than that).

http://zsh.sourceforge.net/Guide/zshguide.html


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

* Re: Context-aware file name completion with preferences
  2010-05-02  9:16     ` Bart Schaefer
@ 2010-05-02 10:21       ` Mikael Puhakka
  0 siblings, 0 replies; 5+ messages in thread
From: Mikael Puhakka @ 2010-05-02 10:21 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On Sun, May 2, 2010 at 12:16, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Sat, May 1, 2010 at 1:51 PM, Mikael Puhakka <mr.progo@gmail.com> wrote:
>> Whoah! Thanks for a thorough explanation on how this works!
>
> You're welcome, but I can't really pretend that explanation was all
> that thorough.  There's rather a lot of details I'm not going to try
> to write out ...
>

Yes, well I noticed that the explanation of yours doesn't actually
allow completing the other kinds of files. I adapted one example for
PDF reading:

zstyle ':completion:*:(xpdf|epdfview|okular):*' file-patterns \
  '*.(pdf|ps):readable-files *(-/):directories' '*:all-files'

Where I had to move directories to the first set of quotes. Using and
slightly modifying the set example it wouldn't allow me to complete
directories at all. I think this version doesn't cover the case
'*:all-files' at all either. For that particular example (pdf
reading/browsing) this is excellent, for the rest, I'll wait for the
book.

Another good example of this kind of priority completion would be
video watching. For mplayer, I'd like to prefer extensions like "avi",
"mkv" etc. But not all videos have a common extension if not extension
at all! For instance, stream dumps can be without any specific name.
This is why I find important that zsh will also complete names that
aren't preferred, if there are no good choices. Your example pushed me
towards, however. And now I have something concrete to grep and find
in the manuals. The book will hopefully answer the rest.

>> Is there any other book recommendation for mastering zsh?
>
> No other book, but the online user guide may be helpful (don't let the
> 2003 date on the front page worry you, it has been updated a bit more
> recently than that).
>
> http://zsh.sourceforge.net/Guide/zshguide.html
>

This one is familiar. Perhaps not the clearest or simplest guide, (I
learned the basics of Zsh using the FAQ) but as a reference quite
good. The manpages are too enormous to be learned from! But being
references they work all along well.


-- Mikael


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

end of thread, other threads:[~2010-05-02 10:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-01 13:19 Context-aware file name completion with preferences Mikael Puhakka
2010-05-01 20:27 ` Bart Schaefer
2010-05-01 20:51   ` Mikael Puhakka
2010-05-02  9:16     ` Bart Schaefer
2010-05-02 10:21       ` Mikael Puhakka

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