-   Programming (
-   -   VIM syntax highlighting: Use of AWK syntax inside shell scripts (

kaiserkarl13 11-13-2012 10:04 AM

VIM syntax highlighting: Use of AWK syntax inside shell scripts
I've been trying to add a simple script inside ~/.vim/after/syntax/sh.vim that will use the syntax highlighting for awk when the user does something like

#! /bin/sh
awk < myfile.txt "
  BEGIN {i=0}
  /stupid/ {$3 = ''; print; i = i + 1}
  END {print \"I found\", i, \"stupid people\"}

There are facilities to do this, of course, but I'm stumbling over the precise details. Here's what I have so far:


if exists('b:current_syntax')
    " Unset this, or the AWK syntax file thinks it's already loaded!
    let s:current_syntax = b:current_syntax
    unlet b:current_syntax

syntax include @AwkSyntax syntax/awk.vim

if exists('s:current_syntax')
    " Undo what we did before
    let b:current_syntax=s:current_syntax
    unlet b:current_syntax

syntax region AwkScript start=#\(^\s*g\=awk\(\s\+\p\+\)*\s\+\)\@<='#
\      end=#'# contains=@AwkSyntax matchgroup=SpecialComment
syntax region AwkScript end=#"# skip=#\\"#
\      start=#\(^\s*g\=awk\(\s\+\p\+\)*\s\+\)\@<="#
\      matchgroup=SpecialComment contains=@AwkSyntax

This works for the single-quote version, but not the double-quote version (it recognizes everything as an AWK string). I don't know how to resolve this issue. What it should do is start AWK syntax right AFTER the quotation mark (not ON it...) and avoid recognizing the entire string as a shell string. I can't seem to do both of these things at the same time.

All times are GMT -5. The time now is 10:07 PM.