ex (vim) adds an extra space character to empty lines

43 Views Asked by At

I would like to do some processing with ex (vim in ex/silent mode). There is however a strange behaviour if a line is empty. An extra space character is added to that line - even in binary mode. Is it possible to eliminate that?

Check out:

$ printf "a\n1st\n\n3rd\n.\n%%p\n" | ex -b | hexdump -C
00000000  31 73 74 0a 20 0a 33 72  64 0a

Note the Ex rows in printf: a for appending text, three rows, . to close append mode, %p to print everything)

a
1st

3rd
.
%p

The expected result is

00000000  31 73 74 0a 0a 33 72 64  0a
1

There are 1 best solutions below

1
romainl On

printf doesn't seem to be where that extra space comes:

$ printf 'a\n1st\n\n3rd\n.\n%%p\n' | hexdump -C
00000000  61 0a 31 73 74 0a 0a 33  72 64 0a 2e 0a 25 70 0a |a.1st..3rd...%p.|
00000010

ex doesn't seem to be involved either, at least used in a somewhat more "normal" way:

$ printf "a\n1st\n\n3rd\n.\nwzob\nq\n" | ex | hexdump -C zob
00000000  31 73 74 0a 0a 33 72 64  0a                       |1st..3rd.|
00000009

but you are not using ex that way. You are somehow making a kind of screen dump to stdout, which might explain the appearance of that space.

Without knowing what you are actually trying to do it is hard to provide a better solution. In any case, being a full screen document-oriented program, ex is generally not suited for use in a pipe so you might want to look for a different approach.