[PATCH] - vi: Append a newline to opened files that don't already end in one

David Stumph dstumph at gmail.com
Wed Jan 10 02:32:27 UTC 2024


Package: busybox
Version: master
Severity: bug

Hello all,

I found a bit of broken behavior when `busybox vi` is used on a file
that doesn't end in a newline. If you go to the last line of the file
and type "o", the new line that opens below it contains the last
character of the previous line. This shouldn't happen, and I attached
a demo text file that you can reproduce it on. The "A" command on that
line is also broken, as it desynchronizes the cursor from where text
appears.

I made a patch to fix it by adding a newline to the end of files that
don't already have one. This comes with the caveat that opening a
file, not making any edits, and saving it can append a newline. Vi,
vim, nano, and gedit already add this newline though, so this is
expected behavior.

Output of `make bloatcheck`:
function                                             old     new   delta
init_text_buffer                                     161     193     +32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 32/0)               Total: 32 bytes
    text       data        bss        dec        hex    filename
1026178      16515       1808    1044501      ff015    busybox_old
1026210      16515       1808    1044533      ff035    busybox_unstripped

Thanks,
David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vi-newline.patch
Type: text/x-patch
Size: 522 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20240109/be8be036/attachment.bin>
-------------- next part --------------
Go to the beginning of the last line with your cursor, and press 'o'. It
will move the 1 onto its own line, which is improper behavior.

You can also type "A" to append to the line, and the cursor ends up getting
desynchronized from where characters appear.

If it doesn't show the problem, you might need to truncate the ending newline
character if one got added.

This line: 1


More information about the busybox mailing list