Nvim with prose: how to set up proper `autoformat` line-wrapping

57 Views Asked by At

I'm working a Neovim config for writing prose. I want it to autoformat the current paragraph, continuously line-wrapping. Suppose I use :set formatoptions=a to accomplish this. This leads to the following annoying behavior, where █ represents the cursor:

here is some text
█

Now, if I enter insert mode and type and here is more, it puts it on the previous line:

here is some text and here is more

Instead, I want it to leave the newline like this:

here is some text
and here is more

Looking at :help fo-table, I see an interesting option:

w       Trailing white space indicates a paragraph continues in the next line.                                                 
        A line that ends in a non-white character ends a paragraph.

If I enable it with :set fo+=w, now the above example works as desired. However, now if I try manually formatting a long line with gq, it will add (actual) trailing spaces to the end of every line:

Here is a very very very very very very very very very very very very very very long line.

becomes

Here is a very very very very very very very very very very very very very_
very long line.

where _ represents a space.

I want it to not add spaces when manually formatting (as when the w option isn't used), but I also want it leave the newline intact when inserting a second line (as when w is used). How can I achieve both of these?

1

There are 1 best solutions below

0
codiggermkt On

Maybe you can just keep using gq, and have the following autocmd to get rid of the trailing spaces au BufWritePre * :let pos=winsaveview()| silent! %s/\([^[:space:]]\)\s\+$/\1/e | call histdel('search', -1) | call winrestview(pos)