Background
Vim uses ranges to specify which lines should be passed to a command. For example, :5,10
will use lines 5-10, :'a,'b
will use the lines between mark a
and mark b
, and :'<,'>
will use the lines that are selected in visual mode.
Question
Is there a way to do this with sub-line granularity? What if I want to use only part of a line instead of the whole line?
Example use case
I want to pass some text within my file into an external shell program. For example, I can use write_c
(see :help w_c
) to pass some text into cat
, which will just print it out. Let's say I have the following file:
hello
this is my file
goodbye
and I put my cursor on the m
. Then I use v to enter visual mode and fe to go to the next e
. Now my visual selection is my file
. If I then press : to enter command mode and type :w !cat
(so the entire command is :'<,'>:w !cat
), then I see the message this is my file
, when I only wanted to see my file
because the entire line that the visual selection was on got piped into cat
.
How can I select just my file
and pipe only that part into cat
?