zsh-users
 help / color / mirror / code / Atom feed
* Why doesn't cd ignore files when you type say "cd fred*"
@ 2003-12-04 14:54 zzapper
  2003-12-04 15:37 ` Peter Stephenson
  2003-12-04 17:30 ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: zzapper @ 2003-12-04 14:54 UTC (permalink / raw)
  To: zsh-users

Hi Y'All

Why doesn't cd ignore files when you type say "cd fred*" and there
exists a file frederick.txt and a directory frederick ??

I guess it's becauses the FNG handling  fred* doesn't know that it's
output is to be passed to cd .

But can clever zsh be configued to avoid this behaviour.

Could I tell cd to ignore files ending say .html,.txt etc




zzapper
--

vim -c ":%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?"

http://www.vim.org/tips/tip.php?tip_id=305  Best of Vim Tips


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 14:54 Why doesn't cd ignore files when you type say "cd fred*" zzapper
@ 2003-12-04 15:37 ` Peter Stephenson
  2003-12-04 16:47   ` zzapper
  2003-12-04 17:30 ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2003-12-04 15:37 UTC (permalink / raw)
  To: zsh-users

zzapper wrote:
> Why doesn't cd ignore files when you type say "cd fred*" and there
> exists a file frederick.txt and a directory frederick ??
> 
> I guess it's becauses the FNG handling  fred* doesn't know that it's
> output is to be passed to cd .

It also doesn't know whether or not you want error checking.

> But can clever zsh be configued to avoid this behaviour.
> 
> Could I tell cd to ignore files ending say .html,.txt etc

Use a wrapper.  It's not 100% accurate in guessing what form of cd you
are using, but it will work the vast majority of the time.

cd() {
  local -a dirs
  local d
  for d in $*; do
    [[ -d $d ]] && dirs=($dirs $d)
  done
  if (( ${#dirs} == 1 )); then
    builtin cd $dirs
  else
    # Assume it's something like e.g. `cd SOURCE REPLACE'
    builtin cd "$@"
  fi
}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 15:37 ` Peter Stephenson
@ 2003-12-04 16:47   ` zzapper
  2003-12-05 10:54     ` Oliver Kiddle
  0 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2003-12-04 16:47 UTC (permalink / raw)
  To: zsh-users

On Thu, 04 Dec 2003 15:37:54 +0000, Peter Stephenson <pws@csr.com>
wrote:

>Use a wrapper.  It's not 100% accurate in guessing what form of cd you
>are using, but it will work the vast majority of the time.
>
>cd() {
>  local -a dirs
>  local d
>  for d in $*; do
>    [[ -d $d ]] && dirs=($dirs $d)
>  done
>  if (( ${#dirs} == 1 )); then
>    builtin cd $dirs
>  else
>    # Assume it's something like e.g. `cd SOURCE REPLACE'
>    builtin cd "$@"
>  fi
>}

Yes tasty! ( I've called this cdd and left the builtin cd alone). (is
the keyword function optional?)

But are there any solutions using COMPCTRL?

zzapper
--

vim -c ":%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?"

http://www.vim.org/tips/tip.php?tip_id=305  Best of Vim Tips


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 14:54 Why doesn't cd ignore files when you type say "cd fred*" zzapper
  2003-12-04 15:37 ` Peter Stephenson
@ 2003-12-04 17:30 ` Bart Schaefer
  2003-12-04 20:50   ` zzapper
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2003-12-04 17:30 UTC (permalink / raw)
  To: zsh-users

On Dec 4,  2:54pm, zzapper wrote:
} 
} Why doesn't cd ignore files when you type say "cd fred*" and there
} exists a file frederick.txt and a directory frederick ??
} 
} I guess it's becauses the FNG handling  fred* doesn't know that it's
} output is to be passed to cd .

Congratulations, you win a no-prize for accurate guessing.

} But can clever zsh be configued to avoid this behaviour.

You could always use a wrapper like this:

	function cd {
	    local -a dirs
	    dirs=( "${^@}"(-/N) )
	    if (( $#dirs == 1 ))
	    then builtin cd $dirs
	    else builtin cd "$@"
	    fi
	}

However, that might sometimes foil use of the two-argument form of "cd".


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 17:30 ` Bart Schaefer
@ 2003-12-04 20:50   ` zzapper
  2003-12-04 22:56     ` Pavol Juhas
  0 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2003-12-04 20:50 UTC (permalink / raw)
  To: zsh-users

Thanx Stephen & Bart

Can anyone talk me thru the following two expressions  I can guess
what they are doing but I'd like to know the principles (which do the
same thing (?)). I'm a zsh newbie and the shell syntax is still a bit
shocking!


   dirs=( "${^@}"(-/N) )  


[[ -d $d ]] && dirs=($dirs $d)   # if $d is a directory then load
dirname in array dirs
zzapper
--

vim -c ":%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?"

http://www.vim.org/tips/tip.php?tip_id=305  Best of Vim Tips


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 20:50   ` zzapper
@ 2003-12-04 22:56     ` Pavol Juhas
  0 siblings, 0 replies; 9+ messages in thread
From: Pavol Juhas @ 2003-12-04 22:56 UTC (permalink / raw)
  To: zsh-users

On Thu, Dec 04, 2003 at 08:50:33PM +0000, zzapper wrote:
> Thanx Stephen & Bart
> 
> Can anyone talk me thru the following two expressions  I can guess
> what they are doing but I'd like to know the principles (which do the
> same thing (?)). I'm a zsh newbie and the shell syntax is still a bit
> shocking!
> 
>    dirs=( "${^@}"(-/N) )  

"${^array}"_text  expands array to
    "${array[1]}"_text "${array[2]}"_text ...
and so the expression in parenthesis is expanded to
    ( "$1"(-/N) "$2"(-/N) "$3"(-/N) ... )

