0

I use mailmap files for some git repos. The lines in the files can be in one of these formats:

Proper Name <[email protected]>
<[email protected]> <[email protected]>
Proper Name <[email protected]> <[email protected]>
Proper Name <[email protected]> Commit Name <[email protected]>

I want to keep the lines formatted as a table with columns separated with 2 spaces, e.g.

Some Dude     <[email protected]>              <[email protected]>
Other Author  <[email protected]>           <[email protected]>
Other Author  <[email protected]>           <[email protected]>
Santa Claus   <[email protected]>  <[email protected]>

How can I make Vim reformat a mailmap file this way on save (e.g. if a new name is too large for the column size)? I guess it's possible with an autocommand like this:

autocmd BufWritePre mailmap :<reformat_cmd>

but I'm not sure how to implement the actual command.

planetp
  • 14,248
  • 20
  • 86
  • 160

1 Answers1

0

If you're on Linux you can use column. But we need to mark where each column ends first. To do it I add here # before each section using sed:

:autocmd BufWritePost mail silent :%!sed 's/^\([^<]\+\)\?*\(<[^>]\+>\)*\([^<]\+\)\?*\(<[^>]\+>\)\?/\1 \#\2 \#\3
 \#\4/' | column -t -s '\#'

You should be able to easily extend it and/or use s command directly in vim on BufWritePre.

Karol Samborski
  • 2,757
  • 1
  • 11
  • 18