How to build and run a shell command based on the contents of several lines in Vim?

Viewed 5752

A frequently used feature of Vim for me is filtering a file (or a selection of text) through an external command and replace the selection with the result, e.g.:

:'<,>'!sort

so

c
b
a

will be sorted and will result in

a
b
c

It's also possible to replace the current line with the return value of an external command, e.g.:

:,!ls | wc -l

will insert the number of files in the current directory (in my case e.g.:)

41

But is there a way to pass the string to a command for the shell?

As an example, this might be the content of my visual selection:

line_x
line_y
line_z

I need to execute some shell command and take each of the selected lines as one shell script parameter, e.g.:

my_bash_command line_x -c -e -f
my_bash_command line_y -c -e -f
my_bash_command line_z -c -e -f

What is the best way to do this?

3 Answers
Related