You are not logged in.

#1 2010-05-28 12:59:58

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

[solved] zsh: make auto_cd perform cd && ls instead of just cd?

Hi,

you're probably familiar with it but in zsh (and maybe other shells) there is an option called auto_cd which performs the cd command on a specified input given that input is not a command but matches an executable directory.
Example:

 > ls                                                              [~] 14:51:42
code  down  var
 > down                                                            [~] 14:51:42
 >                                                            [~/down] 14:52:07

While this is very nice it'd be even better if it would list the content of the directory when cding to it.
I already have a substitute for my normal cd command:

cd() { cd $@ && ls }

(well, actually it's
cd() { builtin pushd $@ && ls --color=auto --group-directories-first -hF }
but i suppose that's not relevant)

which will change the directory and list it's content:

 > cd down                                                         [~] 14:55:16
kernel26-nb-2.6.34-1-i686.pkg.tar.xz
kernel26-nb-2.6.34-2-i686.pkg.tar.xz
kernel26-nb-firmware-2.6.34-1-i686.pkg.tar.xz
kernel26-nb-firmware-2.6.34-2-i686.pkg.tar.xz
kernel26-nb-headers-2.6.34-1-i686.pkg.tar.xz
kernel26-nb-headers-2.6.34-2-i686.pkg.tar.xz
 >

Any idea how i can get the same behaviour with AUTO_CD?
I guess it can be done by altering the source code of zsh and I'll look into that but maybe you know of an easier solution.

Thanks in advance,
demian

Last edited by demian (2010-05-28 17:00:02)


no place like /home
github

Offline

#2 2010-05-28 13:10:07

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: [solved] zsh: make auto_cd perform cd && ls instead of just cd?

Here's a way I thought of:

preexec() { ODIR="$(pwd)" }          
precmd() { [[ "$(pwd)" != $ODIR ]] && ls }

Offline

#3 2010-05-28 13:37:56

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: [solved] zsh: make auto_cd perform cd && ls instead of just cd?

Thanks for the quick response.
Works like a charm.

I'm also using:

cd() { builtin pushd $@ && ls --color=auto --group-directories-first -hF }
cdl() { builtin pushd $@ && ls --color=auto --group-directories-first -hlF }
cda() { builtin pushd $@ && ls --color=auto --group-directories-first -hlAF }
cdd() { builtin pushd $@ && ls --color=auto -d *(-/N) }
cdf() { builtin pushd $@ && ls --color=auto *(-.N) }
cdad() { builtin pushd $@ && ls --color=auto -d *(-/DN) }
cdaf() { builtin pushd $@ && ls --color=auto *(-.DN) }
cdbig() { builtin pushd $@ && ls --color=auto -lArSh }
cdnew() { builtin pushd $@ && ls --color=auto -lAhrt }
cdold() { builtin pushd $@ && ls --color=auto -lAht }
cdsmall() { builtin pushd $@ && ls --color=auto -lASh }

Can i get preexec to recognize these different functions and execute the appropriate ls line?
I just tried it with a case statement and $0/$1 but that doesn't seem to be the way:

precmd() {
if [[ "$(pwd)" != $ODIR ]];then
  case "$0" in
    cdl) ls -l;;
    *) ls;;
  esac
fi
}
alias cdl=cd

With that condition cdl still executes ls instead of ls -l.

Last edited by demian (2010-05-28 14:13:39)


no place like /home
github

Offline

#4 2010-05-28 14:54:27

JohannesSM64
Member
From: Norway
Registered: 2009-10-11
Posts: 623
Website

Re: [solved] zsh: make auto_cd perform cd && ls instead of just cd?

precmd doesn't know about the command you last ran. preexec does:

man zshmisc wrote:

preexec
              Executed just after a command has been read and is about to be executed.  If the history mechanism
              is active (and the line was not discarded from the history buffer), the string that the user typed
              is passed as the first argument, otherwise it is an empty string.  The actual command that will be
              executed (including expanded aliases) is passed in two different forms: the second argument  is  a
              single-line,  size-limited  version  of the command (with things like function bodies elided); the
              third argument contains the full text that is being executed.

You can put LS_USED=$1 in preexec then use that in precmd.
There is probably a cleaner way to do all this though, you should ask in #zsh freenode.

Offline

#5 2010-05-28 17:00:31

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: [solved] zsh: make auto_cd perform cd && ls instead of just cd?

Thanks, solved.
Here's the code:
http://pastebin.com/r1BqW7uX


no place like /home
github

Offline

Board footer

Powered by FluxBB