1" Vim syntax file 2" Language: MS-DOS batch file (with NT command extensions) 3" Maintainer: Mike Williams <[email protected]> 4" Filenames: *.bat 5" Last Change: 6th September 2009 6" Web Page: http://www.eandem.co.uk/mrw/vim 7" 8" Options Flags: 9" dosbatch_cmdextversion - 1 = Windows NT, 2 = Windows 2000 [default] 10" 11 12" quit when a syntax file was already loaded 13if exists("b:current_syntax") 14 finish 15endif 16 17" Set default highlighting to Win2k 18if !exists("dosbatch_cmdextversion") 19 let dosbatch_cmdextversion = 2 20endif 21 22" DOS bat files are case insensitive but case preserving! 23syn case ignore 24 25syn keyword dosbatchTodo contained TODO 26 27" Dosbat keywords 28syn keyword dosbatchStatement goto call exit 29syn keyword dosbatchConditional if else 30syn keyword dosbatchRepeat for 31 32" Some operators - first lot are case sensitive! 33syn case match 34syn keyword dosbatchOperator EQU NEQ LSS LEQ GTR GEQ 35syn case ignore 36syn match dosbatchOperator "\s[-+\*/%!~]\s" 37syn match dosbatchOperator "=" 38syn match dosbatchOperator "[-+\*/%]=" 39syn match dosbatchOperator "\s\(&\||\|^\|<<\|>>\)=\=\s" 40syn match dosbatchIfOperator "if\s\+\(\(not\)\=\s\+\)\=\(exist\|defined\|errorlevel\|cmdextversion\)\="lc=2 41 42" String - using "'s is a convenience rather than a requirement outside of FOR 43syn match dosbatchString "\"[^"]*\"" contains=dosbatchVariable,dosBatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell 44syn match dosbatchString "\<echo\([^)>|]\|\^\@<=[)>|]\)*"lc=4 contains=dosbatchVariable,dosbatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell 45syn match dosbatchEchoOperator "\<echo\s\+\(on\|off\)\s*$"lc=4 46 47" For embedded commands 48syn match dosbatchCmd "(\s*'[^']*'"lc=1 contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator 49 50" Numbers - surround with ws to not include in dir and filenames 51syn match dosbatchInteger "[[:space:]=(/:,!~-]\d\+"lc=1 52syn match dosbatchHex "[[:space:]=(/:,!~-]0x\x\+"lc=1 53syn match dosbatchBinary "[[:space:]=(/:,!~-]0b[01]\+"lc=1 54syn match dosbatchOctal "[[:space:]=(/:,!~-]0\o\+"lc=1 55syn cluster dosbatchNumber contains=dosbatchInteger,dosbatchHex,dosbatchBinary,dosbatchOctal 56 57" Command line switches 58syn match dosbatchSwitch "/\(\a\+\|?\)" 59 60" Various special escaped char formats 61syn match dosbatchSpecialChar "\^[&|()<>^]" 62syn match dosbatchSpecialChar "\$[a-hl-npqstv_$+]" 63syn match dosbatchSpecialChar "%%" 64 65" Environment variables 66syn match dosbatchIdentifier contained "\s\h\w*\>" 67syn match dosbatchVariable "%\h\w*%" 68syn match dosbatchVariable "%\h\w*:\*\=[^=]*=[^%]*%" 69syn match dosbatchVariable "%\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=%" contains=dosbatchInteger 70syn match dosbatchVariable "!\h\w*!" 71syn match dosbatchVariable "!\h\w*:\*\=[^=]*=[^!]*!" 72syn match dosbatchVariable "!\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=!" contains=dosbatchInteger 73syn match dosbatchSet "\s\h\w*[+-]\==\{-1}" contains=dosbatchIdentifier,dosbatchOperator 74 75" Args to bat files and for loops, etc 76syn match dosbatchArgument "%\(\d\|\*\)" 77syn match dosbatchArgument "%[a-z]\>" 78if dosbatch_cmdextversion == 1 79 syn match dosbatchArgument "%\~[fdpnxs]\+\(\($PATH:\)\=[a-z]\|\d\)\>" 80else 81 syn match dosbatchArgument "%\~[fdpnxsatz]\+\(\($PATH:\)\=[a-z]\|\d\)\>" 82endif 83 84" Line labels 85syn match dosbatchLabel "^\s*:\s*\h\w*\>" 86syn match dosbatchLabel "\<\(goto\|call\)\s\+:\h\w*\>"lc=4 87syn match dosbatchLabel "\<goto\s\+\h\w*\>"lc=4 88syn match dosbatchLabel ":\h\w*\>" 89 90" Comments - usual rem but also two colons as first non-space is an idiom 91syn match dosbatchComment "^rem\($\|\s.*$\)"lc=3 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 92syn match dosbatchComment "^@rem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 93syn match dosbatchComment "\srem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 94syn match dosbatchComment "\s@rem\($\|\s.*$\)"lc=5 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 95syn match dosbatchComment "\s*:\s*:.*$" contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 96 97" Comments in ()'s - still to handle spaces before rem 98syn match dosbatchComment "(rem\([^)]\|\^\@<=)\)*"lc=4 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell 99 100syn keyword dosbatchImplicit append assoc at attrib break cacls cd chcp chdir 101syn keyword dosbatchImplicit chkdsk chkntfs cls cmd color comp compact convert copy 102syn keyword dosbatchImplicit date del dir diskcomp diskcopy doskey echo endlocal 103syn keyword dosbatchImplicit erase fc find findstr format ftype 104syn keyword dosbatchImplicit graftabl help keyb label md mkdir mode more move 105syn keyword dosbatchImplicit path pause popd print prompt pushd rd recover rem 106syn keyword dosbatchImplicit ren rename replace restore rmdir set setlocal shift 107syn keyword dosbatchImplicit sort start subst time title tree type ver verify 108syn keyword dosbatchImplicit vol xcopy 109 110" Define the default highlighting. 111" Only when an item doesn't have highlighting yet 112 113hi def link dosbatchTodo Todo 114 115hi def link dosbatchStatement Statement 116hi def link dosbatchCommands dosbatchStatement 117hi def link dosbatchLabel Label 118hi def link dosbatchConditional Conditional 119hi def link dosbatchRepeat Repeat 120 121hi def link dosbatchOperator Operator 122hi def link dosbatchEchoOperator dosbatchOperator 123hi def link dosbatchIfOperator dosbatchOperator 124 125hi def link dosbatchArgument Identifier 126hi def link dosbatchIdentifier Identifier 127hi def link dosbatchVariable dosbatchIdentifier 128 129hi def link dosbatchSpecialChar SpecialChar 130hi def link dosbatchString String 131hi def link dosbatchNumber Number 132hi def link dosbatchInteger dosbatchNumber 133hi def link dosbatchHex dosbatchNumber 134hi def link dosbatchBinary dosbatchNumber 135hi def link dosbatchOctal dosbatchNumber 136 137hi def link dosbatchComment Comment 138hi def link dosbatchImplicit Function 139 140hi def link dosbatchSwitch Special 141 142hi def link dosbatchCmd PreProc 143 144 145let b:current_syntax = "dosbatch" 146 147" vim: ts=8 148