Giorgio Robino
Giorgio Robino

Reputation: 2265

how to align justify in vim editor?

I'd like to use vim to edit long texts (prose / no code), in a well formatted way.

Let consider this file:

lorem_original screenshot

I can use sentences, paragraphs, etc. vim's features but the long sentences formatting isa bit ugly/poor readable.

So I'd like to have a some justification alignement with strict fixed colums width (let say columns = exactly 100).

I done some tests instlalling par unix command line utility (sudo apt-get install par) and setting vim to use it (with 'justify' flag):

:set formatprg=par\ jw100

and doing vim command:

gqG

but without a perfect joy, see screenshot here:

not-perfect alkignement using par

as you see I don't have an exact justification to 100 chars. why ?! It could be depend because par do not recognize some >8bit chars (of Italian language text here) ?

In fact there is a second issue: I don't want to permanently convert the original text in an aligned elaboration that destroy original sentence (introducing blanks/new line), but i would just VIEW the text leaving the original text untouched. There is a way to do that formatted view mode ?

Upvotes: 4

Views: 2866

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

A regex to achieve this:

:%s/\([^,]\{100}\)/\1\r/g

Also could place hyphen at end-of-line split words with:

:%s/\(\w\)$\n\(\w\)/\1-\r\2/g

And so on and so forth.

Upvotes: 0

tlehman
tlehman

Reputation: 5167

To temporarily use par on your file, make sure you've saved with :w and then use :!par jw100 <% | less, it will show you a readonly scrollable 100-char justified version of your file. When you are done, hit q and then Enter

Here's an example on some Lorem Ipsum text:

enter image description here

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172530

Vim has :set wrap, and that's it. Remember, it's not a WYSIWYG editor, so viewing as justified text is out of scope.

You would indeed have to convert the physical text (back and forth). If the par utility doesn't work (I think it should deal with UTF-8 encoded text; check your $LANG value for that), you could also give the $VIMRUNTIME/macros/justify.vim script that ships with Vim a try.

Upvotes: 1

Related Questions