How can I pipe the contents of a register to the standard input of an external command? I intuitively tried the following, but it doesn't work (may serve to illustrate my need, though):
:"0w !some_command
If contents of the register does not happen to contain NULs then it is as simple as
call system('some_command', @r)
. Problem is not so simple once you acknowledge the fact that register contents may have NUL byte inside: in this case you need to use getreg()
and force it to produce a list form of register (this is what second 1
to getreg()
does)
call system('some_command', getreg('r', 0, 1) + (getregtype('r') isnot# 'v' ? [''] : []))
. This form will preserve NULs which may happen to live inside register. Note though that getreg('r', 0, 1)
will not have trailing newline even if you copied text in linewise mode which is why I have written + (getregtype('r') isnot# 'v' ? [''] : [])
to add it (form @r
does not have this problem).
A list form is a way to represent binary data predating the time blobs were introduced. To convert random binary data into this form you split it at NL bytes (preserving any resulting empty strings) and then replace all NUL bytes to NL.