Is it possible to pipe an unsaved buffer to an external command and then replace the buffer by the command’s output?


You can use the "filter" command,:!, which like most commands can be prefixed with a range:


If you prefix this with a range, it will pipe those lines to the command's stdin, and replace those lines with the program's output. In this case you want to use % as the range, which means the entire buffer. For example:


You can find the Vim documentation with :help :range!.

| improve this answer | |
  • Very helpful. I use this to format long SQL queries to be more readable, e.g. open vim, paste the raw SQL string, then inside vim, I run :%!sqlformat --reindent --keywords upper --identifiers lower - – Eldamir May 18 '18 at 6:47

Yes that should be possible. One possibility: the system() function takes as optional argument the input to be used for the external command.

| improve this answer | |

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