How can I stop Vim from joining lines when I use gqG?

code-formatting, vim

Solution

Alternatively, you can use an external program to format your code. I would suggest astyle (http://astyle.sourceforge.net/ and also in the ubuntu repo's), in combination with the vim plugin vim-autoformat (https://github.com/Chiel92/vim-autoformat).

The latter integrates the first into vim, such that astyle formats your code when you press `gqG`.

Problem

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.

Original source