4

I tried to create a custom syntax file to read FIX messages. Note: I am certain my custom files are being loaded by Vim.

The file format is sequences of NUMBER=VALUE pairs separated by the unprintable 0x1 character. I wanted a few of the pairs (the ones with important data) to turn colors when I open the file in Vim. I don't care about which colors they turn, as long as they are all different and are easily distinguishable from each other.

Specifically, I want the tags associated with the following numbers to be highlighted: 44, 52, 54, 60, 9717.

Here's my syntax file (~/.vim/syntax.vim)

if version >= 600
    if exists("b:current_syntax")
         finish
    endif
else
    syntax clear
endif

syn case ignore

"syn match fixPrice "\|44[=][^|]\+\|"
"syn match fixSendingTime "\|52[=][^|]\+\|"
"syn match fixTransactTime "\|60[=][^|]\+\|"
"syn match fixCorrelation "\|9717[=][^|]\+\|"
"syn match fixSide "\|54[=][^|]\+\|"
syn match fixPrice "44"
syn match fixSendingTime "|52"
syn match fixTransactTime "\|60"
syn match fixCorrelation "\|9717"
syn match fixSide "\|54"

" Define the default highlighting
if version >= 508 || !exists("did_fix_syntax_inits")
        if version < 508
                let did_fix_syntax_inits = 1
                command -nargs=+ HiLink hi link <args>
        else
                command -nargs=+ HiLink hi def link <args>
        endif

        HiLink fixPrice Label
        HiLink fixSendingTime Number
        HiLink fixTransactTime Include
        HiLink fixCorrelation String
        HiLink fixSide Identifier

        delcommand HiLink
endif

let b:current_syntax = "fix"

Sample line of data:

8=FIXT.1.1^A9=380^A35=X^A49=CME^A56=0^A34=13362975^A52=20130626135401572^A1128=9^A268=3^A279=0^A269=2^A48=42025^A22=8^A270=-224^A271=1^A273=135401000^A274=2^A451=32^A1020=23^A83=523628^A5799=1^A5797=1^A279=0^A269=2^A48=17311^A22=8^A270=12356^A271=1^A273=135401000^A277=1^A451=-395^A1020=201377^A83=5671131^A5797=1^A279=0^A269=2^A48=122816^A22=8^A270=12580^A271=1^A273=135401000^A274=2^A277=1^A451=-427^A1020=31^A83=1396693^A5797=2^A75=20130626^A10=001^A

Note that the ^A characters are the 0x1 unprintable characters, you can fix this file with:

:%s/\^A/<01>/

Where <01> is entered with Ctrl+v followed by 001.

1 Answer 1

2

This should work, note that <01> is not literal text, it is the 0x01 character. You can enter it with Ctrl+v followed by 001.

if exists("b:current_syntax")
        finish
endif

syn case ignore

syn match fixPrice         "<01>44=[^<01>]*"
syn match fixSendingTime   "<01>52=[^<01>]*"
syn match fixSide          "<01>54=[^<01>]*"
syn match fixTransactTime  "<01>60=[^<01>]*"
syn match fixCorrelation   "<01>9717=[^<01>]*"

hi def link fixPrice         Label
hi def link fixSendingTime   Number
hi def link fixSide          Identifier
hi def link fixTransactTime  Include
hi def link fixCorrelation   String

let b:current_syntax = "fix"

If you copy/pasted it, you can fix it up with: :%s/<01>/<01>/g, where the first <01> is literal text, and the second is the 0x01 character.

The regexp explained:

  • <01> - match 0x01 character
  • 44= - followed by your 'magic' number and a = sign
  • [^<01>]* - followed by 0 or more of any character except the 0x01 character.

You could maybe make the text stand out more by using bold text, or different background colours. But I'll leave that up to you ;-)

I removed all of the legacy cruft for very, very old Vim versions. You don't need it.

Screenshot:

enter image description here

Your Answer

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

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