vimtricks.wiki Concise Vim tricks, one at a time.

How do I filter text through an external program using a motion in normal mode?

Answer

!{motion}{program}

Explanation

The ! operator in normal mode lets you pipe a range of text through any external program and replace it with the output. Unlike :'<,'>!command which requires visual selection, the ! operator works with any motion or text object, making it faster for common operations.

How it works

  • ! — starts the filter operator (like d or c, it waits for a motion)
  • {motion} — defines the range of lines to filter (e.g., ip for inner paragraph, } for to next blank line, 5j for 5 lines down)
  • After pressing !{motion}, Vim drops to the command line with :.,.+Nlines! prefilled — type the program name and press Enter

Example

To sort a paragraph alphabetically:

banana
apple
cherry
date

With cursor on banana, type !ip then sort and press Enter:

apple
banana
cherry
date

Tips

  • !ip sort — sort the current paragraph
  • !ip sort -r — reverse sort the paragraph
  • !ip column -t — align columns in the paragraph
  • !! filters the current line (double ! like dd), e.g., !!date replaces the line with the current date
  • Use !i} or !ap for variations on paragraph scope
  • The external program must read from stdin and write to stdout

Next

How do I ignore whitespace changes when using Vim's diff mode?