[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Wrapping lines in VI



Radim Gelner <radim.gelner@siemens.at> wrote:
>This is a newbie question to vi:
>
>I want the text at the vi to wrap nicely a the end of the line so 
>that I do not have to press Enter after every line. I set up the
>wrapmargin variable in vim. But what do I do, when I rearrange the
>text and want to wrap it again paragraph after paragraph to get
>rid of 'dents'?

It's a somewhat bizarre set of key sequences, but in command mode (this
is all vim-specific, by the way, and will probably work in other vi
clones):

  gqip - rewraps current paragraph
  gq}  - rewraps next paragraph

Since . repeats the last action, you can normally do gq}... etc. until
all your text is wrapped.

In fact, 'gq' followed by any movement command will rewrap the text from
the current position of the cursor to the line where the cursor ends up
after the movement command, so, for instance, gq2<down-arrow> will
rewrap this and the next two lines.

If you want more advanced formatting, you could use a shell escape with
/usr/bin/fmt or /usr/bin/par.

By the way, I normally tend to use textwidth rather than wrapmargin, as
it's usually more important to me to have a maximum number of characters
in a line than for the line to end at least some number of characters
from the right window border (this is important for mail and news, for
example), but your mileage may vary.

Finally, if you have a reasonably recent version of vim (5.4 or later),
you might want to look into autocommands in conjunction with filetypes.
These can give you the ability to define defaults for various types of
files with a great deal of flexibility. For example, if you put the
following lines in your ~/.vimrc:

  syntax on
  au!
  au FileType mail      set textwidth=72

... you'll find that all files are syntax-highlighted by default where
possible and all mail and news articles you edit will be wrapped to 72
characters by default.

-- 
Colin Watson                                           [cjw44@cam.ac.uk]


Reply to: