From 2f98016038fa03ef24a0f7ff44bbbb6a4b960fcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Daron?= Date: Thu, 12 Sep 2024 08:35:05 +0200 Subject: [PATCH] adding relative filename expansion --- book/src/commands.md | 26 +++++++++++---------- helix-view/src/editor/variable_expansion.rs | 21 +++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/book/src/commands.md b/book/src/commands.md index 6cdcfce2a..8a6ba8bdb 100644 --- a/book/src/commands.md +++ b/book/src/commands.md @@ -7,18 +7,20 @@ Command mode can be activated by pressing `:`. The built-in commands are: ## Using variables in typed commands and mapped shortcuts Helix provides several variables that can be used when typing commands or creating custom shortcuts. These variables are listed below: -| Variable | Description | -| --- | --- | -| `%{basename}` | The name and extension of the currently focused file. | -| `%{filename}` | The absolute path of the currently focused file. | -| `%{ext}` | The extension of the current file | -| `%{lang}` | The language of the current file | -| `%{dirname}` | The absolute path of the parent directory of the currently focused file. | -| `%{cwd}` | The absolute path of the current working directory of Helix. | -| `%{linenumber}` | The line number where the primary cursor is positioned. | -| `%{cursorcolumn}`| The position of the primary cursor inside the current line. | -| `%{selection}` | The text selected by the primary cursor. | -| `%sh{cmd}` | Executes `cmd` with the default shell and returns the command output, if any. | +| Variable | Description | +| --- | --- | +| `%{basename}` | The name and extension of the currently focused file. | +| `%{filename}` | The absolute path of the currently focused file. | +| `%{filename:rel}` | The relative path of the file according to the current working directory (will give absolute path if the file is not a child of the current working directory) | +| `%{filename:git_rel}` | The relative path of the file according to the git repo, or current working directory if not inside a git repo. (will give absolute path if the file is not a child of the git directory or the cwd) | +| `%{ext}` | The extension of the current file | +| `%{lang}` | The language of the current file | +| `%{dirname}` | The absolute path of the parent directory of the currently focused file. | +| `%{cwd}` | The absolute path of the current working directory of Helix. | +| `%{linenumber}` | The line number where the primary cursor is positioned. | +| `%{cursorcolumn}` | The position of the primary cursor inside the current line. | +| `%{selection}` | The text selected by the primary cursor. | +| `%sh{cmd}` | Executes `cmd` with the default shell and returns the command output, if any. | ### Example ```toml diff --git a/helix-view/src/editor/variable_expansion.rs b/helix-view/src/editor/variable_expansion.rs index ec16dc672..8accfb064 100644 --- a/helix-view/src/editor/variable_expansion.rs +++ b/helix-view/src/editor/variable_expansion.rs @@ -51,6 +51,27 @@ impl Editor { .and_then(|it| it.to_str()) .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) .to_owned(), + "filename:git_rel" => { + // This will get git repo root or cwd if not inside a git repo. + let workspace_path = helix_loader::find_workspace().0; + doc.path() + .and_then(|p| { + p.strip_prefix(workspace_path) + .unwrap_or(p) + .to_str() + }) + .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) + .to_owned() + } + "filename:rel" => { + let cwd = helix_stdx::env::current_working_dir(); + doc.path() + .and_then(|p| { + p.strip_prefix(cwd).unwrap_or(p).to_str() + }) + .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) + .to_owned() + } "dirname" => doc .path() .and_then(|p| p.parent())