1

I would appreciate some help writing a simple syntax file. I have files with the following header:

Job Number: 20030003    Date: October 28, 2020  Time: 01:36:00  
Vessel: Enterprise  
Client:  Shell  Area: Atlantic Shores Offshore Windfarm  
Lat: 40 22.6625  Lon: 7-34 02 06.2200  Y:  2358609.81  X:  483116.09

   0.15 1511.41  19.38  
   0.45 1513.60  18.95  
   0.75 1513.15  18.44  
   1.05 1512.62  18.18  
   1.35 1512.58  18.17  
   1.65 1512.62  18.18  

My keywords are:

Job Number  
Date  
Time  
Vessel  
Client  
Area  
Lat  
Lon  
Y  
X

I have those defined, but I'm having trouble defining the values. The problems are:

  1. The values may have spaces in them
  2. The values may occur at the end of a line
  3. Other than values at the end of a line, the value terminator may either be two spaces, a tab and a space, or a single tab.
  4. My syntax file is highlighting the columns of numbers below my header.

Here is my current syntax file:

" Quit when a syntax file was already loaded.  
if exists('b:current_syntax') | finish|  endif  
  
syntax match velVar "Client" nextgroup=velAssignment  
syntax match velVar "Vessel" nextgroup=velAssignment  
syntax match velVar "Area" nextgroup=velAssignment  
syntax match velVar "Date" nextgroup=velAssignment  
syntax match velVar "Time" nextgroup=velAssignment  
syntax match velVar "Job Number" nextgroup=velAssignment  
syntax match velVar "Lat" nextgroup=velAssignment  
syntax match velVar "Lon" nextgroup=velAssignment  
syntax match velVar "Y" nextgroup=velAssignment  
syntax match velVar "X" nextgroup=velAssignment  
syntax match velAssignment ":" contained nextgroup=velValue  
syntax match velValue " [ -.,:[:alnum:]]*$" nextgroup=velNum  
syntax match velValue " [ -.,:[:alnum:]]*  " nextgroup=velNum  
syntax match velValue " [ -.,:[:alnum:]]*^I" nextgroup=velNum  
syntax match velValue " [ -.,:[:alnum:]]*^I " nextgroup=velNum  
syntax match velNum^I"^ *[:digit:]*$"  
  
hi def link velVar Identifier  
hi def link velAssignment Statement  
hi def link velValue String  
  
let b:current_syntax = 'vel'  
4
  • I've managed to get my values highlighted, but now it's extending down into my data... Is there any way to match EVERYTHING after line 5 and have that be a separate color? – Sam Alleman Dec 14 '20 at 18:06
  • 1
    I'd start by looking at :help pattern and searching for line number. The format is \%<linenumber>l where <linenumber> is a specific line number, \%<<linenumber>l matches above a specific line number and \%><linenumber>l matches below a certain line number. I would try those options first and just use 5. The manual does state a warning about inserting and deleting lines can quickly mess up highlighting. – Vee Dec 14 '20 at 18:18
  • 1
    AWESOME!! I can't seem to change the colors of the columns of numbers, but at least it's showing up as different from the header. THANK YOU! – Sam Alleman Dec 14 '20 at 18:41
  • 1
    Excellent! I'll upgrade my comment to an answer if you could mark it as accepted. – Vee Dec 14 '20 at 19:00
1

I think what you want is found at :help pattern and searching for line number.

                                                /\%l /\%>l /\%<l
\%23l   Matches in a specific line.
\%<23l  Matches above a specific line (lower line number).
\%>23l  Matches below a specific line (higher line number).
        These three can be used to match specific lines in a buffer.  The "23"
        can be any line number.  The first line is 1. {not in Vi}
        WARNING: When inserting or deleting lines Vim does not automatically
        update the matches.  This means Syntax highlighting quickly becomes
        wrong.
        Example, to highlight the line where the cursor currently is:
                :exe '/\%' . line(".") . 'l.*' 
        When 'hlsearch' is set and you move the cursor around and make changes
        this will clearly show when the match is updated or not.

So what you want to add for your syntax matches is \%>5l

NOTE: The manual does state a warning about inserting and deleting lines can quickly mess up highlighting.

If you are having issues with highlighting in certain columns, there is additional pattern matches for column specific values as well:

\%23c   Matches in a specific column.
\%<23c  Matches before a specific column.
\%>23c  Matches after a specific column.
        These three can be used to match specific columns in a buffer or
        string.  The "23" can be any column number.  The first column is 1.
        Actually, the column is the byte number (thus it's not exactly right
        for multi-byte characters).  {not in Vi}
        WARNING: When inserting or deleting text Vim does not automatically     
        update the matches.  This means Syntax highlighting quickly becomes
        wrong.
        Example, to highlight the column where the cursor currently is:
                :exe '/\%' . col(".") . 'c'
        When 'hlsearch' is set and you move the cursor around and make changes
        this will clearly show when the match is updated or not.
        Example for matching a single byte in column 44:
                /\%>43c.\%<46c
        Note that "\%<46c" matches in column 45 when the "." matches a byte in 
        column 44.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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