Skip to content

Add directory stack to cd#145

Open
tramzee wants to merge 1 commit into
dundalek:masterfrom
tramzee:ramset
Open

Add directory stack to cd#145
tramzee wants to merge 1 commit into
dundalek:masterfrom
tramzee:ramset

Conversation

@tramzee

@tramzee tramzee commented Jun 25, 2019

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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

tramzee commented Jul 1, 2019

Copy link
Copy Markdown
Author

@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

tramzee commented Jul 1, 2019

Copy link
Copy Markdown
Author

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

@dundalek

dundalek commented Jul 1, 2019

Copy link
Copy Markdown
Owner

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

2 participants