helix icon indicating copy to clipboard operation
helix copied to clipboard

helix-term: introduce cdf / change-directory-to-current-file

Open cole-h opened this issue 3 years ago • 8 comments

Fixes: https://github.com/helix-editor/helix/issues/3519.

cole-h avatar Aug 23 '22 19:08 cole-h

It's been a while since I last contributed here, so please don't be shy on telling me how I can improve / make this better :)

cole-h avatar Aug 23 '22 19:08 cole-h

IMHO Due to how Helix likes to handle things, wouldn't it make more sense to go to the project root directory, like a "home" equivalent, with the fallback being the parent of the current file? There's another pr that's working on shell expansion that I think would solve this problem a little better.

AceofSpades5757 avatar Aug 24 '22 00:08 AceofSpades5757

I don't mind if there's a command for going to the project root, but I would really like a command like this that allows me to go to the directory a file is in, and not the project root. I usually start helix from the project root anyways, so that would be a completely useless no-op in almost every case of mine.

Here's an example where I would find this supremely useful: in Nix, where relative paths are relative to the file referencing the path, it's nice to be able to make a new file just by :cd and then :o newfile.nix instead of having to type out the entire directory tree to where it should be located, e.g. :o some/long/list/of/directories/before/we/get/to/where/we/need/to/place/the/newfile.nix. Maybe slightly contrived, but hopefully communicates one of my usecases.

I'd be glad to introduce another command with this ability (cd-to-file, and cdf, maybe?) so that this other PR can do its thing with cd, if that's deemed a better idea.

If there is no interest in this functionality whatsoever, please let me know and I'll move on.

(I'll fix the docs if we decide to merge this feature either as :cd-without-args, or the potential :cd-to-file.)

cole-h avatar Aug 24 '22 14:08 cole-h

I think cd alone should go to the "home" of the project for easier mental mapping: real cd goes to $HOME, helix's cd goes to project home.

Adding a cdf is a good idea though, I have often missed it. But maybe what you're missing is more of a new command to create a file next to another, like ont, open-next-to, where the default argument would be the current file but could be any file path

poliorcetics avatar Aug 29 '22 14:08 poliorcetics

I'd be glad to introduce another command with this ability (cd-to-file, and cdf, maybe?) so that this other PR can do its thing with cd, if that's deemed a better idea.

I think it would be a useful command that we want, but I’m a bit unsure about the naming. What about cd-here?

I think cd alone should go to the "home" of the project for easier mental mapping: real cd goes to $HOME, helix's cd goes to project home.

I agree with that, it feels more natural to me. I also expect this behavior to come in handy once a command such as cd-to-file/cd-here is added.

But maybe what you're missing is more of a new command to create a file next to another, like ont, open-next-to, where the default argument would be the current file but could be any file path

I really like the open-next-to command idea too.

CBenoit avatar Sep 13 '22 16:09 CBenoit

I've rebased on the latest master and regenerated the docs. I reverted my changes to cd and instead introduce a new cdf / change-directory-to-current-file that implements my desired functionality. I'll leave the "cd without args should go to the project's root" functionality to another PR.

EDIT: ~~Happy to squash the two commits together, if you would like.~~ I squashed them anyways.

cole-h avatar Oct 06 '22 23:10 cole-h

I think the change here looks good. I wonder if it's necessary to optimize this use-case so much though? There are a few other ways that I think could be fast enough:

  • Having a register for the current filename, using :cd, inserting the filename, using C-w to delete back to the parent directory of that path, and then <ret>
  • Having a variable for the current file's parent directory and :cd $parent
  • Having a variable only for the current file and using dirname and some shell expansion to cd to that directory

the-mikedavis avatar Oct 10 '22 16:10 the-mikedavis

I'd be somewhat OK with 2. I don't really manually use registers, so 1 would be inconvenient for me. Maybe I'm misunderstanding 3, but I also wouldn't want helix to need to call out to a shell in order to support this.

An unmentioned 4th option that I would also be happy with, is allowing :cd to take a file OR a directory as an argument, and cd to the directory containing the specified file. Maybe have it locked behind a setting? I don't know if we can change the completer based on that though (or if it would just be a "new" completer that would decide which "real" completer to use based on the setting).

cole-h avatar Oct 10 '22 16:10 cole-h

Hi there! Now that 23.10 has released, is there anything I can do to help get this merged?

cole-h avatar Oct 25 '23 21:10 cole-h

Having a register for the current filename, using :cd, inserting the filename, using C-w to delete back to the parent directory of that path, and then

this has been added in the latest release, with the command expansion PR we want to implement soon it will be possible to add a keymap for this without a dedicated builtin command so I would lean towards not merging this

pascalkuthe avatar Oct 25 '23 21:10 pascalkuthe

Good to know, thanks! I'll close this and carry the patch locally until that comes about :)

cole-h avatar Oct 25 '23 21:10 cole-h