Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convenient word wrapping for long paragraphs in vim

I am looking for a technique of writing and editing longer paragraphs in vim. Essentially, what i want is vim to behave like the html textarea where i am currently writing this question.

To some extent I get this behaviour if I set wrap and set linebreak, with j mapped to gj and k mapped to gk. However, when in visual mode, the movements are still line-wise and I know of no convenient way of selecting a part of a long line.

Additionally, I know that the textwidth option automatically breaks lines if the current column of the cursor is above a certain treshold. But if I edit the line from say the middle, the line doesnt break automatically when it gets too long.

I also know I can use gq to format a group of selected lines but it gets tiresome after a while.

What is a technique for good, automatic line wrapping in vim?

like image 690
lsund Avatar asked Oct 14 '15 10:10

lsund


People also ask

How do I wrap text in Vim?

If you want to wrap lines in a specific area, move the cursor to the text you want to format and type gq followed by the range. For example, gqq wraps the current line and gqip wraps the current paragraph. The following sets a wrap margin of 2 characters from the right window border.

What is Nowrap in Vim?

Answer: To get Vim to not wrap text, issue the "vim set nowrap" command, like this: :set nowrap. By default vim will wrap long lines in the editor, but this command changes that display, so long lines will now go off-screen to your right.

What is GQ in Vim?

In Vim, you may want to format long lines, that is, wrap long lines so the longest is, say, 80 characters. The standard approach is to set the local 'textwidth' option, then use gq to format the wanted lines. :setl tw=80 gggqG. In the above, gggqG is gg (go to the first line) then gq (format) to G (the last line).


1 Answers

If you set a in your formatoptions you might get what you whant:

:set formatoptions+=a

See: http://vimdoc.sourceforge.net/htmldoc/change.html#auto-format

...
a   Automatic formatting of paragraphs.  Every time text is inserted or
    deleted the paragraph will be reformatted.  See |auto-format|.
    When the 'c' flag is present this only happens for recognized
    comments.
...
like image 115
UlfR Avatar answered Oct 10 '22 23:10

UlfR