helix
helix copied to clipboard
Add command expansions %key{body}
Based on: https://github.com/helix-editor/helix/pull/3393
%sh{body} uses shell_impl
from helix-term/src/commands.rs which cannot be accessed from helix-view, what should be done in this case?
So I think we can formalize expansion a little by moving the replacement function into the editor module in helix-view.
I personally would prefer this as a separate module so helix-view/src/editor/substitute.rs
I think editors.rs
is already a bit too much of a kitchen sink and this is pretty orthogonal so it makes more sense as its own module IMO
Like this?
// helix-view/src/editor.rs
mod variable_expansion;
pub use variable_expansion::expand_variables;
...
// helix-view/src/editor/variable_expansion.rs
pub fn expand_variables<'a>(...
yeah exactly :+1:
Awesome, looking forward to this! Can we document this somewhere, esp. the exact variable names supported?
Also, should this be marked as closing https://github.com/helix-editor/helix/issues/3134?
Checking out https://github.com/helix-editor/helix/pull/7090 I think it's a good idea for users to be able to expand the variables while in :command
mode and maybe even add the variables to the list of suggestions for autocompletion. What do you think?
I just pushed this feature into a new branch of my fork (ce-prompt).
Input inside a Prompt (command_mode, file_picker, etc) can be expanded from variables to values using Ctrl + .
https://github.com/helix-editor/helix/assets/22417151/25befd25-8e7f-4195-b458-3f7b9c4d6c6a
@ksdrar Today I updated to use this new branch and this command is not working anymore for me.
s = ':open %sh{~/.dotfiles/support/pipe.rb switch_to_spec %sh{pwd} %val{filename}}'
It opens 4 buffers:
-
%sh{~/.dotfiles/support/pipe.rb
-
switch_to_spec
-
~/my-project-pwd
-
}
I think this is looking good. @archseer had the idea of replacing the
%val{varname}
syntax with just%varname
, for example%filename
,%dirname
, etc..%sh{..}
would stay the same. I think Kakoune's syntax makes more sense for Kakoune which mostly integrates with scripting / shell. I don't forsee us needing other%X{..}
s other than maybe one for the plugin system language (though maybe this whole syntax/feature would change with the plugin system). What do you think?
I think it's better to leave it be %val{...}
to avoid issues with something like %filename.bak
or unintended replacements. Or maybe changing it to %{varname}
to avoid val
repetition.
@ksdrar Hope you are well. Is this nearly ready to go? I think it just misses some docs and implementing/discussing the-mikedavis's suggested change?
Hello @David-Else, I just pushed a commit to "cmd-exp-2" branch (it's rebased to latest changes) in my fork changing the expansion method and also uses the proposed %sh{} and %{varname} changes. Can you or someone else try it, please.
@ksdrar I tried the latest pull request here with the :sh echo %val{filename}
format and it works great. I use this script and the gh
cli command to audition pull requests:
ghpr() { GH_FORCE_TTY=100% gh pr list --limit 300 |
fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window 'down,70%' --header-lines 3 |
awk '{print $1}' |
xargs gh pr checkout; }
I am afraid I don't know how to pull in your branch https://github.com/ksdrar/helix/tree/cmd-exp-2
using git commands so it creates a new branch in my helix source code repository on my local machine. If you could tell me I would be happy to try it out... or just push the changes here and I will test them :)
@ksdrar I'll try it today. I'm a heavy user of this feature. But I'm using the previous branch, this one had some bugs the last time I tested, reported here
@ksdrar I'll try it today. I'm a heavy user of this feature. But I'm using the previous branch, this one had some bugs the last time I tested, reported here
Thanks!, I tried your command and it works fine (I just tried parsing it as I obviously don't have your ruby script). But if you have any problem, report back please :)
@ksdrar I tried the latest pull request here with the
:sh echo %val{filename}
format and it works great. I use this script and thegh
cli command to audition pull requests:ghpr() { GH_FORCE_TTY=100% gh pr list --limit 300 | fzf --ansi --preview 'GH_FORCE_TTY=100% gh pr view {1}' --preview-window 'down,70%' --header-lines 3 | awk '{print $1}' | xargs gh pr checkout; }
I am afraid I don't know how to pull in your branch
https://github.com/ksdrar/helix/tree/cmd-exp-2
using git commands so it creates a new branch in my helix source code repository on my local machine. If you could tell me I would be happy to try it out... or just push the changes here and I will test them :)
You may try the branch using the following commands.
git remote add ksdrar https://github.com/ksdrar/helix
git pull ksdrar cmd-exp-2
git switch cmd-exp-2
Or if you don't want to create a branch in your local machine (not recommended).
git remote add ksdrar https://github.com/ksdrar/helix
git merge ksdrar/cmd-exp-2
@ksdrar I followed your instructions and it works great! For example, this works perfectly:
:sh echo %{filename}:%{linenumber}
@danillos I think you are better qualified to test the expansions are all correct as per your post https://github.com/helix-editor/helix/pull/3393#issuecomment-1520996787
@ksdrar I did some tests and found these issues:
Issue 1:
When executing :sh echo %sh{echo a}
it works showing a
but the same command in a keybind t = ':sh echo %sh{echo a}'
it shows %sh{echo a}
Issue 2:
:sh echo %sh{echo %{filename} %sh{pwd}}
it works
:sh echo %sh{echo %{filename} %sh{pwd} %{filename}}
it doesn't works, it seems that any args after the inner %sh breaks it, because :sh echo %sh{echo %{filename} %{filename} %sh{pwd}}
works.
@danillos I just pushed a commit for the second issue. I tried and it was caused by not trimming the command output. Can you try it please.
About the first issue, the thing is the keybind arguments are parsed by separating the string and saving it as a vector, so the command arguments end up being ["%sh{echo", "a}"]. We would have to join the vector again and then separate it once more for it to work with the expansion. It was previously spotted by @the-mikedavis in https://github.com/helix-editor/helix/pull/6979#discussion_r1191574086. I think we should join them to be parsed.
@ksdrar Confirmed, Issue 2 was fixed.
@ksdrar Confirmed, Issue 2 was fixed.
Thanks!. I just pushed a commit addressing the first issue, it should work as expected now.
@ksdrar Issue 1 was fixed too.
But I'm not allowed to save changes, when I attempt to :w
, it shows it:
I have an issue with the latest cmd-exp-2
version. I can't use my shortcut Z = { Z = ":wq", Q = ":q!" }
, it just says quit! takes no arguments
, probably the same thing as https://github.com/helix-editor/helix/pull/6979#issuecomment-1694238823
Yeah, I just pushed another commit. Now it should work fine
Yeah, I just pushed another commit. Now it should work fine
Fixed!
Can this be used in languages.toml
? Something like formatter = { command = 'prettier', args = [ "%{filename}" ] }
? I can't really experiment with this until https://github.com/helix-editor/helix/pull/6846 is merged.
Can this be used in
languages.toml
? Something likeformatter = { command = 'prettier', args = [ "%{filename}" ] }
? I can't really experiment with this until #6846 is merged.
Not really. There's https://github.com/helix-editor/helix/pull/5626 and it's only being held back because the variable declaration syntax (%val{varname} or %{varname}, etc) is not set in stone yet afaik. I think once this PR is merged the formatter one will follow.
@ksdrar, everything is working fine on the branch cmd-exp-2
.
Currently, filename
provides the absolute path, while the dirname
command gives the absolute path up until the filename. Is it possible to obtain just the filename? Wouldn't it be more logical for the filename
to only provide the file name? We could easily create the absolute path by using dirname filename
, and get the bonus of having access to just the file name. Just an idea, I am probably missing some context here.
I just added basename
and selection
variables with the latest commit to "cmd-exp-2". But I think renaming filename
to filepath
and basename
to filename
would make their meaning clearer for some users, what do you think?