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

Textfile-Favoring Completion



A while back I posted about trying to complete only text files for vi,
vim, and emacs.  I'd had the problem of opening executable files instead
of the longer named source files (app versus app.c).  Bart and Peter
offered some good suggestions, but for one reason or another, nothing's
worked out perfect.  I think Peter suggested this one:

   # First match files that end in known text file extensions or are
   # directories.  Failing that, match anything.
   zstyle ':completion:*:*:(emacs|vi|vim):*' file-patterns \
          '*.(c|h|pl|tex|txt|cpp|java|php|bib):globbed-files
          *(-/):directories' \
          '*:all-files'

This one works okay, but that regular expression which details all known
text files becomes unwieldy.  Dotfiles aren't currently matched with
that pattern.  I thought I'd lost my .muttrc once because only my .mutt
directory would complete.  Completion has minimized my usage of ls, and
this can get me in trouble.  Bart offered a solution that fixed this and
was almost always right on target, scanning the result of the UNIX file
utility for the word 'text'.  This was a little slow, however:

   textfiles() {
      reply=( ${${(@M)${(f)"$(file $REPLY 2>/dev/null)"}\:#*text*}%:*} )
   }
   zstyle ':completion:*:*:(emacs|vi|vim):*' file-patterns \
          '%p(e,textfiles,):globbed-files *(-/):directories' \
          '*:all-files' 

Now I'm using something slightly simpler that has the same drawback as
the first solution in hiding certain files when there's a valid match in
the first group.  (For instance, I want to complete an executable plain
file 'itself.sh' but there's a directory 'include' that supercedes.)  It
does cut back on the regular expression maintenance, though.  (I haven't
entered the extensions for LaTeX yet, so this may change.)

   # First match files that aren't plain executable files or don't
   # end in .o.  Failing that, match anything.
   zstyle ':completion:*:*:(emacs|vi|vim):*' file-patterns \
          '*~*.o(^*):globbed-files' '*:all-files'

Just thought I'd share.  Someone posted something similar specifically
about LaTeX a few weeks ago which caused me to revisit the issue.

-- 
Chris Johnson
cjohnson@xxxxxxxxxx
http://www.cs.utk.edu/~cjohnson



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