11

CONTEXT: Part of a job I'm doing involves pasting paragraphs of text from a word doc into a ruby file.

PROBLEM: These paragraphs are getting pasted in as a single very long line of text and I have to manually insert newlines to make the lines of reasonable length.

SOLUTION: Is there a way to make the pasting function "aware" of reasonable margin limits and wrap the text when I paste it in?

steve_gallagher
  • 3,778
  • 8
  • 33
  • 51
  • 1
    Do you want to wrap the text, as in keep it one long line but make it viewable, or break the text up at some arbitrary boundary, say 70 characters? – AlG Oct 11 '12 at 14:28
  • @Al G I want an arbitrary boundary, of reasonable size, around 70 characters sounds reasonable – steve_gallagher Oct 11 '12 at 15:30

4 Answers4

15

first do a set textwidth

:set tw=80

then do gqq - for a single line

for the whole file

ggVGgqq
srini.venigalla
  • 5,137
  • 1
  • 18
  • 29
4

Sure you can do this with:

:set wrap

This will show the text as wrapped without altering the underlying structure or inserting line breaks. It's sometimes also helpful to:

:set linebreak

This causes vim to wrap without breaking words.

It's also possible to:

:set wrapmargin

Which sets how far on the right wrapping should start.

Benj
  • 31,668
  • 17
  • 78
  • 127
  • `:set wrap` doesn't really do it though if the text pasted already has internal line breaks. They won't be reformatted, and you'll end up with some short lines. – Michael Berkowski Oct 11 '12 at 14:28
  • 1
    @MichaelBerkowski The OP states that the text appears as a "single very long line of text" which suggests that internal line breaks won't be a problem. – Benj Oct 11 '12 at 14:30
  • @Benj Don't these just cause the text to wrap "visually" but not structurally. My apologies if the post wasn't clear but I want that single line of text to actually be broken up into several, separate individual lines of text, like how the paragraph looks in the word document. – steve_gallagher Oct 11 '12 at 15:12
  • @steve_gallagher Yes, this is true, Vim can handle both possibilities. Actually `:set wrap` is closer to what Word does since word doesn't insert line breaks when wrapping either. – Benj Oct 11 '12 at 15:15
4

vi, vim, and gvim support the 'ex' level commands:

:set ws wm=10

which sets a wrap margin at 10 characters from the right border and enforces a "wrap scan" - automatic wrapping as you type. This won't work for pasting text, though. For that, the 'fmt' command exists, which is native to Unix/Linux and supplied on Cygwin and GnuWin32 (see How do I get fmt-like functionality for Vim in Windows?)..

The "fmt" command provides a filter for reformatting existing text with word breaks, and it accepts a numeric flag (e.g., "-80") to specify line width. You can invoke this from within the vim editor, after pasting in the long lines.

You do:

!!fmt

to reformat a long line (keyboard shortcut for ex command ":.!fmt")

Or, to rewrap a whole paragraph:

!}fmt

from the paragraph's first line.

This should save you some time.

Community
  • 1
  • 1
blackcatweb
  • 1,003
  • 1
  • 10
  • 11
3

I typically have a need to import text and then have to wrap the whole document: I use:

:g/./normal gqq

Hope that helps.