zsh-users
 help / color / mirror / code / Atom feed
* persistant Directory history?
@ 2006-11-06 16:45 chesss
  2006-11-06 17:40 ` Stephane Chazelas
  2006-11-07 14:52 ` Francisco Borges
  0 siblings, 2 replies; 14+ messages in thread
From: chesss @ 2006-11-06 16:45 UTC (permalink / raw)
  To: zsh-users

Is it possible to have some kind of directory history, taht would last
after closing the shell? As attempted here:
http://www.macosxhints.com/article.php?story=20050806202859392 - thsi
doesn't seem to work as zlogout is not executed when an interactive
shell is closed(i think)

Also some other shell seems to have this feature
from http://www.softpanorama.org/Articles/slightly_skeptical_view_on_shell.shtml
>There are also the savedirs and dirsfile shell variables that can be
set >to store the directory stack automatically on logout and restore
it on >login. The dirstack shell variable can be examined to see the
directory >stack and set to put arbitrary directories into the
directory stack.

Basically I want to save the directory stack automatically and have
ctrl+r like history..
Possible?

Thzx


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

* Re: persistant Directory history?
  2006-11-06 16:45 persistant Directory history? chesss
@ 2006-11-06 17:40 ` Stephane Chazelas
  2006-11-06 21:14   ` chesss
  2006-11-07 14:52 ` Francisco Borges
  1 sibling, 1 reply; 14+ messages in thread
From: Stephane Chazelas @ 2006-11-06 17:40 UTC (permalink / raw)
  To: chesss; +Cc: zsh-users

On Mon, Nov 06, 2006 at 10:15:26PM +0530, chesss wrote:
> Is it possible to have some kind of directory history, taht would last
> after closing the shell?
[...]

I have in ~/.zshrc:

SAVED_DIRSTACK=~/.zsh-dir-stack
[[ -f $SAVED_DIRSTACK ]] && . $SAVED_DIRSTACK
trap '
  print -r "dirstack=(${(@qq)dirstack})" > $SAVED_DIRSTACK
' EXIT

-- 
Stéphane


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

* Re: persistant Directory history?
  2006-11-06 17:40 ` Stephane Chazelas
@ 2006-11-06 21:14   ` chesss
  2006-11-06 21:50     ` Stephane Chazelas
  0 siblings, 1 reply; 14+ messages in thread
From: chesss @ 2006-11-06 21:14 UTC (permalink / raw)
  To: zsh-users

> I have in ~/.zshrc:
>
> SAVED_DIRSTACK=~/.zsh-dir-stack
> [[ -f $SAVED_DIRSTACK ]] && . $SAVED_DIRSTACK
> trap '
>   print -r "dirstack=(${(@qq)dirstack})" > $SAVED_DIRSTACK
> ' EXIT

Exactly how will this work?
What I want is a command that will show me the past directories I have
cd'ed to. including those from past shells.


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

* Re: persistant Directory history?
  2006-11-06 21:14   ` chesss
@ 2006-11-06 21:50     ` Stephane Chazelas
  2006-11-07  0:19       ` William Scott
  0 siblings, 1 reply; 14+ messages in thread
From: Stephane Chazelas @ 2006-11-06 21:50 UTC (permalink / raw)
  To: chesss; +Cc: zsh-users

On Tue, Nov 07, 2006 at 02:44:30AM +0530, chesss wrote:
> >I have in ~/.zshrc:
> >
> >SAVED_DIRSTACK=~/.zsh-dir-stack
> >[[ -f $SAVED_DIRSTACK ]] && . $SAVED_DIRSTACK
> >trap '
> >  print -r "dirstack=(${(@qq)dirstack})" > $SAVED_DIRSTACK
> >' EXIT
> 
> Exactly how will this work?
> What I want is a command that will show me the past directories I have
> cd'ed to. including those from past shells.

Those line just preserve the directory stack. If you've enabled
the completion system, all you need to do is type:

cd +<Tab>

and cycle through your past directories (you may need to set the
auto_pushd option as well).

See the "dirs" command, the auto_pushd option, the dirstack
array, the pushd/popd commands in zsh manual.

-- 
Stéphane


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

* Re: persistant Directory history?
  2006-11-06 21:50     ` Stephane Chazelas
@ 2006-11-07  0:19       ` William Scott
  2006-11-07  8:18         ` Stephane Chazelas
  0 siblings, 1 reply; 14+ messages in thread
From: William Scott @ 2006-11-07  0:19 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: chesss, zsh-users


> the completion system, all you need to do is type:
>
> cd +<Tab>
>
> and cycle through your past directories (you may need to set the
> auto_pushd option as well).
>
> See the "dirs" command, the auto_pushd option, the dirstack
> array, the pushd/popd commands in zsh manual.


