Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 653
Austin R Avatar asked Dec 10 '12 14:12

Austin R


People also ask

Does vim add newline at end of file?

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.


1 Answers

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.

like image 141
Ingo Karkat Avatar answered Nov 10 '22 08:11

Ingo Karkat