I have a bad habit of writing lines of code that are too damn long, so I finally got around to setting "colorcolumn=101" and textwidth=100 in my .vimrc.
I would like to be able to retroactively apply these line width restrictions to files I've already written, and I've found that starting at the top of the file and pressing gqG sort of does the trick. It will split lines that are too long properly, BUT it also join lines together if they fit within 100 characters.
So if at first I had:
import java.io.File;
import java.io.IOException;
import java.util.Map;
It would turn into:
import java.io.File; import java.io.IOException; import java.util.Map;
I don't really want the line joining behavior though.
Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.
To just break long lines, you can do this for each line individually with gqq
. Combine this with a conditional execution only on lines longer than 100 (:help /\%v
) with :global
, like this:
:%global/\%>100v/normal! gqq
Note that this may still introduce syntax errors, e.g. when breaking lines after a // ...
comment leader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With