Skip to content

Commit

Permalink
adding relative filename expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
tdaron committed Sep 12, 2024
1 parent e774706 commit 2f98016
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
26 changes: 14 additions & 12 deletions book/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions helix-view/src/editor/variable_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 2f98016

Please sign in to comment.