Search code examples
vimerrorformat

Justifying/aligning errorformat output in Vim


I use Vim/gVim for programming in javascript (node). I have jslint wired up as makeprg in my filetype plugin. Here's errorformat:

efm=%-P%f,
        \%A%>%\\s%\\?#%*\\d\ %m,%Z%.%#Line\ %l\\,\ Pos\ %c,
        \%-G%f\ is\ OK.,%-Q

And here is the output of jslint:

routes/pcr.js
#1 'db' was used before it was defined.
db.collection('pcrs', function (err, collection) { // Line 11, Pos 5
#2 'db' was used before it was defined.
db.collection('pcrs', function (err, collection) { // Line 23, Pos 5
#3 'BSON' was used before it was defined.
collection.findOne({'_id': new BSON.ObjectID(id)}, function (err, item) { // Line 24, Pos 40

And here is the output to the quickfix window:

routes/pcr.js|11 col 5| 'db' was used before it was defined.
routes/pcr.js|23 col 5| 'db' was used before it was defined.
routes/pcr.js|24 col 40| 'BSON' was used before it was defined.

After the column number, I'd like to left pad that out to say 2 digits (I hope a file has no more than 99 errors!) so that it looks like:

routes/pcr.js|11 col  5| 'db' was used before it was defined.
routes/pcr.js|23 col  5| 'db' was used before it was defined.
routes/pcr.js|24 col 40| 'BSON' was used before it was defined.

I guess this would also affect line numbers 0-9 as well. Is it possible to conditionally pad the output?


Solution

  • :help quickfix-window mentions about reformatting of error lists.

    The following setting works for me(update):

    au BufRead quickfix setl modifiable
                \| silent exe "%!perl -ple '
                    \my ($file, $pos, $msg) = split qr{[|]}, $_, 3;
                    \my $aligned_pos = sub {
                    \  my @p = split qr{[ ]}, shift;
                    \  return                                        if @p == 0;
                    \  return sprintf q{\\%3s}, @p                   if @p == 1;
                    \  return sprintf q{\\%3s \\%s}, @p              if @p == 2;
                    \  return sprintf q{\\%3s \\%s \\%2s}, @p        if @p == 3;
                    \  return sprintf q{\\%3s \\%s \\%2s \\%-8s}, @p if @p == 4;
                    \  return join q{ }, @p;
                    \}->($pos);
                    \$_ = join q{|}, $file, $aligned_pos, $msg;
                \'"
                \| setl nomodifiable