Sorry to hijack, but is there a simple way to have a shared directory 
stack between several terminal sessions?  I hacked together something that 
works, but I have a bad feeling that I reinvented the wheel (and never 
quite made it round).

Thanks.

Bill Scott



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

* Re: persistant Directory history?
  2006-11-07  0:19       ` William Scott
@ 2006-11-07  8:18         ` Stephane Chazelas
  2006-11-07 14:22           ` William Scott
  0 siblings, 1 reply; 14+ messages in thread
From: Stephane Chazelas @ 2006-11-07  8:18 UTC (permalink / raw)
  To: William Scott; +Cc: chesss, zsh-users

On Mon, Nov 06, 2006 at 04:19:27PM -0800, William Scott wrote:
> 
> >the completion system, all you need to do is type:
> >
> >cd +<Tab>
> >
> >and cycle through your past directories (you may need to set the
> >auto_pushd option as well).
> >
> >See the "dirs" command, the auto_pushd option, the dirstack
> >array, the pushd/popd commands in zsh manual.
> 
> 
> Sorry to hijack, but is there a simple way to have a shared directory 
> stack between several terminal sessions?  I hacked together something that 
> works, but I have a bad feeling that I reinvented the wheel (and never 
> quite made it round).
[...]

Maybe something like:

SAVED_DIRSTACK=~/.zsh-dir-stack-shared
get_saved_dirstack() {
  [[ -r $SAVED_DIRSTACK ]] &&
    . $SAVED_DIRSTACK
}
save_dirstack() {
  print -r "dirstack=(${(@qq)dirstack})" > $SAVED_DIRSTACK
}

functions[preexec]+='
  get_saved_dirstack'
functions[chpwd]+='
  save_dirstack'

(untested).

-- 
Stéphane


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

* Re: persistant Directory history?
  2006-11-07  8:18         ` Stephane Chazelas
@ 2006-11-07 14:22           ` William Scott
  0 siblings, 0 replies; 14+ messages in thread
From: William Scott @ 2006-11-07 14:22 UTC (permalink / raw)
  To: William Scott, chesss, zsh-users

 >> Sorry to hijack, but is there a simple way to have a shared directory
>> stack between several terminal sessions?  I hacked together something
>> that
>> works, but I have a bad feeling that I reinvented the wheel (and never
>> quite made it round).
> [...]
>
> Maybe something like:
>
> SAVED_DIRSTACK=~/.zsh-dir-stack-shared
> get_saved_dirstack() {
>   [[ -r $SAVED_DIRSTACK ]] &&
>     . $SAVED_DIRSTACK
> }
> save_dirstack() {
>   print -r "dirstack=(${(@qq)dirstack})" > $SAVED_DIRSTACK
> }
>
> functions[preexec]+='
>   get_saved_dirstack'
> functions[chpwd]+='
>   save_dirstack'
>
> (untested).
>
> --
> Stéphane
>

Thanks.  That is similar enough to what I have to not make me feel like a
complete fool, but much more simple and elegant.

Bill

I'd show what I have but the server just kicked the bucket.


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

* Re: persistant Directory history?
  2006-11-06 16:45 persistant Directory history? chesss
  2006-11-06 17:40 ` Stephane Chazelas
@ 2006-11-07 14:52 ` Francisco Borges
  2006-11-07 20:19   ` chesss
  1 sibling, 1 reply; 14+ messages in thread
From: Francisco Borges @ 2006-11-07 14:52 UTC (permalink / raw)
  To: chesss; +Cc: zsh-users

: On Mon, Nov 06, 2006 at 10:15PM +0530, chesss wrote:

> Basically I want to save the directory stack automatically and have
> ctrl+r like history..
> Possible?

I really wish the zsh-devs would include something for this in Zsh.
After I started having a persistent dirstack, I really had to wonder how
I had lived without it for so long :-)

Turns out there is a lot of people with custom ways to implement it.

[...]

As Stephane pointed out, all you need to do is to somehow set the
dirstack variable when you log in.

But I rather rewrite the zsh_dir_file everytime I change dirs. instead
of only after logout. Also (making a long story short) right after
login, just setting $OLDPWD won't allow you to use

% cd -

so I force the shell to visit the first dir in the stack to be able to
have this.

#-------------------------------------------------
# http://www.zsh.org/mla/users/2006/msg00173.html
# Adding a (rather primitive) dirstack history
DIRSTACKSIZE=20
if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
    dirstack=( ${(uf)"$(< ~/.zdirs)"} )
    # "cd -" won't work after login by just setting $OLDPWD, so
    cd $dirstack[0] && cd - > /dev/null
fi
chpwd() { dirs -pl >! ~/.zdirs }
#-------------------------------------------------

My 2 cents on the topic....

