From c0666d1219a82cd4e850a30ca1a7f18294c58e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Daron?= Date: Mon, 11 Nov 2024 14:16:16 +0100 Subject: [PATCH] adding shorthands --- book/src/commands.md | 31 +++++++++++---------- helix-view/src/editor/variable_expansion.rs | 6 ++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/book/src/commands.md b/book/src/commands.md index bd7f822cf..a57769757 100644 --- a/book/src/commands.md +++ b/book/src/commands.md @@ -7,21 +7,21 @@ 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. | -| `%{dirname}` | The absolute path of the parent directory of the currently focused file. | -| `%{cwd}` | The absolute path of the current working directory of Helix. | -| `%{git_repo}` | The absolute path of the git repository helix is opened in. Fallback to `cwd` if not inside a git repository| -| `%{filename}` | The absolute path of the currently focused file. | -| `%{filename:rel}` | The relative path of the file according to `cwd` (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 `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 | -| `%{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}` or `%{b}` | The name and extension of the currently focused file. | +| `%{dirname}` or `%{d}` | The absolute path of the parent directory of the currently focused file. | +| `%{cwd}` | The absolute path of the current working directory of Helix. | +| `%{git_repo}` | The absolute path of the git repository helix is opened in. Fallback to `cwd` if not inside a git repository| +| `%{filename}` or `%{f}` | The absolute path of the currently focused file. | +| `%{filename:rel}` | The relative path of the file according to `cwd` (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 `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 | +| `%{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 @@ -29,3 +29,4 @@ Helix provides several variables that can be used when typing commands or creati # Print blame info for the line where the main cursor is. C-b = ":echo %sh{git blame -L %{linenumber} %{filename}}" ``` + diff --git a/helix-view/src/editor/variable_expansion.rs b/helix-view/src/editor/variable_expansion.rs index e50bb0ffb..ace1f1a14 100644 --- a/helix-view/src/editor/variable_expansion.rs +++ b/helix-view/src/editor/variable_expansion.rs @@ -41,14 +41,14 @@ impl Editor { last_push_end = end + 1; let value = match &input[index + 2..end] { - "basename" => doc + "basename" | "b" => doc .path() .and_then(|it| { it.file_name().and_then(|it| it.to_str()) }) .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) .to_owned(), - "filename" => doc + "filename" | "f" => doc .path() .and_then(|it| it.to_str()) .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) @@ -74,7 +74,7 @@ impl Editor { .unwrap_or(crate::document::SCRATCH_BUFFER_NAME) .to_owned() } - "dirname" => doc + "dirname" | "d" => doc .path() .and_then(|p| p.parent()) .and_then(std::path::Path::to_str)