LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   VIM syntax highlighting: Use of AWK syntax inside shell scripts (https://www.linuxquestions.org/questions/programming-9/vim-syntax-highlighting-use-of-awk-syntax-inside-shell-scripts-4175436950/)

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
Code:

#! /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:

Code:

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
endif

syntax include @AwkSyntax syntax/awk.vim

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

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.

kaiserkarl13 03-03-2021 09:59 AM

Solution
 
This is an old post, but I realized I never posted my solution, so here it is.

Place the following in ~/.vim/after/syntax/sh.vim:
Code:

" Make the shell recognize AWK syntax when appropriate!
if exists('b:current_syntax')
    " Unset this, or the TeX syntax file thinks it's already loaded!
    let s:current_syntax = b:current_syntax
    unlet b:current_syntax
endif

syntax include @AwkSyntax syntax/awk.vim

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

syntax match shStatement '\(^\s*\|\s\+\)\@<=g\=awk\>'

syntax region AwkDblQuotes start=#\(\(^\s*\|\s\+\)g\=awk\(\s\+[^"]\+\)*\s\+\)\@<="#
\      end=#"# skip=#\\"# contains=AwkScriptDbl matchgroup=shQuote
\      containedin=shDo
syntax region AwkSglQuotes start=#\(\(^\s*\|\s\+\)g\=awk\(\s\+[^']\+\)*\s\+\)\@<='#
\      end=#'# contains=AwkScript matchgroup=SpecialComment keepend
\      containedin=shDo

syntax region AwkScript start=#[^']# end=#.\@=# contained
\      matchgroup=SpecialComment contained
\      containedin=AwkSglQuotes contains=@AwkSyntax
syntax region AwkScriptDbl start=#[^"]# end=#\\\@<!"\@=# keepend
\      matchgroup=SpecialComment containedin=AwkDblQuotes contained
\      contains=@AwkSyntax
syntax region awkString start=#\\"# end=#\\"# contained keepend
\      containedin=AwkScriptDbl
\      contains=shDeref,shDerefSimple,shDerefVar,shDerefSpecial
syntax match awkFieldVars '\\\$\d\+' contained containedin=AwkScriptDbl



All times are GMT -5. The time now is 02:49 AM.