Cheers,
-- 
Francisco Borges


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

* Re: persistant Directory history?
  2006-11-07 14:52 ` Francisco Borges
@ 2006-11-07 20:19   ` chesss
  2006-11-07 23:18     ` Francisco Borges
  0 siblings, 1 reply; 14+ messages in thread
From: chesss @ 2006-11-07 20:19 UTC (permalink / raw)
  To: chesss, zsh-users

Finally this worked for me  just perfect :)  . i got it from
http://www.zsh.org/mla/users/2006/msg00173.html

DIRSTACKSIZE=20
if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
dirstack=( $(< ~/.zdirs) )
popd > /dev/null
fi
precmd() {
dirs -l >! ~/.zdirs # added -l
}
cd

The only problem with the above was that a new shell would start with
the last directory. So I added the extra 'cd' at the end :D
Thanks all

On 11/7/06, Francisco Borges <f.borges@rug.nl> wrote:
> : On Mon, Nov 06, 2006 at 10:15PM +0530, chesss wrote:
>
> > Basically I want to save the directory stack automatically and have
> > ctrl+r like history..
> > Possible?
>
> I really wish the zsh-devs would include something for this in Zsh.
> After I started having a persistent dirstack, I really had to wonder how
> I had lived without it for so long :-)
>
> Turns out there is a lot of people with custom ways to implement it.
>
> [...]
>
> As Stephane pointed out, all you need to do is to somehow set the
> dirstack variable when you log in.
>
> But I rather rewrite the zsh_dir_file everytime I change dirs. instead
> of only after logout. Also (making a long story short) right after
> login, just setting $OLDPWD won't allow you to use
>
> % cd -
>
> so I force the shell to visit the first dir in the stack to be able to
> have this.
>
> #-------------------------------------------------
> # http://www.zsh.org/mla/users/2006/msg00173.html
> # Adding a (rather primitive) dirstack history
> DIRSTACKSIZE=20
> if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
>     dirstack=( ${(uf)"$(< ~/.zdirs)"} )
>     # "cd -" won't work after login by just setting $OLDPWD, so
>     cd $dirstack[0] && cd - > /dev/null
> fi
> chpwd() { dirs -pl >! ~/.zdirs }
> #-------------------------------------------------
>
> My 2 cents on the topic....
>
> Cheers,
> --
> Francisco Borges
>
>


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

* Re: persistant Directory history?
  2006-11-07 20:19   ` chesss
@ 2006-11-07 23:18     ` Francisco Borges
  2006-11-07 23:52       ` Stephane Chazelas
  2006-11-08 11:35       ` chesss
  0 siblings, 2 replies; 14+ messages in thread
From: Francisco Borges @ 2006-11-07 23:18 UTC (permalink / raw)
  To: zsh-users; +Cc: chesss

: On Wed, Nov 08, 2006 at 01:49AM +0530, chesss wrote:

> Finally this worked for me  just perfect :)  . i got it from
> http://www.zsh.org/mla/users/2006/msg00173.html

That was my first attempt (i.e. taking code from someone else ;-)) to
solve it, but there were problems with it.

> DIRSTACKSIZE=20
> if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
> dirstack=( $(< ~/.zdirs) )
> popd > /dev/null
> fi
> precmd() {
> dirs -l >! ~/.zdirs # added -l
> }
> cd
>
> The only problem with the above was that a new shell would start with
> the last directory. So I added the extra 'cd' at the end :D

Here are the caveats of that code:

1. it will fail if your directories have spaces in their names. To solve
   this you have to use "dirs -p" to put one dir per line, and use "f"
   to read them.

2. The popd is used to avoid having multiple repeated entries; but a
   beter solution is to simply use a "u" when loading the directories so
   that duplicate entries are deleted.

3. You dont need to save the dirstack after every command, only when you
   change dirs, so it's beter to use "chpwd" instead of "precmd".

Making these changes, you would have a better ;-) persistent dirstack
history with:

#----------------------------------------------------------
DIRSTACKSIZE=20
if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
    dirstack=( ${(uf)"$(< ~/.zdirs)"} )
fi
chpwd() { dirs -pl >! ~/.zdirs }
#----------------------------------------------------------

If you want $OLDPWD to work as well, then you have to include

cd $dirstack[0] && cd - > /dev/null

inside that "if" block.

Cheers,
-- 
Francisco Borges


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

* Re: persistant Directory history?
  2006-11-07 23:18     ` Francisco Borges
