Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Automatic formatting of paragraphs
Back to page
Edit
Edit source
View history
Talk (2)
Edit Page
Automatic formatting of paragraphs
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=440 |previous=438 |next=441 |created=2003 |complexity=basic |author=Stanislav Sitar |version=6.0 |rating=44/17 |category1= |category2= }} 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. Alternatively, format options can be set so that a paragraph is reflowed automatically, after each change. {{help|auto-format}} An example setting for <code>formatoptions</code> (<code>fo</code>) is: <pre> :setl fo=aw2tq </pre> ==Comments== {{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. <pre> setlocal wrap nolist linebreak breakat=\ nnoremap j gj nnoremap k gk vnoremap j gj vnoremap k gk </pre> ---- 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: <pre> au BufEnter *.txt setl tx ts=4 sw=4 fo+=n2a </pre> ---- 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>. 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.
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Help
(
view source
)
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Template:Todo
(
view source
)
Follow on IG
TikTok
Join Fan Lab