Created
December 1, 2025 20:25
-
-
Save mkatychev/86321b557259e41a742774b5a1a60ecf to your computer and use it in GitHub Desktop.
Expand enviromental variables right to left
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fill-env-var [] { | |
| let current_prompt = (commandline) | |
| let var_idx = $current_prompt | str index-of "$env." --end | |
| let head = match $var_idx { | |
| # no '$env.' was found | |
| -1 => return | |
| 0 => "" | |
| $idx => ($current_prompt | str substring ..($idx - 1)) | |
| } | |
| # 5 is the offset for f in '$env.foo` | |
| mut var = $current_prompt | str substring ($var_idx + 5).. | |
| if ($var | str length) == 0 { | |
| return | |
| } | |
| let tail = match ($var | str index-of " ") { | |
| -1 => "" | |
| $idx => { | |
| let tail = $var | str substring $idx.. | |
| $var = $var | str substring ..($idx - 1) | |
| $tail | |
| } | |
| } | |
| let var_path = $var | split words | into cell-path | |
| let val = $env | get --optional $var_path | |
| match ($val | describe) { | |
| "path" | "string" => {} | |
| _ => return | |
| } | |
| let output = [$head $val $tail] | str join | |
| commandline edit --replace $output | |
| commandline set-cursor --end | |
| } | |
| $env.config.keybindings ++= [ | |
| { | |
| name: env_vars | |
| modifier: control | |
| keycode: char_g | |
| mode: [emacs vi_insert vi_normal] | |
| event: [ | |
| {send: executehostcommand cmd: fill-env-var} | |
| ] | |
| } | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment