Wikia

Vim Tips Wiki

Changes: Fix email quotations with long lines

Edit

Back to page

(Change <tt> to <code>, perhaps also minor tweak.)
(Vim default of 'comments' includes n:>)
 
(2 intermediate revisions by 2 users not shown)
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. 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"
 
</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 is the {{help|prefix=no|'comments'}} option to be set properly, in particular, it needs to include the <code>n:&gt;</code> flag, which unfortunately the filetype plugin "mail.vim" does not set, but which Vim does include by default)</span>.
  +
  +
The bundled mail.vim filetype sets the options {{help|prefix=no|'textwidth'}} and {{help|prefix=no|'formatoptions'}} to some reasonable defaults and also defines two maps for quoting text. 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]], (e.g. your .vimrc should contain a line like <code>filetype plugin on</code>, see {{help|ftplugins}}) this should cause Vim to load your desired settings for any buffer with the "mail" filetype. If your 'comments' option has been changed from the Vim default to no longer include <code>n:&gt;</code>, this file is the place to add it back in as well.
  +
  +
However for mutt, Vim can detect the filetype automatically and therefore load the specific filetype, since the path and filename always contains <code>/tmp/mutt-XXXX</code> where XXX is a variable string. (For other applications, you may need to call Vim and set the filetype specifically like <code>set editor="vim -c 'set filetype=mail'"</code>)
  +
  +
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>
   
----
+
===Quote the Text===
The comments setting must contain the quoting character (usually '>'). So with
+
* From visual mode simply press <code>\q</code> (assuming your default Leader key is '\' see {{help|<LocalLeader>}}) and the visually selected text will be preceeded by a '> ' using the Mapping provided in the default filetype plugin.
<pre>
 
set comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-
 
</pre>
 
   
everything works as described.
+
==References==
  +
*{{help|gq}}
  +
*{{help|object-motions}}
  +
*{{help|text-objects}}
  +
*{{help|ap}}
  +
*{{help|ip}}
   
----
+
==Related plugins==
See also Brian Medley's {{script|id=99|text=email ftplugin}} that does a very nice job on this issue.
+
*Brian Medley's {{script|id=99|text=email ftplugin}}
   
----
+
==Comments==

Latest revision as of 18:03, 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. For example, when using Mutt, put this line in your .muttrc:

set editor="vim"

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 is the 'comments' option to be set properly, in particular, it needs to include the n:> flag, which unfortunately the filetype plugin "mail.vim" does not set, but which Vim does include by default).

The bundled mail.vim filetype sets the options 'textwidth' and 'formatoptions' to some reasonable defaults and also defines two maps for quoting text. 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, (e.g. your .vimrc should contain a line like filetype plugin on, see :help ftplugins) this should cause Vim to load your desired settings for any buffer with the "mail" filetype. If your 'comments' option has been changed from the Vim default to no longer include n:>, this file is the place to add it back in as well.

However for mutt, Vim can detect the filetype automatically and therefore load the specific filetype, since the path and filename always contains /tmp/mutt-XXXX where XXX is a variable string. (For other applications, you may need to call Vim and set the filetype specifically like set editor="vim -c 'set filetype=mail'")

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 selectionEdit

  • 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 formatEdit

  • 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 objectEdit

  • From anywhere in the paragraph, type gqip or gqap

Quote the TextEdit

  • From visual mode simply press \q (assuming your default Leader key is '\' see :help <LocalLeader>) and the visually selected text will be preceeded by a '> ' using the Mapping provided in the default filetype plugin.

ReferencesEdit

Related pluginsEdit

CommentsEdit

Around Wikia's network

Random Wiki