Elvish: CDPATH feature equivalent

Created on 27 Mar 2017  路  8Comments  路  Source: elves/elvish

CDPATH as implemented by other shells would be great to see in elvish - if you don't know how it works: you define a list of paths that cd will recognize, for example CDPATH=~/foo:~/devel will let you "cd bar" if the full path is ~/foo/bar without being in the parent folder.

Most helpful comment

So we are on the same page :)

I think the awesome-elvish repo can have a section for "how to replace feature X in $other_shell", and a module that implements cd with cdpath definitely belongs there.

All 8 comments

I am closing this as won't-fix for two reasons:

  1. It can be implemented easily as follows:

    cdpaths = []
    fn cd [dir]{
      for p $cdpath {
        if ?(test -d $p/$dir) {
          cd $p/$dir
          return
        }
    }
      builtin:cd $dir
    }
    
  2. I don't like the UX of this feature: if I have bar in both ~/foo and ~/devel, I can only go to one of them directly. I think a better approach is

    i. Pin both ~/foo and ~/dev (#342).
    ii. Use location mode to go to one of them and cd from there.

I agree with @xiaq that this should not be implemented by the core Elvish cd implementation. I disagree with @xiaq'a second argument against implementing the feature since that behavior is inherent in the mechanism. The resolution of external commands that might exist in more than a single directory in $PATH is no different from doing cd path that might match more than one entry in $CDPATH.

For me, Elvish's location mode has replaced every other "cd shortcut" mechanism from previous shells. As commonly-used directories float to the top of the location list, and even seldom-used ones stay in the list forever, all it takes is to go there at least once by hand, and forever after it takes no more than a few keystrokes (Ctrl-L, then a few characters from the directory name) to change into it.

Location mode is something I am probably underutilizing because nothing like it exists in any of the other shells I've used over the years and I've only recently started using elvish on a regular basis as my interactive shell. Whereas I've relied on CDPATH for thirty years. So my feelings are definitely colored by familiarity with one and not the other. I use CDPATH for one very specific scenario. On my home system it only has two directories: ~/projects/personal and ~/projects/3rd-party. This means I can cd elvish, cd vim, etc., regardless of my CWD, and "jump" to the named project. It may be that in a another week or two I'll find that location mode is an adequate substitute; especially if I remap [ctrl-L] to invoke it since I find [alt-L] awkward to type on my laptop keyboard.

@krader1961 Ctrl-L is actually the default; I assume you are using readline-binding which maps Ctrl-L to clearing the screen?

In any case, you can very easily implement the cdpath feature by writing a simple replacement cd.

I assume you are using readline-binding which maps Ctrl-L to clearing the screen?

Yes, and I should probably drop that and instead explicitly add any of the readline bindings in my rc.elv that I want to use. Alternatively, simply override the readline-binding of [ctrl-L].

you can very easily implement the cdpath feature by writing a simple replacement cd

Yes, and to reinforce my, and others, point: CDPATH should not be supported by the builtin cd. Whether something like @zzamboni's cd function should support it, so new users who really want it can just be told use github.com/zzamboni/elvish-modules/dir, is the only question.

P.S., I already find location mode superior to the mcd (menu cd) command I wrote for ksh long ago and the cdh (cd using history) I wrote for fish a few years ago.

So we are on the same page :)

I think the awesome-elvish repo can have a section for "how to replace feature X in $other_shell", and a module that implements cd with cdpath definitely belongs there.

"How to do X in Elvish" would be a great idea, but I would even give it more prominence, since most new users are not likely to find awesome-elvish right away. I would put it as a new section in the Cookbook, or even as a new page under Learn.

BTW (unrelated, but in my line of thinking) I just noticed that direnv https://direnv.net/ already has support for Elvish!

Was this page helpful?
0 / 5 - 0 ratings