Ivan Tomica

(Re)Wrap text in vim

Since I try to use vi/vim editor for almost everything related to editing text and I use it for writing emails too. I use mutt with vim as my text editor but I also have it set up in a way that it wraps text on 80th column:

set editor='vim -c "set textwidth=80" -c "set wrap"'

That’s great, and works great… except when you all of the sudden remember to write something in the middle of existing text, then it all falls apart.

vim-wrap

See what I mean? Alright, and here’s how it can be fixed “on the fly”.

To reformat text you can use gq vim command. This will reformat current line, to reformat multiple lines you can select them with:

v

in visual mode or you can do it with:

13gq

which will re-format 13 lines from the cursor (current line + 12 after).

UPDATE:

Igor in the comment section pointed out that you can also use:

gqap

to re-wrap paragraph and to use:

gqas

to re-wrap sentence.

About 
Sysadmin on the everlasting journey of learning. Always in search for an opportunity to prove myself and to learn something new. My addiction is learning and my main goal is to excel in every aspect of Linux/Unix system administration.

2 Comments

  1. Igor Pozgaj

    2016-07-15 - 19:37
    Reply

    “gq” also takes the usual text object, so the easier way to do this would be “gqap” (for a paragraph), or “gqas” (for a sentence). It’s way faster than using the visual mode to select the text object.

    • Ivan Tomica

      2016-07-15 - 20:51
      Reply

      Thank you for pointing this out to me Igor, I have updated an article accordingly. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *