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

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

Closed
wants to merge 1 commit into from

Conversation

cole-h
Copy link
Contributor

@cole-h cole-h commented Aug 23, 2022

Fixes: #3519.

@cole-h
Copy link
Contributor Author

cole-h commented Aug 23, 2022

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

@AceofSpades5757
Copy link
Contributor

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.

@cole-h
Copy link
Contributor Author

cole-h commented Aug 24, 2022

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

@poliorcetics
Copy link
Contributor

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

@CBenoit
Copy link
Member

CBenoit commented Sep 13, 2022

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 CBenoit added C-enhancement Category: Improvements A-helix-term Area: Helix term improvements labels Sep 13, 2022
@cole-h
Copy link
Contributor Author

cole-h commented Oct 6, 2022

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 cole-h changed the title helix-term: cd to the parent of the current document when provided no arguments helix-term: introduce cdf / change-directory-to-current-file Oct 6, 2022
@cole-h cole-h force-pushed the cd-no-args branch 2 times, most recently from 42e0f0a to c833187 Compare October 6, 2022 23:51
@kirawi kirawi added the S-waiting-on-review Status: Awaiting review from a maintainer. label Oct 6, 2022
@the-mikedavis
Copy link
Member

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

@cole-h
Copy link
Contributor Author

cole-h commented Oct 10, 2022

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

This will set the current working directory to that of the current
file's parent.
@cole-h
Copy link
Contributor Author

cole-h commented Oct 25, 2023

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

@pascalkuthe
Copy link
Member

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

@cole-h
Copy link
Contributor Author

cole-h commented Oct 25, 2023

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

@cole-h cole-h closed this Oct 25, 2023
@cole-h cole-h deleted the cd-no-args branch August 9, 2024 16:17
@cole-h
Copy link
Contributor Author

cole-h commented Aug 9, 2024

Now that #4709 has merged, here's a macro you can use to get this functionality!

[keys.normal.space]
"c" = "@:cd <C-r>%<C-w><ret>"

Pressing spacec will cd helix to the directory containing the current file!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-helix-term Area: Helix term improvements C-enhancement Category: Improvements S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

There is no way to cd to the directory of the current file
7 participants