NeuronQ NeuronQ - 1 month ago 11
Bash Question

vim - execute range of lines as shell, output in same buffer

Let's say I'm in a buffer like this, on line 4, I want to run line 1 to 2 and have the output in the same buffer on line 4 (where cursor is):

echo "Testing"
echo "more testing"

# and here I want the output from running lines 1 to 2

...I know I can do
1,2w !sh
to run lines 1 and 2 and have the output shown in whatever that temporary buffer is. But, how do I get into my actual buffer for later editing?

(And the same thing to work with visual mode selected text, not just with line ranges given by numbers.)


You were using :w !... (:help :w_c), but you probably want :! (:help :!):

gg       - go to top
Vj       - select the two lines
y        - yank into a buffer
4gg      - go to 4th line
V        - select it
p        - paste over it
gv       - reselect the pasted range
:!sh<CR> - execute in shell and replace

or, trusting ex commands more,


NB: !sh is in most cases equivalent to !, as ! will call your default shell.