How to filter visual selection?

vim

Solution

The `:!` filter command always works on entire lines (this is as old as the original vi, embedding Ex commands that start with `:`), and the `'<,'>` range also only specifies lines.

You can use the venerable vis plugin to achieve your result; it offers a `:B` command that limits the following command to the visually selected text.

:'<,'>B !echo foobar

The plugin works for all kind of Ex commands; for `:substitute` within the selection you can also use the special `\%V` atom, though.

Problem

Suppose we have some nice bunch of text: ``` Hello world ``` and we select the `world` in visual mode. Then we could filter the visual selection using a shiny command like this one: ``` :'<,'>!echo foobar ``` However, that would replace the entire line with `foobar` instead of just the `world`. Why is this, and how can we just replace the `world`?

Original source