I have a common situation in the shell when a command is attempted
command "/some/complex/and terrible/path" -vf --various --flags --and -- things --config-file="/some other/annoyingly/large/path/to/somewhere"
and it turns out that I'm mostly there but need to move some stuff around. Suppose I have to move the last argument all the way to the front to attempt this next.
command --config-file="/some other/annoyingly/large/path/to/somewhere" "/some/complex/and terrible/path" -vf --various --flags --and -- things
Trying to take a keystroke golf look at this, it's a dire situation. This is actually a nightmare shell command entry scenario where you kind of have to re-type (or copy&paste) almost half the command. The best hope of sanity might be to use an external editor [like this].
Even still, Vim isn't smart enough to do arg parsing (so there is no quick way to slurp out the last arg there using e.g. daW
, though it should be possible to complete the job within 8 keystrokes or so given the power of vim).
I think that this can be a decent workaround, because I do bring a lot of software to bear on editing text in vim since I use it to do all of my coding.
But what I want is if Zsh (or even Bash!) line editor could be programmed somehow to let me shove an arg around. Kind of like this: https://github.com/AndrewRadev/sideways.vim But of course, this plugin won't work on space delimited shell arguments either. And it requires vim.
I'd be happy with either a directional hop (so that I can put my cursor somewhere on the --config-file
arg and then mash HopLeft to hop the arg to the left) or a yank & paste (so i can put my cursor somewhere on the --config-file
arg, yank it out, hit home to go to the front and paste it after it).
This way we can rapidly recompose shell arguments by navigating the args as a unit rather than navigating individual characters as a unit. It would speed up general command editing a lot. I care about this a lot because I do an unusually large amount of coding directly in the shell.
If we are to take the idea further this would be implemented as an editor mode, let's call it Argument Edit Mode, where the cursor moves across args instead of characters and a more developed set of actions can be performed on args as a unit.
Let's come back down to earth now. The question is this: can I programmatically cause zsh to delete the shell arg that i'm currently on? And then paste it back in? Can I bind a key to let me hop based on actual args instead of just hopping words?
There doesn't seem to be ready-to-use binds for use with bindkey
, but it feels like there might be a way to do it if i can programmatically control the position of the cursor. I do use a plugin that can live syntax highlight my command after all. https://github.com/zsh-users/zsh-syntax-highlighting
command --last-arg --all-other-args
' can be typed as!!:0 !!:$ !!:1-
, and tab-completion will fill it in so you can verify what you're about to execute (and/or tweak with ZLE).!!:1-
(mostly remembering what1-
means and how it works) and the fact that once I tab-complete something like!!:-3
I can't go back and adjust it to be!!:-2
without back-deleting it and typing it out again. Stuff like that. It is a workable thing to do, yes. But It's just not quite there from a user experience perspective.!!:-3
doesnt provide me that, it actually provides me the command and first 3 args. I am having a hard time finding docs that go over this stuff and I do not know if it is possible to fetch an nth-from-last arg. At the end of the day, the point is that my brain works by pointing a cursor at an arg to refer to it, not by scanning the entire command to count which arg i'm looking at to type it in.!!:-3
reads as though it should be third-from-last; instead that dash means 'do a range' and with no beginning index, 0 is implied. The ZLE commandundo
will, in fact, revert tab-completion. I think this is bound toctrl-_
in emacs bindings; I'm unsure what the vi command bindingu
points to since it sure doesn't work.ctrl+_
to horizontally split a pane for tmux so it will need to be rebound