4 ms·
> I have bindings that allow me to execute the current line or paragraph under the cursor in the underlying shell and then inject the result into the current bu
by AdieuToLogic 3y ago
> I have bindings that allow me to execute the current line or paragraph under the cursor in the underlying shell and then inject the result into the current buffer.
As @kps mentioned in a peer comment, using the built-in ex[0] '!' command can do what the vim script above does.
For example, to execute the line where the cursor is currently placed and replace the same line's content using POSIX shell:
:. !sh
To do the same with multiple lines I like to mark the first command, then the last, and run:
:'a,'b !sh
Where the "a" buffer is the first command and the "b" buffer is the last command in this example. There are many other ways to specify ranges too. I recommend beginning with vim's builtin help:
:help range
And going from there.
0 - https://man.freebsd.org/cgi/man.cgi?query=ex&apropos=0&sektion=0&manpath=FreeBSD+13.2-RELEASE+and+Ports&arch=default&format=html https://man.freebsd.org/cgi/man.cgi?query=ex&apropos=0&sekti...
- kps 3y ago^ What they said. (I was terse on my way to bed yesterday.) The big benefit of `!` is that, like anything else in visual mode, it works with motion operators. I often write fragments of shell, e.g. to generate something repetitive, and run it with `!!` (doubling for the current line, same as things like `dd` or `yy`), `!}` (block), or `!G' (rest of file).
- ckw 3y agoFirst, I don't want to type <shift><semicolon><period><space><shift><1><s><h><enter>. I want to type at most two characters, because I do this a lot. I chose sx, but really any good <leader><foo> combination would be fine. Note they're both on the same hand; I had pretty bad rsi for a couple years and was typing almost exclusively with my left hand. Second, :. !sh eats the command. I want the command to remain in place in the buffer. Imagine you have a buffer full of useful commands that you use repeatedly with minor tweaks, and you use the output of those commands to write other bespoke commands, and so on. Third, :. !sh leaves you sitting above the result of the command. I rarely want to be there. I want to be positioned right after the result, on a new line, like my buffer is a shell-- I see the end of the result and can immediately begin typing the next command.
- AdieuToLogic 3y ago>First, I don't want to type <shift><semicolon><period><space><shift><1><s><h><enter>. I want to type at most two characters, because I do this a lot. I chose sx, but really any good <leader><foo> combination would be fine. Note they're both on the same hand; I had pretty bad rsi for a couple years and was typing almost exclusively with my left hand. > Second, :. !sh eats the command. I want the command to remain in place in the buffer. Imagine you have a buffer full of useful commands that you use repeatedly with minor tweaks, and you use the output of those commands to write other bespoke commands, and so on. map <leader>sx yyp:.!sh<CR> > Third, :. !sh leaves you sitting above the result of the command. I rarely want to be there. I want to be positioned right after the result, on a new line, like my buffer is a shell-- I see the end of the result and can immediately begin typing the next command. map <leader>sx o<ESC>mtkyyp:.!sh<CR>'t HTH