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?

Stefan Witzel
  • 335
  • 2
  • 10

1 Answers1

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
mattb
  • 2,787
  • 2
  • 6
  • 20