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?
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.
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.
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.
first do a set textwidth
:set tw=80
then do gqq
- for a single line
for the whole file
ggVGgqq
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With