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

Re: making `cd` work unquoted



2009-07-25 11:17:22 -0700, Bart Schaefer:
> On Jul 25,  9:10am, spiralofhope wrote:
> }
> } I'm trying to make a function which will accept this:
> }   cd foo bar baz
> } and have it acted upon as:
> }   cd "foo bar baz"
> } 
> } So far, I've run into a few snags.
> } 
> } I don't see how I can make a 'cd' function which will refer to the
> } 'cd' builtin.  Not a big deal, I could call the function 'ccd'.
> 
> cd() { builtin cd "$*" }
> 
> } I can't do something simple like cd "$@"
> 
> "$@" is the same as "$1" "$2" "$3" ...
> 
> "$*" is the same as "$1 $2 $3 ..."
[...]

Strictly speaking, in "$*", the positional parameters are joined
with the first character of $IFS (or with nothing if $IFS is
empty or with a space if $IFS is unset). So that's "space"
unless $IFS is modified.

So, I'd rather do:

cd() { local IFS=' '; builtin cd "$*"; }

Another problem is that "cd" alone, to get into one's home
directory becomes cd "" which does not nothing.

cd() {
  emulate -L zsh
  [[ $# -eq 1 && -z $1 ]] || builtin cd ${(j: :)*}
}

Note that even after that,

cd foo   bar baz

won't cd into "foo   bar baz" but into "foo bar baz".

You may want to check for options as well.

It's dangerous to rebind existing commands as then you end up
getting used to it and shooting yourself in the foot when on
another system or other shell.

I'd recommand to use another name like "mycd" or "c".

Another approach could be to add a key binding that quotes
Everything from the second word (or from the first non-option
argument). So that you would visually see what happens and could
apply it to any other command.

-- 
Stephane



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