Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you enable word wrap in vim when printing

I wanted to print a simple text document and make sure words wrap on word boundaries. I tried both

set linebreak

and

set wrap

but when printing, it just breaks on the right column in the middle of words. Is this possible for printing?

like image 742
mattwright Avatar asked Feb 10 '09 17:02

mattwright


People also ask

How to turn on word wrap in Vim?

Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap. So using either: :set wrap!


1 Answers

You are creating a text file without any built-in linebreaks so each paragraph is a single "line", even though with linebreak and wrap set, it looks like they are multiple lines). This is why printing breaks at fixed places. (According to http://www.vim.org/htmldoc/various.html#printing it does not seem like you can have vim respect linebreak/wrap during print.)

To avoid this, if you want text to wrap while you are editing, do

set textwidth=70

to wrap at the 70th column. If you want your file to have long lines (e.g., so it formats fine when loaded into MS Word or something), then you will have to pre-process the text version before printing it. So, for example, you can try:

fmt file.txt | lpr

or if you have enscript installed, you should be able to try:

enscript --word-wrap file.txt

to print. An existing file can be wrapped by running in vim:

gggqG

that is, 'gg' to go to start of file and 'gqG' to reformat 'gq' from the current position (i.e. the first line) to the last line (by going to 'G'). 'gq' will respect your current textwidth setting.

like image 116
Emil Sit Avatar answered Oct 16 '22 12:10

Emil Sit