I'm editing a paragraph in a existing HTML file remotely, and it has proper indentation (by Emacs' html-mode standards) and hard line breaks where lines would exceed 80 chars or so. Like this:

    Aaaaaaaaaaaaaa bbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc dd
    eeeeeeeeeeeee ffffffffffffff gggggggggggggggggggggggggggggggg eee

I need to rewrite, say, half of the text in the paragraph, which keeping the style intact. In Emacs, I can edit the text without worrying about indentation and linebreaks, and then press one key combination, M-q, that would call fill-paragraph and put line breaks and indentation in proper places (or I can use this key combination during editing, if I'm feeling extra tidy).

Is there a way to do something similar in Vim?


First, :setl textwidth=80. After editing the text, select the edited lines in visual mode (e.g. Vjjj), then re-indent with =. Finally, re-establish the selection and re-format: gvgq.

  • Thanks, that helps. If I'm not careful with selection, gq pushes several tags together on the same line, but looks like this is the best option anyway. – dgutov Feb 23 '13 at 23:20
:set textwidth=80

Will set the line limit to 80. This article explains how to link a HTML tidy program into vim: http://vim.wikia.com/wiki/Cleanup_your_HTML

  • What if it's on a remote machine, one I don't have much access to? Otherwise I would just use Emacs. – dgutov Feb 23 '13 at 5:37
  • Could you give me the best approximation using Vim's built-in features? – dgutov Feb 23 '13 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.