Wikia

Vim Tips Wiki

Changes: Fix email quotations with long lines

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(clean up and merge comments)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=273
 
|id=273
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
When using Vim as your editor of choice, even for email processing - as I do - it is often unpleasing how some MUAs 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.
+
__NOTOC__
  +
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 75 char break. For example, when using Mutt, put this line in your .muttrc, or use a custom .vimrc_mail for 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:
 
<pre>
 
<pre>
set editor="vim -c 'set fo=tcrq' -c 'set tw=76'"
+
set editor="vim -c 'set filetype=mail'"
 
</pre>
 
</pre>
   
For other MUAs this has to be fitted. However, now, when your quoted email is displayed, you can use this Vim sequence to fix it:
+
Make sure when you have an email message loaded in Vim, that the value of 'formatoptions' contains both <code>c</code> and <code>q</code>, 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" <span style="font-size:80%">(technically, what matters for the {{help|prefix=no|'comments'}} option to be set properly, which setting filetype to "mail" should do for you)</span>.
  +
  +
If these options are not set to your liking, create a file <code>~/.vim/ftplugin/mail.vim</code> 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 {{help|prefix=no|id=gq}} operator:
  +
  +
===Format visual selection===
 
*Move cursor to first line of broken paragraph.
 
*Move cursor to first line of broken paragraph.
 
*Press 'V' and move to the last line of the paragraph you want to fix.
 
*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.
 
*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.
   
==Comments==
+
===Movement-based format===
You can also simply use the <code>{</code> and <code>}</code> paragraph motions in conjunction with <code>gq</code>. For example, move to the top of a paragraph using <code>{</code>, then hit <code>gq}</code> to reformat it.
+
*Move cursor to the first line (possibly by pressing <code>{</code> to jump to the top of the paragraph).
  +
*Type <code>gq}</code> to format from the cursor to the end of the paragraph, <code>gqq</code> to format just the current line, <code>gq4j</code> to format 4 lines, etc.
   
----
+
===Format a text object===
Like just about everything else, <code>gq</code> will take any motion command. I find myself stepping through mails and random files with <code>:set tw=72</code> or whatnot and <code>gqj</code> and <code>J</code> and occasionally inserting stray <code>></code> characters.
+
*From anywhere in the paragraph, type <code>gqip</code> or <code>gqap</code>
   
----
+
==References==
The comments setting must contain the quoting character (usually '>'). So with
+
*{{help|gq}}
<pre>
+
*{{help|object-motions}}
set comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-
+
*{{help|text-objects}}
</pre>
+
*{{help|ap}}
  +
*{{help|ip}}
   
everything works as described.
+
==Related plugins==
  +
*Brian Medley's {{script|id=99|text=email ftplugin}}
   
----
+
==Comments==
See also Brian Medley's {{script|id=99|text=email ftplugin}} that does a very nice job on this issue.
 
 
----
 

Revision as of 15:19, November 19, 2012

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

References

Related plugins

Comments

Around Wikia's network

Random Wiki