Is it possible to pipe an unsaved buffer to an external command and then replace the buffer by the command’s output?
2 Answers
You can use the "filter" command,:!
, which like most commands can be prefixed with a range:
:{range}!cmd
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:
:%!sort
You can find the Vim documentation with :help :range!
.
-
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 -
– EldamirMay 18, 2018 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.