Vim Tips Wiki

Fix email quotations with long lines

1,613pages on
this wiki
Revision as of 15:19, November 19, 2012 by Fritzophrenic (Talk | contribs)

Tip 273 Printable Monobook Previous Next

created 2002 · complexity basic · version 5.7

When using Vim as your editor of choice, even for email processing - as I do - it is often unpleasing how some email clients quote the email body produced by mailers such as Outlook. The lines often span across multiple visual lines and it's difficult to reply on certain parts of it.

With Vim, you can quickly fix those quotations to maintain a proper hard line break at a desired character width.

First, configure your email client to launch Vim for email replies, and to set the filetype of the message to "mail". For example, when using Mutt, put this line in your .muttrc:

set editor="vim -c 'set filetype=mail'"

Make sure when you have an email message loaded in Vim, that the value of 'formatoptions' contains both c and q, that the value of 'textwidth' is set to a good width (like 76, to insert line breaks after 76 characters), and that 'filetype' is "mail" (technically, what matters for the 'comments' option to be set properly, which setting filetype to "mail" should do for you).

If these options are not set to your liking, create a file ~/.vim/ftplugin/mail.vim and set them as you like. Assuming your vimrc turns on File type plugins, this should cause Vim to load your desired settings for any buffer with the "mail" filetype.

Now, when your quoted email is displayed in Vim, there are a few ways to fix the quoted text, all relying on the gq operator:

Format visual selection

  • Move cursor to first line of broken paragraph.
  • Press 'V' and move to the last line of the paragraph you want to fix.
  • Press 'g' and then 'q'. The marked text will wrap around to your specified textwidth (76 in our case) and the quotations will be preserved across the lines.

Movement-based format

  • Move cursor to the first line (possibly by pressing { to jump to the top of the paragraph).
  • Type gq} to format from the cursor to the end of the paragraph, gqq to format just the current line, gq4j to format 4 lines, etc.

Format a text object

  • From anywhere in the paragraph, type gqip or gqap


Related plugins


Around Wikia's network

Random Wiki