Sample Header Ad - 728x90

zsh line editor: Can I navigate and edit by argument?

7 votes
2 answers
1465 views
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]](http://nuclearsquid.com/writings/edit-long-commands/) . 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
Asked by Steven Lu (2422 rep)
Jan 16, 2018, 08:03 PM
Last activity: May 29, 2022, 05:23 PM