Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using vim, is there a command to have pasted text automatically line-wrapped?

Tags:

text

vim

ruby

edit

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?

like image 923
steve_gallagher Avatar asked Oct 11 '12 14:10

steve_gallagher


People also ask

What is paste mode Vim?

In Python, code blocks like loops are denoted using text indentation. To avoid this from happening, you can use Vim's paste mode. When you enable paste mode, Vim will not auto-indent any text that you paste. To enable paste mode, follow this process: In Vim, ensure you are command mode by hitting the Esc key.

How do I paste above a line in Vim?

m` : set a mark in the current cursor position. o<Esc>p : create a new line below and paste the text in this line. O<Esc>P : create a new line above and paste the text in this line.

How do I paste from clipboard in Vim?

We can use the “+p and <Ctrl+r>+ in normal and command mode, respectively. However, it might be more convenient if we use the <Ctrl+Shift+v> hotkey since it does the same thing as “+p. Similarly, <Ctrl+Shift+c> will yank the text into the + register to be available for pasting outside of Vim.


3 Answers

first do a set textwidth

:set tw=80

then do gqq - for a single line

for the whole file

ggVGgqq
like image 84
srini.venigalla Avatar answered Oct 16 '22 16:10

srini.venigalla


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.

like image 20
Benj Avatar answered Oct 16 '22 17:10

Benj


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.

like image 21
blackcatweb Avatar answered Oct 16 '22 16:10

blackcatweb