(-/N) is a globing qualifier, where '-' specifies that following
qualifiers apply to targets of symlinks, '/' requests directories, and
so '-/' requires directories or symbolic links that point to
directories.  'N' turns on the null_glob option for this pattern, which
means that the word is deleted if it does not match a file (otherwise an
error message would be printed).  You may try this with
    print no_dir(/)
and
    print no_dir(/N)

Summing up  ( "${^@}"(-/N) )  is expanded to an array of positional
arguments which are directories or symbolic links to directories.

Ref: man zshexpn  then search for /glob qua/
     info zsh filename\ generation
     zsh reference card from www.zsh.org is also very useful

HTH,

Pavol


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-04 16:47   ` zzapper
@ 2003-12-05 10:54     ` Oliver Kiddle
  2003-12-05 11:06       ` zzapper
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Kiddle @ 2003-12-05 10:54 UTC (permalink / raw)
  To: zsh-users; +Cc: zzapper

zzapper wrote:

> Yes tasty! ( I've called this cdd and left the builtin cd alone). (is
> the keyword function optional?)
> 
> But are there any solutions using COMPCTRL?

Yes.

With old style completion, try the globcomplete option.

With new completion, you can use the _match completer. You can use it
in the main list of completers. That will cause all globs like "fred*"
to be matched against the completion matches.

I prefer _expand because it does proper filename generation, handling
globbing flags etc but I bind _match to a separate key. So if I was to
do `cd fred*' and then press escape *, it would just complete the
directory. I think this below is the relevant setup code I use. You may
want to adjust the first zstyle and do without the second.

bindkey '\e*' match-word
zstyle ':completion:match-word::::' completer _all_matches _match _ignored
zstyle ':completion:match-word:*' match-original both
zle -C match-word complete-word _generic

Oliver


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-05 10:54     ` Oliver Kiddle
@ 2003-12-05 11:06       ` zzapper
  2003-12-05 14:27         ` Oliver Kiddle
  0 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2003-12-05 11:06 UTC (permalink / raw)
  To: zsh-users

On Fri, 05 Dec 2003 11:54:38 +0100, Oliver Kiddle
<okiddle@yahoo.co.uk> wrote:
So if I was to
>do `cd fred*' and then press escape *, it would just complete the
>directory. I think this below is the relevant setup code I use. You may
>want to adjust the first zstyle and do without the second.
>
>bindkey '\e*' match-word
>zstyle ':completion:match-word::::' completer _all_matches _match _ignored
>zstyle ':completion:match-word:*' match-original both
>zle -C match-word complete-word _generic

Oliver that works great but what is all that gobbledegook?


zzapper
--

vim -c ":%s/^/WhfgTNabgureRIvzSUnpxre/|:%s/[R-T]/ /Ig|:normal ggVGg?"

http://www.vim.org/tips/tip.php?tip_id=305  Best of Vim Tips


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

* Re: Why doesn't cd ignore files when you type say "cd fred*"
  2003-12-05 11:06       ` zzapper
@ 2003-12-05 14:27         ` Oliver Kiddle
  0 siblings, 0 replies; 9+ messages in thread
From: Oliver Kiddle @ 2003-12-05 14:27 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users

zzapper wrote:
> >
> >bindkey '\e*' match-word
> >zstyle ':completion:match-word::::' completer _all_matches _match _ignored
> >zstyle ':completion:match-word:*' match-original both
> >zle -C match-word complete-word _generic
> 
> Oliver that works great but what is all that gobbledegook?

It's not easy to explain that fully in a short e-mail.

Hopefully you're already familar with bindkey. It allows you to bind a
key or key combination to an editor command. This binds escape * to
`match-word'.

In addition to lots of builtin editor commands, zsh allows you to
define your own. The `zle -C' command here creates the match-word
editor command. Editor commands which do completion are a little
different so they are defined with `zle -C' instead of `zle -N'.

To implement a user-defined editor widget, a function has to be
written. In this case, we use _generic which is an already existing
completion function. _generic hooks in to the existing function based
completion system. With it, escape * is now going to behave like a
second tab key and do completion. With the zstyle commands we make it
behave differently while still doing completion.

zstyle is a powerful command which is used for configuring many aspects
of completion. It is similar to setting shell options or variables but
a particular "style" can have a different value in different contexts.

You may have already used zstyle in your startup file. The `completer'
style is a very common one. For example, you may use something like:

zstyle ':completion:::::' completer _expand _complete _approximate

This defines the default completer so will be used for your tab key.
This would allow completion to do expansion (expanding file patterns
like fred*), then normal completion and then approximate completion
(which allows for errors in what you have typed).

Above, I used ':completion:match-word::::' as the context. This
restricts the style to applying for our new match-word editor command.
So escape * now does completion but uses a different set of completers.
The important completer in this case was _match. It alone would do the
job you were asking about. The other two completers (_all_matches and
_ignored) just add some extra functionality.

The same goes for the second (match-original) style. That just allows
you to go back to the original string you had typed (such as fred*)
more easily if completion finds several matches.

I hope that makes it clearer. Completion is very configurable and this
isn't the simplist example to start with.

Oliver


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

end of thread, other threads:[~2003-12-05 14:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-04 14:54 Why doesn't cd ignore files when you type say "cd fred*" zzapper
2003-12-04 15:37 ` Peter Stephenson
2003-12-04 16:47   ` zzapper
2003-12-05 10:54     ` Oliver Kiddle
2003-12-05 11:06       ` zzapper
2003-12-05 14:27         ` Oliver Kiddle
2003-12-04 17:30 ` Bart Schaefer
2003-12-04 20:50   ` zzapper
2003-12-04 22:56     ` Pavol Juhas

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