Vim Tips Wiki
m (Selectively formatting individual lines. moved to Format only long lines: Page moved by JohnBot to improve title)
No edit summary
Tag: sourceedit
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1131
 
|id=1131
  +
|previous=1130
|title=selectively formatting individual lines.
 
  +
|next=1132
|created=February 15, 2006 10:26
+
|created=2006
 
|complexity=basic
 
|complexity=basic
|author=http://byculla.blogspot.com
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=15/6
 
|rating=15/6
  +
|category1=
|text=
 
  +
|category2=
You want to format/wrap long lines, but not touch shorter lines.
 
 
}}
  +
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 <code>gq</code> to format the wanted lines.
  +
<pre>
  +
:setl tw=80
  +
gggqG
  +
</pre>
   
  +
In the above, <code>gggqG</code> is <code>gg</code> (go to the first line) then <code>gq</code> (format) to <code>G</code> (the last line).
   
  +
That works, but it also joins consecutive short lines together although it does not join lines that are separated with an empty line. For example, this text:
Example: for each line longer than 80 chars, run that line thru the par text formatter.
 
  +
<pre>
  +
hello
  +
world
   
  +
final
:g/.\{80,\}/ .!par w70
 
  +
</pre>
   
  +
would be formatted to:
  +
<pre>
  +
hello world
   
  +
final
  +
</pre>
   
  +
To wrap long lines without affecting short lines, use:
This need arises when you cut/paste a programming snippet from your web browser into vim.
 
  +
<pre>
  +
:g/./ normal gqq
  +
</pre>
   
  +
The [[Power of g|g command]] executes <code>normal gqq</code> on each line matching <code>.</code> (any line with at least one character). The normal-mode <code>gqq</code> command then formats that line.
and you want to leave the indented code alone, but format the long explanation.
 
   
  +
An alternative would be to use the [[par text reformatter]]. The following replaces each line that is 80 or more characters with the result of running par. If the buffer contains 1000 long lines, this will call par 1000 times.
See vim tip 584, on the capabilities of par.
 
  +
<pre>
 
:g/.\{80,\}/ .!par w70
  +
</pre>
   
 
This need arises when you copy/paste a programming snippet into Vim, and you want to leave the indented code alone, but format the long explanation.
   
  +
==See also==
  +
*[[VimTip347|347 Format paragraph without changing the cursor position]]
  +
*[[VimTip440|440 Automatic formatting of paragraphs]]
  +
*[[VimTip850|850 Automatic word wrapping]]
  +
*[[VimTip989|989 Word wrap without line breaks]]
   
 
==Comments==
 
 
How is this better than <code>gq</code> with a line width of 80?
 
  +
:This IS better than gq with a line width of 80 because it prevents short lines without empty line between them from being concatenated! --February 3, 2016
 
  +
::See {{help|'formatprg'}}. You could probably get the best of both worlds. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]])
 
 
 
 
}}
 
 
== Comments ==
 
How is this better than gq with a line width of 80?
 
 
C52MKIV--AT--hotmail.com
 
, February 16, 2006 5:55
 
----
 
<!-- parsed by vimtips.py in 0.504866 seconds-->
 

Revision as of 03:20, 4 February 2016

Tip 1131 Printable Monobook Previous Next

created 2006 · complexity basic · version 6.0


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).

That works, but it also joins consecutive short lines together although it does not join lines that are separated with an empty line. For example, this text:

hello
world

final

would be formatted to:

hello world

final

To wrap long lines without affecting short lines, use:

:g/./ normal gqq

The g command executes normal gqq on each line matching . (any line with at least one character). The normal-mode gqq command then formats that line.

An alternative would be to use the par text reformatter. The following replaces each line that is 80 or more characters with the result of running par. If the buffer contains 1000 long lines, this will call par 1000 times.

:g/.\{80,\}/ .!par w70

This need arises when you copy/paste a programming snippet into Vim, and you want to leave the indented code alone, but format the long explanation.

See also

Comments

How is this better than gq with a line width of 80?

This IS better than gq with a line width of 80 because it prevents short lines without empty line between them from being concatenated! --February 3, 2016
See :help 'formatprg'. You could probably get the best of both worlds. --Fritzophrenic (talk)