Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directory stack to cd #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add directory stack to cd #145

wants to merge 1 commit into from

Conversation

tramzee
Copy link

@tramzee tramzee commented Jun 25, 2019

I like to use cd - to flip back and forth between two directories.

This implements a very rudimentary directory stack that allows an easy way to return to a previous directory. It also adds a dh command that allows one to view the stack.

@dundalek
Copy link
Owner

Thanks for the contribution 👍

I need to do more research to see how the directory stack works in other shells, but from the top of my mind they usually also have popd and pushd built-ins. I will get to a deeper review hopefully during the weekend.

@dundalek
Copy link
Owner

When reading more about directory stack in other shells I also learned that cd sets the OLDPWD environment variable, which is currently notimplemented. So in case of switching to last directory we could just swap PWD and OLDPWD to enable the functionality. Supporting cd - seems useful to me.

I have a question, do you just use cd to jump to last directory or do you use the functionality to jump to a directory deeper in the stack? I try not to blindly copy every feature and personally have never used the directory stack.

Comments to the proposed implementation:

  • Try better to mimic standard builtins:
    • popd
    • pushd
    • dirs - list the stack, I would prefer to use that instead of dh for more compatibility
  • I think it would be great if the dirs just returned the sequence to make it more composable. You could then do for example dirs | (first) or dirs | (count)
  • Expansion of directory stack is not done by cd but it is a shell expansion. For example cd ~4 works to change directory, but you can also use it with any other command like ls ~4.
    -No unit tests.

Conlusion

I see two options:

  1. To support flipping back and forth between two directories with cd - we can add OLDPWD and flip it with PWD.
  2. To add proper implementation of the directory stack would require more work, and I am not sure if it is that useful.

@tramzee Would 1) be enough to cover your usage?


Sources:

@tramzee
Copy link
Author

tramzee commented Jul 1, 2019

@dundalek I use the directory stack deeper than just one. However, I would say that 90% of my usage is cd -. Having said that I can certainly live without any stack whatsoever. Completion makes changing directories easy enough without excessive amounts of typing.

@tramzee
Copy link
Author

tramzee commented Jul 1, 2019

Also, I would be willing to tale a cut at doing the full directory stack implementation (pushd, popd, and dirs).

@dundalek
Copy link
Owner

dundalek commented Jul 1, 2019

@tramzee Cool, that makes sense. I am also interested in having the full dir stack implementation, so that would be great.

FYI I refactored the built-ins a bit so that they are now defined with defcmd which is cleaner than being hardcoded. Also it should be now possible to implement more customizations in user space in ~/.closhrc. For example I think it could be an interesting experiment to try to implement the whole dir stack functionality via defcmds in ~/.closhrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants