0

While editing files I often encounter the following task: I have a standalone script that takes input and outputs a modified version of it (could be sort, could be a sed script, could be a more complicated python script) and I want to run it on a block of text within vim.

This answer explains how to feed a text block to an external script and I assume that one can similarly read a script's output into a file using :r. But how can I do both: feed text out of vim to an external script and its output back into vim?

2

1 Answer 1

2

You can filter a block of text in vim using the normal mode commant !{motion}. For instance, to use the external sort tool on the text below (ignoring the fact that vim has it's own sort for now):

b
a
d
c

use !ip (external command !, inside paragraph), which will take you to the command line with a prepopulated command line prompt (mine looked like :.,.+4!) where you simply type whatever external tool (e.g. :.,.+4!sort) you want to send the text to as stdin. The resulting stout will replace the selected lines:

a
b
c
d
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.