I want to use a shell command (fmt in this case) to operate on every (non-)matching line of a regex in a file. I'd prefer to do it within vim! I am, nonetheless, open for a pure shell/bash/zsh solution too, since vim offers :! as well.

Is it possible to combine


with a shell command that is executed on matching lines? Something like

:g/pattern/!fmt -w 72

is not working.


I found the answer! You can use

:g!/^ /:.!fmt -w 40

or without ! to use that shell command on the found lines. This, however, will work on single lines only, so it won't remove linebreaks, just add new ones. If somebody can improve this, feel free to post how :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.