Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ZSH USERS NOTE BACKWARD COMPATIBILITY WARNING!

Better documentation of zsh changes in README
  • Loading branch information...
commit 3d896ea6c4b2b9e776212222f05671fa7f737515 1 parent 138cf7a
rupa authored
Showing with 10 additions and 1 deletion.
  1. +10 −1 README
11 README
View
@@ -3,7 +3,16 @@
* ZSH USERS BACKWARD COMPATIBILITY WARNING *
* *
* z now handles 'precmd' set up for zsh. Current users using zsh should *
-* remove the precmd function described in the Installation instructions. *
+* remove the precmd function that was described in the installation *
+* instructions for previous versions. *
+* *
+* In short, this: *
+* . /path/to/z.sh *
+* function precmd () { *
+* _z --add "$(pwd -P)" *
+* } *
+* should now just be: *
+* . /path/to/z.sh *
* *
* ZSH USERS BACKWARD COMPATIBILITY WARNING *
* *

20 comments on commit 3d896ea

Sorin Ionescu

This is how I load z in Prezto.

Is it no longer necessary?

function _z-precmd {
  _z --add "${PWD:A}"
}

autoload -Uz add-zsh-hook
add-zsh-hook precmd _z-precmd

Also, you should use "${PWD:A}" instead of a sub shell.

rupa
Owner

z now appends a function _z_precmd to the precmd_functions array (as documented http://zsh.sourceforge.net/Doc/Release/Functions.html), so it'll be run as a precmd without clobbering existing stuff (the reason I felt I couldn't automate it before was worrying about clobbering existing precmds).

I would prefer to use the $PWD variable, but pwd allows whether symlinks are resolved to be configurable, and $PWD doesn't resolve symlinks.

So yeah you should either remove that stuff, or you can also set $_Z_NO_PROMPT_COMMAND if you want to continue handling precmd yourself.

rupa
Owner

What's the :A do to $PWD?

Sorin Ionescu

Right, add-zsh-hook is a convenience function to add a bunch of hooks. In this case, it actually does precmd_functions+=(foo).

${PWD:A} resolves the full path, including symbolic links. Read the Modifiers section in man zshexpn, more specifically, a and A.

rupa
Owner

Awww, I wish bash had that. I'm going to leave things the way they are in z, but if you want that bit of extra oomph you can keep handling things manually as described above.

Thanks :)

Sorin Ionescu

BASH lacks many useful things. Zsh is bloated with quite a few useless things.

If I source the latest z, it's going to use the sub shell no matter what. I can't add my own hook since there will be two hooks.

rupa
Owner

If you export _Z_NO_PROMPT_COMMAND=1 before sourcing z, it won't do any of the precmd management, so you can manage the hook yourself.

Sorin Ionescu

Actually, since I'm using the same function name as yours, it would just redefine the function.

Why does the alias z generates have 2>&1 in it?

Sorin Ionescu

After taking a second look, that precmd function seems to only affect Zsh, not both Zsh and BASH; so, you could change it to my own.

rupa
Owner

Yours has a dash (_z-precmd) where mine has an underscore (_z_precmd), but that seems plausible, as long as you redefine it after z is sourced. And you'd have to make sure that it doesn't get appended to precmd_functions twice.

If i remember correctly, the stderr redirect is because, when doing z -l, I did a 'clever hack' and print to stderr, so that stdout remains empty, and z doesn't attempt to cd. Someone complained that it made it hard for them to pipe the output of z -l, so I redirect stderr back to stdout after the fact and everyone was happy.

rupa
Owner

Yeah it's definitely zsh only and I'm tempted to change it, but I want it to respect $_Z_NO_RESOLVE_SYMLINKS, and I cant think of anything more elegant than:


if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
  _z_precmd() {
    _z --add "${PWD}"
  }
else
  _z_precmd() {
    _z --add "${PWD:A}"
  }
fi
Sorin Ionescu

Is $PWD ever relative? You may want lowercase a, as in "${PWD:a}" to still record full paths, just not resolve symbolic links.

It may not be elegant, but there is no reason to complicate it by inserting the if inside of the function.

rupa
Owner

You're right about 'a', I'm going to go ahead and commit the thing, despite it being kind of clunky.

Sorin Ionescu

Zsh has a mathematical ternary operator.

$ print $((1?1:0))          
1
$ print $((0?1:0))
0

You could have something like bellow, which is uglier than the if statement.

_z_precmd() {
  _z --add "${PWD:$((_Z_NO_RESOLVE_SYMLINKS?a:A}"
}
rupa
Owner

Nice! In this codebase, but not in general, I prefer ugly to verbose :), so I'll probably use that in preference to the if statement. I was messing around with variable/parameter substitutions to no avail, but that's gonna work! I'll probably commit that (with the typo fixed, after some testing) sometime later, but in any case, it won't effect any changes you need to make in your project.

Thanks!

rupa
Owner

On review it looks like this doesn't work with strings:


~$ print $((0?a:A))
0
~$ print $((1?a:A)) 
0
Sorin Ionescu

I guess it only works for math. Now, I remember why I don't use it.

There is even an uglier way.

_z_precmd() {
  setopt LOCAL_OPTIONS PROMPT_PERCENT
  _z --add "${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}"
}
Sorin Ionescu

You probably should not use that even if it works. It's ugly beyond readability. Things can break. Using prompt expansion outside of prompts is complicated and finicky.

rupa
Owner

It doesn't quite work but I'm impressed at something that arguably surpasses perl at being unreadable. Super fun!:


rupa@lucy/etc% zsh
rupa@lucy/etc% echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
/etc
rupa@lucy/etc% echo ${PWD:${(%):-%($SHELL:a:A)}}                 
zsh: bad math expression: operand expected at `'
rupa@lucy/etc% _Z_NO_RESOLVE_SYMLINKS=1 echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
/etc
rupa@lucy/etc% export _Z_NO_RESOLVE_SYMLINKS=1
rupa@lucy/etc% echo ${PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
zsh: division by zero
rupa@lucy/etc% echo {PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}} 
{PWD:rivate/etc/1)}
rupa@lucy/etc% export _Z_NO_RESOLVE_SYMLINKS=
rupa@lucy/etc% echo {PWD:${(%):-%($_Z_NO_RESOLVE_SYMLINKS:a:A)}}
{PWD:}
rupa@lucy/etc% 

I'm not sure what's going on, but I'm enjoying it! And it's close ...

I have a feeling if I read the man stuff you linked I could get it, and despite that I'm a bash person, I just might.

(I'm in /etc cause it's actually a symlink to /private/etc, but I'm sure you knew that :)

Sorin Ionescu

You probably should employ quotations around some stuff.

Please sign in to comment.
Something went wrong with that request. Please try again.