Vi / vim - Insert a external command response

I would like to execute and external command and insert it into a particular area of the file I am editing. Note that I have the ORIGINAL AT&T vi training doc dated 1987. Doesnt explain it.

As an example, in a vi editor I have

I would like the result :

After executing the following from vi command mode :

:r!echo $HOME

I did try to join. But the external command response is joined to the end of the existing text.

Ill also look at join and yank and paste options

Thanks in advance!

Try the following sequence of commands:

/ all<carriage-return>

(where <carriage-return> is entered by hitting the "return" key) This will position the cursor on the space where you want to insert your text.

s<carriage-return><carriage-return><ESC>

(where <ESC> is entered by hitting the escape key) This will create an empty line where we can insert the output from an external command and leave the cursor on the line after the empty line we just created.

.-1!echo $HOME<carriage-return>

which will replace the contents of the empty line above where where are sitting with the output produced by the command echo $HOME and leave the cursor on the text that was just replaced.

kJJ

to move up one line and join the next two lines to that line.

Then continue with other changes you want to make to the file...

PS: Note that the above instructions assume that your cursor is located at the start of the line you mentioned in your post. If that is not where you are sitting, the initial search pattern may need to be more extensive to be sure you locate the proper spot on the proper line.