sponsor Vim development Vim logo Vim Book Ad

basic Tip #579: Cut&Paste; without too much newlines, eg. into WORD

 tip karma   Rating 4/6, Viewed by 991 

created:   October 6, 2003 2:43      complexity:   basic
author:   Siegfried Bublitz      as of Vim:   5.7

Sometimes I am forced to enter text into MS Word. Since this tool is not
reliable and fast enough for me, I often prefer to create the text in
VIM before and cut&paste; it into Word.
Unfortunately, Word creates newlines of its own and regards entered
newlines as paragraphs. So I created a mapping that deletes all single
newlines from a selected area but keeps multiple ones. Here it is:

vmap <C-C> "+y:let @+ = substitute(@+, "\n\n\n*", "±", "g")
\\|:let @+ = substitute(@+, "\n", " ", "g")<CR>
\\|:let @+ = substitute(@+, "±", "\\n", "g")<CR>
\\|'<


Just shortly what is does:
Copy the visual area into the selection register +, subsitute two and
more newlines but a strange symbol hopefully not contained in the area
(±), substitute single newlines by a blank and resubstitute the strange
symbol by one newline, then go back to the beginning of the selected area.

If you select now text in VIM with V, it is copied with <Ctrl-C> as
described and can be pasted normally into another application.


Siegfried

 rate this tip  Life Changing Helpful Unfulfilling 

<<Specify Range with search patterns | Switching back and forth between ViM and Visual Studio .NET >>

Additional Notes

sgovindachar at yahoo, October 6, 2003 9:43
  
Here's another way:  
Step 1:  set text width to a very large number;  the number should
             be bigger than the number of words in any para.
             Example:  set tw=5000
Step 2:  Reformat the entire text with the gq command, for
            example, by using the series of commands below
            (the first three commands below select the text, the
            last is the gq command):
            gg
            Ctrl-q
            G
            gq  
To copy to word, one can do the following:
            gg
            "*yG
The preceding copies the text into the clipboard.
Then go to word and paste using "Ctrl-v"
    
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo