Vim Tips Wiki
(Undo revision 37563 by 115.112.58.163 (talk))
(/* Formatting a paragraph)
Tag: Visual edit
Line 19: Line 19:
   
 
==Formatting a paragraph==
 
==Formatting a paragraph==
You may edit a paragraph consisting of several lines. When finished editing, some lines may be shorter than required. To fix this, in normal mode, type <code>gqip</code> to format the "inner paragraph". In this context, "format" means to reflow the paragraph so that all lines are the optimal length.
 
   
  +
1.     
Alternatively, format options can be set so that a paragraph is reflowed automatically, after each change. {{help|auto-format}}
 
   
  +
2.     
An example setting for <code>formatoptions</code> (<code>fo</code>) is:
 
  +
<nowiki> </nowiki>JPMorgan will have eliminated 27,000 jobs by the
  +
<nowiki> </nowiki>year-end
  +
  +
3.     
  +
<nowiki> </nowiki>company has less work to do refinancing loans
  +
  +
4.     
  +
<nowiki> </nowiki>Many big banks, including Wells Fargo
  +
  +
JPMorgan said it expects
  +
146,000 Chase Bank jobs by the year-end
 
<pre>
 
<pre>
 
:setl fo=aw2tq
 
:setl fo=aw2tq

Revision as of 16:25, 7 November 2014

Tip 440 Printable Monobook Previous Next

created 2003 · complexity basic · author Stanislav Sitar · version 6.0


When editing a plain-text file (not a program) it can be useful to have lines automatically broken when a certain length is reached. For example, the following command sets the current buffer so that lines longer than 60 characters are broken (a newline is automatically inserted):

:setlocal textwidth=60
" Following (using abbreviations) is equivalent.
:setl tw=60

Formatting a paragraph

1.     

2.      JPMorgan will have eliminated 27,000 jobs by the year-end

3.      company has less work to do refinancing loans

4.      Many big banks, including Wells Fargo

JPMorgan said it expects 146,000 Chase Bank jobs by the year-end

:setl fo=aw2tq

Comments

 TO DO 

  • Explain what above fo does, and how to configure so paragraphs do not have the first line indented, and are separated by a blank line.
  • Discuss :setl fo+=a and :setl fo-=a.
  • Incorporate following comments.

Sometimes it is useful to keep each paragraph as one long line (that is, do not break the paragraph into lines). See these related tips:

The following sets the current buffer so that long lines are automatically wrapped on the screen, with lines only breaking at a space. The mappings move the cursor up/down by screen lines instead of by file lines. There is a space following the backslash for the 'breakat' option (the set command requires a backslash before each space). Each long line will be displayed on the screen as multiple lines.

setlocal wrap nolist linebreak breakat=\ 
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk

Automatically inserting line breaks is a disaster when editing programs, but is very good for editing a text file. With the following in your vimrc, the settings will only be applied to *.txt files:

au BufEnter *.txt setl tx ts=4 sw=4 fo+=n2a

If you make a change to a wrapped paragraph and need to re-format it, you can use gq followed by a movement over the area you want to re-format, e.g. 3j or }.

Automatic text wrapping and re-formatting with gq can work on comments lines also, provided the value of the 'comments' option is set correctly.