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 (likedorc, it waits for a motion){motion}— defines the range of lines to filter (e.g.,ipfor inner paragraph,}for to next blank line,5jfor 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!likedd), e.g.,!!datereplaces the line with the current date- Use
!i}or!apfor variations on paragraph scope - The external program must read from stdin and write to stdout