Piping vim buffers

Published 2026-09-11

During the day, I often need to perform one-off experiments to validate an idea. I dislike creating temporary files/directories and prefer using a REPL. But, REPLs are a real pain for any program more than 2-3 lines. Let's say I input the following function into the Python REPL:

>>> def some_function():
...     print('line10')
...     print('line2')
...     print('line3')
...     print('line4')
...
>>> some_function()
line10
line2
line3
line4
>>>

The first statement should be printing line1 not line10. To fix it requires me to page through the history multiple times (6! in fact) to correct/redefine the function. I could do a reverse search on each line, but that's also cumbersome. In my opinion, REPLs aren't great for editing more than a couple lines at a time. In bash, you can edit multiple lines at once using visual mode, but it get's stored as a single line in history. My current solution is to open vim with an empty file and then pipe the buffer into a command. Using the example above, I can pipe the entire buffer to python :w !python3. It will even show me the output!

line1
line2
line3
line4

Press ENTER or type command to continue