@ 2006-11-07 23:52       ` Stephane Chazelas
  2006-11-08 10:41         ` Francisco Borges
  2006-11-08 11:35       ` chesss
  1 sibling, 1 reply; 14+ messages in thread
From: Stephane Chazelas @ 2006-11-07 23:52 UTC (permalink / raw)
  To: zsh-users, chesss

On Wed, Nov 08, 2006 at 12:18:12AM +0100, Francisco Borges wrote:
[...]
> > dirstack=( $(< ~/.zdirs) )
[...]
> Here are the caveats of that code:
> 
> 1. it will fail if your directories have spaces in their names. To solve
>    this you have to use "dirs -p" to put one dir per line, and use "f"
>    to read them.
[...]

You're only moving the problem to directories with newline in
there names. See my approach (use ${(qq)dirstack} and ".").

-- 
Stéphane


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

* Re: persistant Directory history?
  2006-11-07 23:52       ` Stephane Chazelas
@ 2006-11-08 10:41         ` Francisco Borges
  2006-11-08 11:19           ` Stephane Chazelas
  0 siblings, 1 reply; 14+ messages in thread
From: Francisco Borges @ 2006-11-08 10:41 UTC (permalink / raw)
  To: Zsh User; +Cc: chesss

: On Tue, Nov 07, 2006 at 11:52PM +0000, Stephane Chazelas wrote:

> On Wed, Nov 08, 2006 at 12:18:12AM +0100, Francisco Borges wrote:
> > 1. it will fail if your directories have spaces in their names. To solve
> >    this you have to use "dirs -p" to put one dir per line, and use "f"
> >    to read them.
>
> You're only moving the problem to directories with newline in
> there names. See my approach (use ${(qq)dirstack} and ".").

Directories with new lines in their names... I must honestly say that I
had *never* given thought about it.

Anyway, if rc_expand_param is set, the "print -r [...]" line wont
work. So you should consider using

print -r "dirstack=(${(@qq)^^dirstack})"

to explicitly turn it off.
-- 
Francisco Borges


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

* Re: persistant Directory history?
  2006-11-08 10:41         ` Francisco Borges
@ 2006-11-08 11:19           ` Stephane Chazelas
  0 siblings, 0 replies; 14+ messages in thread
From: Stephane Chazelas @ 2006-11-08 11:19 UTC (permalink / raw)
  To: Zsh User, chesss

On Wed, Nov 08, 2006 at 11:41:18AM +0100, Francisco Borges wrote:
> : On Tue, Nov 07, 2006 at 11:52PM +0000, Stephane Chazelas wrote:
> 
> > On Wed, Nov 08, 2006 at 12:18:12AM +0100, Francisco Borges wrote:
> > > 1. it will fail if your directories have spaces in their names. To solve
> > >    this you have to use "dirs -p" to put one dir per line, and use "f"
> > >    to read them.
> >
> > You're only moving the problem to directories with newline in
> > there names. See my approach (use ${(qq)dirstack} and ".").
> 
> Directories with new lines in their names... I must honestly say that I
> had *never* given thought about it.
> 
> Anyway, if rc_expand_param is set, the "print -r [...]" line wont
> work. So you should consider using
> 
> print -r "dirstack=(${(@qq)^^dirstack})"
[...]

rc_expand_param is likely to break many things. And other
options are likely to break other things.

A better approach is probably to use emulate -L zsh, for code
you want to share with anyone that might have a different option
setting as yours.

-- 
Stéphane


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

* Re: persistant Directory history?
  2006-11-07 23:18     ` Francisco Borges
  2006-11-07 23:52       ` Stephane Chazelas
@ 2006-11-08 11:35       ` chesss
  1 sibling, 0 replies; 14+ messages in thread
From: chesss @ 2006-11-08 11:35 UTC (permalink / raw)
  To: zsh-users, chesss

> #----------------------------------------------------------
> DIRSTACKSIZE=20
> if [[ -f ~/.zdirs ]] && [[ ${#dirstack[*]} -eq 0 ]]; then
>     dirstack=( ${(uf)"$(< ~/.zdirs)"} )
> fi
> chpwd() { dirs -pl >! ~/.zdirs }

yup this works better! and I am not having any problems with that
newline thing btw. But how there be a newline 'in' a folder name?


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

end of thread, other threads:[~2006-11-08 11:35 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-06 16:45 persistant Directory history? chesss
2006-11-06 17:40 ` Stephane Chazelas
2006-11-06 21:14   ` chesss
2006-11-06 21:50     ` Stephane Chazelas
2006-11-07  0:19       ` William Scott
2006-11-07  8:18         ` Stephane Chazelas
2006-11-07 14:22           ` William Scott
2006-11-07 14:52 ` Francisco Borges
2006-11-07 20:19   ` chesss
2006-11-07 23:18     ` Francisco Borges
2006-11-07 23:52       ` Stephane Chazelas
2006-11-08 10:41         ` Francisco Borges
2006-11-08 11:19           ` Stephane Chazelas
2006-11-08 11:35       ` chesss

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