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 command to change CWD to current buffer's directory #6616

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) mod typed;

pub use dap::*;
use helix_vcs::Hunk;
use log::warn;
pub use lsp::*;
use tokio::sync::oneshot;
use tui::widgets::Row;
Expand Down Expand Up @@ -472,6 +473,7 @@ impl MappableCommand {
record_macro, "Record macro",
replay_macro, "Replay macro",
command_palette, "Open command palette",
change_current_directory_to_buffer_directory, "Change current directory to current buffer's directory",
);
}

Expand Down Expand Up @@ -5379,3 +5381,45 @@ fn replay_macro(cx: &mut Context) {
cx.editor.macro_replaying.pop();
}));
}

fn change_current_directory_to_buffer_directory(cx: &mut Context) {
let buff_dir = doc!(cx.editor)
.path()
.and_then(|path| path.parent().map(|path| path.to_path_buf()));

let buff_dir = match buff_dir {
Some(path) => path,
None => {
cx.editor.set_error("Current buffer has no path or parent");

return;
}
};

match std::env::current_dir() {
Ok(cwd) => {
if buff_dir.display().to_string() == cwd.display().to_string() {
cx.editor.set_status(format!(
"Current working directory is already {}",
cwd.display()
));

return;
}
}
Err(e) => warn!("Couldn't get the current working directory: {e})"),
}

if let Err(e) = std::env::set_current_dir(&buff_dir) {
cx.editor.set_error(format!(
"Couldn't change the current working directory: {e}"
));

return;
}

cx.editor.set_status(format!(
"Current working directory is now {}",
buff_dir.display()
));
}