Vim Recipes ‣ Display ‣ Working with Long Lines
Your file contains lines which are too long to fit on the screen. You find it hard to edit and view.
The :set wrap command, which should be on by default, changes how long lines are displayed. Once they reach the right margin they are broken, and continued on the line below. (To disable this behaviour: :set nowrap).
wrap only changes the way the lines are displayed, however; the file will not be changed. It inserts soft line breaks.
This means that a file containing two particularly long lines may be represented by Vim as having 5 lines, for example, after wrapping. If you try to navigate this file using the basic movement commands j would move between the two logical lines, rather than the screen lines. The solution is to prefix the movement commands with g, so gj moves down one screen line.
If using wrap, you can specify what point the line should be broken by executing :set linebreak. This uses the value of breakat to decide where to break the line. To change the characters used modify breakat.
You can use :set textwidth=width to enforce a maximum line length, after which the text is broken with a "hard" line break. Vim breaks at white space, so lines may be shorter than width. To reformat existing text according to this preference either select them visually and hit gq or, in Normal mode, you can reformat the current paragraph with gqap.
In general, you'll be better off using hard line breaks with textwidth. This removes the need to differentiate between logical lines and screen lines, and means that the file will display reasonably in any editor, even if it doesn't wrap long lines.
Traditionally, text file lines are kept under 80 characters. This is mainly a holdover from the days of terminals whose displays were limited in this way, but is still customary in many programming languages and e-mail. To enforce this restriction just :set textwidth=80.
There's another way to insert hard line breaks without specifying a maximum line length. It is called wrapmargin and wraps lines relative to the width of the terminal window. For example, :set wrapmargin=4 means that when a line is more than four characters away from the right-hand margin, it is broken. This approach is more flexible than textwidth, but has the disadvantage of producing files which will display poorly on smaller displays or when the screen is split between multiple files. Especially if you're sharing the files you produce with others, I suggest the use of textwidth instead.
textwidth takes precedence over wrapmargin. For wrapmargin to take effect textwidth must be zero, as it is by default.