Skip to content

Add or remove a trailing newline

David-Apps edited this page Feb 13, 2022 · 7 revisions

Lines usually end with a newline character. So a text file usually ends with a newline character. Sometimes you might want to add or remove this newline character. You can use these functions to help you.

Add a trailing newline to the end of a buffer

# Add a trailing newline to the end of a buffer
# usage: <at
function:at {
$s/$/\n/f
$g/^$/f d
}

Remove a trailing newline from the end of a buffer

# Remove a trailing newline from the end of a buffer
# usage: <rt
function:rt {
db0
$X
# Separately quote any apostrophes on the last line of the buffer.
s/'/'"'"'/gf
r !printf ''.'
-d
}