LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2012, 10:04 AM   #1
kaiserkarl13
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora
Posts: 28

Rep: Reputation: 5
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.
 
Old 03-03-2021, 09:59 AM   #2
kaiserkarl13
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora
Posts: 28

Original Poster
Rep: Reputation: 5
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
 
1 members found this post helpful.
  


Reply

Tags
awk, shell script, syntax, vim



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vim syntax highlighting Gortex Programming 1 08-18-2008 05:43 PM
VIM Syntax Highlighting JMJ_coder Programming 3 03-05-2008 04:37 PM
[SOLVED] vim without syntax highlighting lord_didger Debian 5 11-18-2006 01:29 PM
vim syntax highlighting MiniMe001 Linux - General 2 06-19-2005 11:08 AM
Vim syntax highlighting NSKL Linux - Software 2 11-09-2002 02:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration