5
cat a.txt
echo hi
echo hu
echo ha

Q: how can I execute ex.: the second line in a.txt when it is opened with vi?

so "vi a.txt" -> then SOMEMAGICCOMMAND -> then the "echo hu" is executed as a command. How?

gasko peter
  • 5,514
  • Do you want the output to replace the command that was run? Where do you want to see the output when a command is executed? – slm Oct 19 '13 at 20:59
  • Are you using vi or vim? I realize you said vi but I just wanted to clarify. – slm Oct 20 '13 at 06:00

2 Answers2

6

Executes current line and captures the output in the file replacing the line

:. !sh

Executes lines 2 to 4 and captures the output in the file replacing those lines

:2,4 !sh

Executes the whole file and captures the output in the file replacing all lines

:% !sh

Same as above but without capturing the output in the file, just printing it

:.w !sh
:2,4w !sh
:%w !sh
forcefsck
  • 7,964
1

Assuming you're using vim

I know that if you select the line containing the text (Shift + V) and then give the command:

:!sh

It will execute the selected line and replace it with the results.

Example

You can see what happens in the demo below.

                      demo page

slm
  • 369,824