Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter visual selection?

Tags:

vim

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?

like image 945
chtenb Avatar asked Apr 24 '13 16:04

chtenb


1 Answers

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.

like image 195
Ingo Karkat Avatar answered Sep 19 '22 23:09

Ingo Karkat