Vim: how to apply an external command only to lines matching a pattern

Two of my favorite Vim features are the ability to apply standard operators to strings matching a regular expression, and the ability to filter the selection or range of strings through an external command. But can these two ideas be combined?

For example, I have a text file that I use as a laboratory notebook, with notes from different dates, separated by a dash line. I can do something like delete all dashes with something like :% g/^-/d. But let me say that I wanted to resize all text lines without touching these dashed lines.

For one paragraph, it will be something like {!}fmt. But how can this be applied to all paragraphs without a stroke? When I try, that seems logical, and just connect the two together with it :% v/^-/!fmt, it doesn't work. (Actually, it looks like he broke Vim ...)

Is there a way to combine these two ideas and only pass the strings (not) matching the pattern to an external command, for example fmt?

+4
source share
2 answers

See how the team works :global.

:global(i :v) make two passes through the buffer,

  • first marking each line that matches,
  • then by executing the given command on the marked lines.

, - Ex - , ( ), .

, , , - , , , :

:v/^-/.!fmt -72

. " " , , fmt. . , , " , , ", :

:g/^-/.,'}!fmt -72

:

+3

, ', '

, , - - ( ):

:g/\v^(-+)@!/normal V!fmt

, , recurvie

nowrapscan:

set nowrapscan

, .

:

/\v^(-+)@!

, n p

qqn:.!awk '{print $2}'^M$

awk .! , '@q' @q

let @q .= '@q'

, :

gg@q

. ,

+1

All Articles