No edit summary |
JohnBeckett (talk | contribs) No edit summary |
||
(24 intermediate revisions by 15 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{TipImported |
||
− | {{review}} |
||
− | {{Tip |
||
|id=440 |
|id=440 |
||
+ | |previous=438 |
||
− | |title=Automatic formatting of paragraphs |
||
+ | |next=441 |
||
− | |created= |
+ | |created=2003 |
− | |complexity= |
+ | |complexity=basic |
|author=Stanislav Sitar |
|author=Stanislav Sitar |
||
|version=6.0 |
|version=6.0 |
||
|rating=44/17 |
|rating=44/17 |
||
+ | |category1= |
||
− | |text= |
||
+ | |category2= |
||
− | When I use Vim to create plain-text files (like mail |
||
− | |||
− | messages) I like the feature that automatically makes lines |
||
− | |||
− | XY characters long. I simply type ':set tw=60' on the |
||
− | |||
− | command line, and as I type lines are broken (autorwapped) |
||
− | |||
− | before they reach 60 characters length. The problem is, |
||
− | |||
− | when I latter decide to edit the paragraph. When I delete or |
||
− | |||
− | add some words, the paragraph looks broken. |
||
− | |||
− | |||
− | |||
− | The solution is to type '<Esc>gqap' to format a paragraph or |
||
− | |||
− | make a mapping for this command. However, it annoys me to do |
||
− | |||
− | it repeatedly. |
||
− | |||
− | |||
− | |||
− | |||
− | |||
− | There is a better solution. |
||
− | |||
− | |||
− | |||
− | 1. Go to http://cream.sourceforge.net/vim.html |
||
− | |||
− | and download the latest patched Vim |
||
− | |||
− | |||
− | |||
− | 2. Install the program. |
||
− | |||
− | |||
− | |||
− | 3. See [http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:auto-format}} :help auto-format]. |
||
− | |||
− | |||
− | |||
− | I will not repeat the docs here. For the impatient, set |
||
− | |||
− | fotmatoptions to aw2tq ':set fo=aw2tq' and start typing. |
||
− | |||
− | The text flows automagically between lines as you type |
||
− | |||
− | text inside the paragraph. |
||
− | |||
− | GREAT!!! |
||
}} |
}} |
||
+ | 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): |
||
+ | <pre> |
||
+ | :setlocal textwidth=60 |
||
+ | " Following (using abbreviations) is equivalent. |
||
+ | :setl tw=60 |
||
+ | </pre> |
||
+ | ==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. |
||
− | not bad, but if you do |
||
+ | Alternatively, format options can be set so that a paragraph is reflowed automatically, after each change. {{help|auto-format}} |
||
− | set linebreak |
||
− | set breakat=\ |
||
− | set nolist |
||
+ | An example setting for <code>formatoptions</code> (<code>fo</code>) is: |
||
− | , and place a mapping: |
||
+ | <pre> |
||
+ | :setl fo=aw2tq |
||
+ | </pre> |
||
⚫ | |||
− | "move one 'corsor-line' line instead of one 'code-line' up/down |
||
+ | {{todo}} |
||
⚫ | |||
+ | *Explain what above <code>fo</code> does, and how to configure so paragraphs do not have the first line indented, and are separated by a blank line. |
||
⚫ | |||
+ | *Discuss <code>:setl fo+=a</code> and <code>:setl fo-=a</code>. |
||
⚫ | |||
+ | *Incorporate following comments. |
||
⚫ | |||
+ | *For CJK/Asian language, <code>:set fo+=m</code> will break line at double width chars too. |
||
⚫ | |||
+ | 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: |
||
+ | *[[VimTip38|38 Move cursor by display lines when wrapping]] |
||
+ | *[[VimTip989|989 Word wrap without line breaks]] |
||
+ | 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 <code>'breakat'</code> option (the <code>set</code> command requires a backslash before each space). Each long line will be displayed on the screen as multiple lines. |
||
− | you can also type and edit paragraphs, and will never need to format it (because it will keep one long line :) |
||
+ | <pre> |
||
+ | setlocal wrap nolist linebreak breakat=\ |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
− | '''Anonymous''' |
||
− | , March 10, 2003 10:41 |
||
---- |
---- |
||
+ | 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: |
||
− | Keeping paragraphs as a one long line is a pretty bad idea if the text is to be read by other people, with other tools. |
||
+ | <pre> |
||
⚫ | |||
+ | </pre> |
||
− | yeti--AT--physics.muni.cz |
||
− | , March 11, 2003 1:27 |
||
---- |
---- |
||
+ | If you make a change to a wrapped paragraph and need to re-format it, you can use <code>gq</code> followed by a movement over the area you want to re-format, e.g. <code>3j</code> or <code>}</code>. |
||
− | |||
− | This is double-edge settings. |
||
− | I found it is a disaster for editing scripts, such as vimrc and jsp, |
||
− | but very good for editing free-style text file. |
||
+ | Automatic text wrapping and re-formatting with <code>gq</code> can work on comments lines also, provided the value of the {{help|prefix=no|id='comments'}} option is set correctly. |
||
− | To get the best of both worlds, I used the following: |
||
− | |||
⚫ | |||
− | |||
− | (Only all *.txt files will use this auto thing) |
||
− | |||
− | |||
− | maxiangjiang--AT--hotmail.com |
||
− | , March 11, 2003 9:26 |
||
⚫ | |||
− | Reaction on notes. |
||
− | |||
− | There a few people that were demanding such functionality |
||
− | some time ago on the Vim mailing list. So when I saw that |
||
− | auto-format thing mentioned, I thought that some people |
||
− | might be interested. |
||
− | |||
− | Of course auto-format is only useful in very specific situations. |
||
− | Like when I type mail AND I want to have list characters switched |
||
− | on. Sometimes I want to see control characters like tabs, trailing |
||
− | spaces and line feeds, so I do ':set list' but when I do that, the |
||
− | long lines are not wraped visualy on word boundaries. For me, |
||
− | this is the most annoying feature in Vim. I like to have list enabled |
||
− | all the time AND I would like to see long lines wraped properly at |
||
− | the same time. This is just a dirty workaround. |
||
− | |||
− | Stano |
||
− | |||
− | Stanislav Sitar |
||
− | , March 12, 2003 2:02 |
||
− | ---- |
||
− | <!-- parsed by vimtips.py in 0.558110 seconds--> |
Revision as of 03:59, 27 August 2018
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
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 gqip
to format the "inner paragraph". In this context, "format" means to reflow the paragraph so that all lines are the optimal length.
Alternatively, format options can be set so that a paragraph is reflowed automatically, after each change. :help auto-format
An example setting for formatoptions
(fo
) is:
: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.
- For CJK/Asian language,
:set fo+=m
will break line at double width chars too.
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.