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

I have a bad habit of writing lines of code that are too long, so I finally circumvented setting "colorcolumn = 101" and textwidth = 100 in my .vimrc.

I would like to be able to retroactively apply these line width limits to the files I already wrote, and I found that starting at the top of the file and pressing gqG, this does the trick. It will split lines that are too long correctly, BUT will also merge the lines if they correspond to 100 characters.

So, if I had:

import java.io.File; import java.io.IOException; import java.util.Map; 

This would turn into:

 import java.io.File; import java.io.IOException; import java.util.Map; 

I really do not want the behavior associated with the line to continue.

+6
source share
2 answers

Alternatively, you can use an external program to format the code. I would suggest astil (http://astyle.sourceforge.net/, as well as in the ubuntu repository) in combination with the vim vim-autoformat plugin (https://github.com/Chiel92/vim-autoformat).

The latter combines the first in vim, so astil formats your code when you press gqG .

+2
source

Just to break long lines, you can do this for each line individually using gqq . Combine this with conditional execution only on lines longer than 100 ( :help /\%v ) with :global , for example:

 :%global/\%>100v/normal! gqq 

Please note that this can still lead to syntax errors, for example. when breaking lines after the comment leader // ...

+5
source

All Articles