├── README.md └── syntax └── python.vim /README.md: -------------------------------------------------------------------------------- 1 | # Python syntax highlighting script for Vim 2 | 3 | Enhanced version of the original Python syntax highlighting script. 4 | Based on `python.vim` from Vim 7.4 distribution by [Zvezdan Petkovic](https://github.com/zvezdan). 5 | 6 | ![python-syntax](https://cdn.nixsys.fr/static/github/python-syntax/preview.png) 7 | 8 | ## Features 9 | 10 | Changes from the original `python.vim` are: 11 | 12 | * [OFF] Highlight `self`, `cls` keywords 13 | * [ON] Highlight class, function parameters 14 | * [ON] Highlight standard operators: `~,!,^,&,|,*,/,%,+,-` 15 | * [ON] Highlight pseudo operators: `-=,//=,*=,&=,%=,+=,!=` 16 | * 3 extras options: 17 | 18 | * `let python_self_cls_highlight = 1` 19 | * `let python_no_operator_highlight = 1` 20 | * `let python_no_parameter_highlight = 1` 21 | 22 | * All the options can be switched on together (recommended). 23 | 24 | * `let python_highlight_all = 1` 25 | 26 | ## Color Scheme 27 | 28 | * Vim keywords: 29 | 30 | * `pythonRepeat` `pythonConditional` `pythonInclude` `pythonTodo` `pythonComment` `pythonStatement` 31 | * `pythonEscape` `pythonSpaceError` `pythonException` `pythonExceptions` `pythonDoctest` `pythonDoctestValue` 32 | * `pythonNumber` `pythonString` `pythonRawString` `pythonBuiltin` `pythonFunction` `pythonClass` `pythonDecorator` 33 | * `pythonSelf` `pythonConstant` `pythonBrackets` `pythonOperator` `pythonExtraOperator` `pythonExtraPseudoOperator` `pythonClassParameters` `pythonFunctionParameters` 34 | 35 | ## How to install 36 | 37 | The easiest installation method is to place `syntax/python.vim` script into your `~/.vim/syntax/` directory. 38 | You can also use `Pathogen` or `Vundle` plugin managers in which case you can install the whole `kh3phr3n/python-syntax` 39 | repository into the corresponding plugins directory. 40 | 41 | ## Informations 42 | 43 | This plugin is strongly inspired by: 44 | 45 | * [python-mode](https://github.com/klen/python-mode) 46 | * [python-syntax](https://github.com/hdima/python-syntax) 47 | * [pretty-vim-python](https://github.com/pfdevilliers/Pretty-Vim-Python) 48 | 49 | ## Contributors 50 | 51 | * [Aramis Razzaghipour](https://github.com/arzg) 52 | 53 | -------------------------------------------------------------------------------- /syntax/python.vim: -------------------------------------------------------------------------------- 1 | " Vim syntax file 2 | " Language: Python 3 | " Maintainer: Zvezdan Petkovic 4 | " Last Change: 2016 Apr 13 5 | " Credits: Neil Schemenauer 6 | " Dmitry Vasiliev 7 | " 8 | " This version is a major rewrite by Zvezdan Petkovic. 9 | " 10 | " - introduced highlighting of doctests 11 | " - updated keywords, built-ins, and exceptions 12 | " - corrected regular expressions for 13 | " 14 | " * functions 15 | " * decorators 16 | " * strings 17 | " * escapes 18 | " * numbers 19 | " * space error 20 | " 21 | " - corrected synchronization 22 | " - more highlighting is ON by default, except: 23 | " - space error highlighting is OFF by default, 24 | " - self, cls keywords highlighting is OFF by default. 25 | " 26 | " Optional highlighting can be controlled using these variables. 27 | " 28 | " let python_no_builtin_highlight = 1 29 | " let python_no_doctest_code_highlight = 1 30 | " let python_no_doctest_highlight = 1 31 | " let python_no_exception_highlight = 1 32 | " let python_no_number_highlight = 1 33 | " let python_space_error_highlight = 1 34 | " 35 | " let python_no_parameter_highlight = 1 36 | " let python_no_operator_highlight = 1 37 | " let python_self_cls_highlight = 1 38 | " 39 | " All the options above can be switched on together. 40 | " 41 | " let python_highlight_all = 1 42 | " 43 | 44 | " For version 5.x: Clear all syntax items. 45 | " For version 6.x: Quit when a syntax file was already loaded. 46 | if version < 600 47 | syntax clear 48 | elseif exists("b:current_syntax") 49 | finish 50 | endif 51 | 52 | " We need nocompatible mode in order to continue lines with backslashes. 53 | " Original setting will be restored. 54 | let s:cpo_save = &cpo 55 | set cpo&vim 56 | 57 | " Keep Python keywords in alphabetical order inside groups for easy 58 | " comparison with the table in the 'Python Language Reference' 59 | " https://docs.python.org/2/reference/lexical_analysis.html#keywords, 60 | " https://docs.python.org/3/reference/lexical_analysis.html#keywords. 61 | " Groups are in the order presented in NAMING CONVENTIONS in syntax.txt. 62 | " Exceptions come last at the end of each group (class and def below). 63 | " 64 | " Keywords 'with' and 'as' are new in Python 2.6 65 | " (use 'from __future__ import with_statement' in Python 2.5). 66 | " 67 | " Some compromises had to be made to support both Python 3 and 2. 68 | " We include Python 3 features, but when a definition is duplicated, 69 | " the last definition takes precedence. 70 | " 71 | " - 'False', 'None', and 'True' are keywords in Python 3 but they are 72 | " built-ins in 2 and will be highlighted as built-ins below. 73 | " - 'exec' is a built-in in Python 3 and will be highlighted as 74 | " built-in below. 75 | " - 'nonlocal' is a keyword in Python 3 and will be highlighted. 76 | " - 'print' is a built-in in Python 3 and will be highlighted as 77 | " built-in below (use 'from __future__ import print_function' in 2) 78 | " - async and await were added in Python 3.5 and are soft keywords. 79 | " 80 | syn keyword pythonStatement class nextgroup=pythonClass skipwhite 81 | syn keyword pythonStatement def nextgroup=pythonFunction skipwhite 82 | 83 | syn keyword pythonStatement False, None, True 84 | syn keyword pythonStatement as assert break continue del exec global 85 | syn keyword pythonStatement lambda nonlocal pass print return with 86 | syn keyword pythonConditional elif else if 87 | syn keyword pythonRepeat for while 88 | syn keyword pythonOperator and in is not or 89 | syn keyword pythonException except finally raise try 90 | syn keyword pythonInclude import 91 | syn keyword pythonAsync async await 92 | 93 | " Generators (yield from: Python 3.3) 94 | syn match pythonInclude "\" display 95 | syn match pythonStatement "\" display 96 | syn match pythonStatement "\" display 97 | 98 | " pythonExtra(*)Operator 99 | syn match pythonExtraOperator "\%([~!^&|*/%+-]\|\%(class\s*\)\@\|<=\|\%(<\|\>\|>=\|=\@\|\*\*\|\.\.\.\|\.\.\|::\|=\)" 100 | syn match pythonExtraPseudoOperator "\%(-=\|/=\|\*\*=\|\*=\|&&=\|&=\|&&\|||=\||=\|||\|%=\|+=\|!\~\|!=\)" 101 | 102 | " Decorators (new in Python 2.4) 103 | syn match pythonDecorator "@" display nextgroup=pythonFunction skipwhite 104 | " The zero-length non-grouping match before the function name is 105 | " extremely important in pythonFunction. Without it, everything is 106 | " interpreted as a function inside the contained environment of 107 | " doctests. 108 | " A dot must be allowed because of @MyClass.myfunc decorators. 109 | 110 | " Colons 111 | syn match pythonColon ":" 112 | 113 | " Class parameters 114 | syn match pythonClass "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained nextgroup=pythonClassVars 115 | syn region pythonClassVars start="(" end=")" contained contains=pythonClassParameters transparent keepend 116 | syn match pythonClassParameters "[^,]*" contained contains=pythonExtraOperator,pythonBuiltin,pythonConstant,pythonStatement,pythonNumber,pythonString,pythonBrackets skipwhite 117 | 118 | " Function parameters 119 | syn match pythonFunction "\%(\%(def\s\|class\s\|@\)\s*\)\@<=\h\%(\w\|\.\)*" contained nextgroup=pythonFunctionVars 120 | syn match pythonFunction '\(\.\)\@<=.\{-}\((\)\@=' 121 | syn match pythonFunction '\(\.\)\@" 197 | syn match pythonNumber "\<0[xX]\x\+[Ll]\=\>" 198 | syn match pythonNumber "\<0[bB][01]\+[Ll]\=\>" 199 | syn match pythonNumber "\<\%([1-9]\d*\|0\)[Ll]\=\>" 200 | syn match pythonNumber "\<\d\+[jJ]\>" 201 | syn match pythonNumber "\<\d\+[eE][+-]\=\d\+[jJ]\=\>" 202 | syn match pythonNumber 203 | \ "\<\d\+\.\%([eE][+-]\=\d\+\)\=[jJ]\=\%(\W\|$\)\@=" 204 | syn match pythonNumber 205 | \ "\%(^\|\W\)\zs\d*\.\d\+\%([eE][+-]\=\d\+\)\=[jJ]\=\>" 206 | endif 207 | 208 | " Group the built-ins in the order in the 'Python Library Reference' for 209 | " easier comparison. 210 | " https://docs.python.org/2/library/constants.html 211 | " https://docs.python.org/3/library/constants.html 212 | " http://docs.python.org/2/library/functions.html 213 | " http://docs.python.org/3/library/functions.html 214 | " http://docs.python.org/2/library/functions.html#non-essential-built-in-functions 215 | " http://docs.python.org/3/library/functions.html#non-essential-built-in-functions 216 | " Python built-in functions are in alphabetical order. 217 | if !exists("python_no_builtin_highlight") 218 | " built-in constants 219 | " 'False', 'True', and 'None' are also reserved words in Python 3 220 | syn keyword pythonBuiltin False True None 221 | syn keyword pythonBuiltin NotImplemented Ellipsis __debug__ 222 | " built-in functions 223 | syn keyword pythonBuiltin abs all any bin bool bytearray callable chr 224 | syn keyword pythonBuiltin classmethod compile complex delattr dict dir 225 | syn keyword pythonBuiltin divmod enumerate eval filter float format 226 | syn keyword pythonBuiltin frozenset getattr globals hasattr hash 227 | syn keyword pythonBuiltin help hex id input int isinstance 228 | syn keyword pythonBuiltin issubclass iter len list locals map max 229 | syn keyword pythonBuiltin memoryview min next object oct open ord pow 230 | syn keyword pythonBuiltin print property range repr reversed round set 231 | syn keyword pythonBuiltin setattr slice sorted staticmethod str 232 | syn keyword pythonBuiltin sum super tuple type vars zip __import__ 233 | " Python 2 only 234 | syn keyword pythonBuiltin basestring cmp execfile file 235 | syn keyword pythonBuiltin long raw_input reduce reload unichr 236 | syn keyword pythonBuiltin unicode xrange 237 | " Python 3 only 238 | syn keyword pythonBuiltin ascii bytes exec 239 | " non-essential built-in functions; Python 2 only 240 | syn keyword pythonBuiltin apply buffer coerce intern 241 | " avoid highlighting attributes as builtins 242 | " syn match pythonAttribute /\.\h\w*/hs=s+1 contains=ALLBUT,pythonBuiltin transparent 243 | endif 244 | 245 | " From the 'Python Library Reference' class hierarchy at the bottom. 246 | " http://docs.python.org/2/library/exceptions.html 247 | " http://docs.python.org/3/library/exceptions.html 248 | if !exists("python_no_exception_highlight") 249 | " builtin base exceptions (used mostly as base classes for other exceptions) 250 | syn keyword pythonExceptions BaseException Exception 251 | syn keyword pythonExceptions ArithmeticError BufferError 252 | syn keyword pythonExceptions LookupError 253 | " builtin base exceptions removed in Python 3 254 | syn keyword pythonExceptions EnvironmentError StandardError 255 | " builtin exceptions (actually raised) 256 | syn keyword pythonExceptions AssertionError AttributeError 257 | syn keyword pythonExceptions EOFError FloatingPointError GeneratorExit 258 | syn keyword pythonExceptions ImportError IndentationError 259 | syn keyword pythonExceptions IndexError KeyError KeyboardInterrupt 260 | syn keyword pythonExceptions MemoryError NameError NotImplementedError 261 | syn keyword pythonExceptions OSError OverflowError ReferenceError 262 | syn keyword pythonExceptions RuntimeError StopIteration SyntaxError 263 | syn keyword pythonExceptions SystemError SystemExit TabError TypeError 264 | syn keyword pythonExceptions UnboundLocalError UnicodeError 265 | syn keyword pythonExceptions UnicodeDecodeError UnicodeEncodeError 266 | syn keyword pythonExceptions UnicodeTranslateError ValueError 267 | syn keyword pythonExceptions ZeroDivisionError 268 | " builtin OS exceptions in Python 3 269 | syn keyword pythonExceptions BlockingIOError BrokenPipeError 270 | syn keyword pythonExceptions ChildProcessError ConnectionAbortedError 271 | syn keyword pythonExceptions ConnectionError ConnectionRefusedError 272 | syn keyword pythonExceptions ConnectionResetError FileExistsError 273 | syn keyword pythonExceptions FileNotFoundError InterruptedError 274 | syn keyword pythonExceptions IsADirectoryError NotADirectoryError 275 | syn keyword pythonExceptions PermissionError ProcessLookupError 276 | syn keyword pythonExceptions RecursionError StopAsyncIteration 277 | syn keyword pythonExceptions TimeoutError 278 | " builtin exceptions deprecated/removed in Python 3 279 | syn keyword pythonExceptions IOError VMSError WindowsError 280 | " builtin warnings 281 | syn keyword pythonExceptions BytesWarning DeprecationWarning FutureWarning 282 | syn keyword pythonExceptions ImportWarning PendingDeprecationWarning 283 | syn keyword pythonExceptions RuntimeWarning SyntaxWarning UnicodeWarning 284 | syn keyword pythonExceptions UserWarning Warning 285 | " builtin warnings in Python 3 286 | syn keyword pythonExceptions ResourceWarning 287 | endif 288 | 289 | if exists("python_space_error_highlight") 290 | " trailing whitespace 291 | syn match pythonSpaceError display excludenl "\s\+$" 292 | " mixed tabs and spaces 293 | syn match pythonSpaceError display " \+\t" 294 | syn match pythonSpaceError display "\t\+ " 295 | endif 296 | 297 | " Do not spell doctests inside strings. 298 | " Notice that the end of a string, either ''', or """, will end the contained 299 | " doctest too. Thus, we do *not* need to have it as an end pattern. 300 | if !exists("python_no_doctest_highlight") 301 | if !exists("python_no_doctest_code_highlight") 302 | syn region pythonDoctest 303 | \ start="^\s*>>>\s" end="^\s*$" 304 | \ contained contains=ALLBUT,pythonDoctest,@Spell 305 | syn region pythonDoctestValue 306 | \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$" 307 | \ contained 308 | else 309 | syn region pythonDoctest 310 | \ start="^\s*>>>" end="^\s*$" 311 | \ contained contains=@NoSpell 312 | endif 313 | endif 314 | 315 | " Sync at the beginning of class, function, or method definition. 316 | syn sync match pythonSync grouphere NONE "^\s*\%(def\|class\)\s\+\h\w*\s*(" 317 | 318 | if version >= 508 || !exists("did_python_syn_inits") 319 | if version <= 508 320 | let did_python_syn_inits = 1 321 | command -nargs=+ HiLink hi link 322 | else 323 | command -nargs=+ HiLink hi def link 324 | endif 325 | 326 | " The default highlight links. Can be overridden later. 327 | HiLink pythonStatement Statement 328 | HiLink pythonConditional Conditional 329 | HiLink pythonRepeat Repeat 330 | HiLink pythonOperator Operator 331 | HiLink pythonException Exception 332 | HiLink pythonInclude Include 333 | HiLink pythonAsync Statement 334 | HiLink pythonDecorator Define 335 | HiLink pythonComment Comment 336 | HiLink pythonTodo Todo 337 | HiLink pythonString String 338 | HiLink pythonRawString String 339 | HiLink pythonQuotes StringDelimiter 340 | HiLink pythonTripleQuotes pythonQuotes 341 | HiLink pythonEscape Special 342 | HiLink pythonBrackets Delimiter 343 | HiLink pythonColon Delimiter 344 | 345 | " Classes, Functions 346 | HiLink pythonClass Type 347 | HiLink pythonFunction Function 348 | 349 | if !exists("python_no_number_highlight") 350 | HiLink pythonNumber Number 351 | endif 352 | if !exists("python_no_builtin_highlight") 353 | HiLink pythonBuiltin Function 354 | endif 355 | if !exists("python_no_exception_highlight") 356 | HiLink pythonExceptions Structure 357 | endif 358 | if exists("python_space_error_highlight") 359 | HiLink pythonSpaceError Error 360 | endif 361 | if !exists("python_no_doctest_highlight") 362 | HiLink pythonDoctest Special 363 | HiLink pythonDoctestValue Define 364 | endif 365 | 366 | if exists("python_self_cls_highlight") 367 | syn keyword pythonSelf self cls 368 | HiLink pythonSelf Identifier 369 | endif 370 | if !exists("python_no_operator_highlight") 371 | HiLink pythonExtraOperator Operator 372 | HiLink pythonExtraPseudoOperator Operator 373 | endif 374 | if !exists("python_no_parameter_highlight") 375 | HiLink pythonBrackets Normal 376 | HiLink pythonClassParameters Constant 377 | HiLink pythonFunctionParameters Constant 378 | endif 379 | 380 | delcommand HiLink 381 | endif 382 | 383 | let b:current_syntax = "python" 384 | 385 | let &cpo = s:cpo_save 386 | unlet s:cpo_save 387 | 388 | " vim:set sw=2 sts=2 ts=8 noet: 389 | 390 | --------------------------------------------------------------------------------