Vim Tips Wiki
Register
(clean up and merge comments)
(You only need to set the editor to vim. Vim can figure out all by itself)
Line 16: Line 16:
 
With Vim, you can quickly fix those quotations to maintain a proper hard line break at a desired character width.
 
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:
+
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 filetype=mail'"
+
set editor="vim"
 
</pre>
 
</pre>
   
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>.
+
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.)</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.
+
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.
  +
  +
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:
 
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:
Line 38: Line 40:
 
===Format a text object===
 
===Format a text object===
 
*From anywhere in the paragraph, type <code>gqip</code> or <code>gqap</code>
 
*From anywhere in the paragraph, type <code>gqip</code> or <code>gqap</code>
  +
  +
===Quote the Text===
  +
* 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.
   
 
==References==
 
==References==

Revision as of 16:52, 19 November 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.).

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.

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

Quote the Text

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

References

Related plugins

Comments