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

2

6

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.

Tarrasa answered 10/12, 2012 at 14:23 Comment(0)
T
2

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.

Tiphanie answered 11/12, 2012 at 17:12 Comment(1)
Apologies to the guy who I just removed best answer from, but this was a really great addition to my Vim.Tarrasa
M
5

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.

Marylouisemaryly answered 10/12, 2012 at 14:50 Comment(0)
T
2

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.

Tiphanie answered 11/12, 2012 at 17:12 Comment(1)
Apologies to the guy who I just removed best answer from, but this was a really great addition to my Vim.Tarrasa

© 2022 - 2024 — McMap. All rights reserved.