Here's a line from an SQL dump with randomized contents:

REPLACE INTO `drxneuleqtx` (`qd`, `mva`, `cuqtgwm`, `vatoj`, `mrfdgnaheui`, `pacc`, `aaqfjhkv`, `epue`, `kvaerg`, `xevtan`, `vmvwwgci`, `hhgvedhjv`, `omndbwf`, `butbjsjrey`, `nwyxtnox`, `aslfh`, `wvtkpyn`, `ywidifrf`, `uxvjfpamlhoa`, `qfmlpisc`, `hlpgj`, `hioplo`, `gebqth`, `bovhklyrphx`, `dijqxqxodwr`, `rdqynsve`, `loxcqfybkdm`, `sakpxfsyqkwi`, `pfypefxwfp`, `exuqiufco`, `kdprjngn`, `bnjfdcws`, `vumrbkeh`, `avgamdde`, `ceewufkv`, `xtvikcuw`, `jeswerey`, `ygqhghq`, `svygrhkw`) 
VALUES (506,'xyjnm','rsrongvucegfcbnpqplcdbfef','kcgottkurypcathxiqte','tctwpmfwkhqojyatkrfbmwvsd',3094,NULL,56806,149,NULL,18.258930,10.362209,0,9,NULL,NULL,NULL,0,0,0,NULL,488,-422,0,0,NULL,NULL,NULL,0,NULL,'lbwyshaxdtpmvbkggwreaaqd','xiyunrgjvgxgwvmyhuuunjfr','wbkyuwaykgostnowratnjmov','mjxgjnbvadhckdmonouhkwgm','wefcoaehlbinlhejgjstfwtk','fnodw','chotb',NULL,'oqidxyqfhtksxbvmstiqlsgayntgveyymrdpbybdstdehhmyircimpexwoqaimrveuwtbvbxcqebgcpfandjpnoevsdkboqyrdxhjylfrrqakcfkwdhiovlensossbttufhvmyxugixmkcgwpvjddcgpdwmytmuhju');   

As you can see the first line lists the field names and the VALUES line lists the values for the relevant fields, but I only used two lines here for easier explanation, in the dump file the two lines above are on a single line.

For easier reading of the dump I'd like it to be automatically colored when the dump file is opened, so that each field name has a different background color and each value has a different background color, but the value and the corresponding field has the same background color.

Note that the number of fields/values can change, depending on the database dump, so it's not fixed. Also, the string values are delimited with single quotes, but the strings can contain single quotes, in which case they are escaped with a backslash: 'stu\'ff'

What is the simplest way to implement this?

1 Answer 1


First, define a major mode that treats single quotes like string delimiters, and backslash as escape character.

Secondly, define font-lock rules for REPLACE INTO and VALUES. You can use a so-called "anchored" rule (a two-layer rule) where the outer rule match the keyword and the inner each string. The inner rule decide which color to use for the strings, which is a generic lisp expression, so you can iterate over a list of predefined colors.

Having said this, font-lock rules are hard to get right. Start by looking at similar code and use good tools, like font-lock-studio. A good starting point is a presentation I held some years ago, font-lock-introduction, at an Emacs meetup in Stockholm.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.