Vim Tips Wiki
Vim Tips Wiki
Advertisement
Tip 948 Printable Monobook Previous Next

created June 8, 2005 · complexity intermediate · author Kyle Wheeler · version 5.7


I frequently get email from people who do not use format-flowed email properly. I prefer to send out "correct" email, including the stuff I'm quoting, and Vim helps me do that. I added this function to my vimrc, and now I use it when editing mail by simply typing:

Fixq

Here's the function:

function! Fixflowed()
  " save cursor position
  let pos = getpos(".")
  " add spaces to the end of every line
  silent! %s/\([^]> :]\)\ze\n>[> ]*[^> ]/\1 /g
  " remove extraneous spaces
  silent! %s/ \+\ze\n[> ]*$//
  " make sure there's only ONE space at the end of each line
  silent! %s/ \{2,}$/ /
  " fix the wockas spacing from the text
  silent! %s/^[> ]*>\ze[^> ]/& /
  " compress the wockas
  while search('^>\+ >', 'w') > 0
    s/^>\+\zs >/>/
  endwhile
  " restore the original cursor location
  call setpos('.',pos)
endfunction

It's a little sensitive because it CAN blow away ascii art (it tries not to), and it also doesn't automatically reflow paragraphs (I have no idea how to do that properly).

I also have another function that helps when inserting indented paragraphs into a format=flowed email; the idea is to remove spaces at the end of indented lines so that they won't be wrapped (quoting is not indenting):

function! Fixindented()
  " remove spaces at end of indented lines
  silent! %s/^\s.*\zs \+$//
endfunction

I set these functions up like this:

autocmd Filetype mail command! Fixq call Fixflowed()
autocmd Filetype mail autocmd BufWritePre <buffer> call Fixindented()

Hope it helps someone.

Comments[]

PAR is a lot better at this kind of thing: http://www.nicemice.net/par/


PAR is a useful tool, but is not specific to format=flowed, and so can be over-zealous in some cases. For example, it doesn't recognized the -- separator between email body and signature, and wraps it into the signature.

Advertisement