In Python3, file is no longer a builtin. It's just a plain ol' name that isn't even associated with anything until you do. But all the syntax files that I've used have it as a builtin.

Is there a simple way (I checked :help syn to look for an antonym, but nothing jumped out at me) to remove file from the syntax category, or do I pretty much just have to define my own?

1 Answer 1


(Note: the Python 2 keywords were removed in February 2021 with Vim 8.2.2554).

You'll have to look at the syntax file and undo what it's doing. You can find the syntax file with :e $VIMRUNTIME/syntax/python.vim, and with /file I found:

" Python 2 only
syn keyword pythonBuiltin     basestring cmp execfile file
syn keyword pythonBuiltin     long raw_input reduce reload unichr
syn keyword pythonBuiltin     unicode xrange


" non-essential built-in functions; Python 2 only
syn keyword pythonBuiltin     apply buffer coerce intern

As far as I know there is no way to remove keywords from the syn keyword declaration, so you'll have to either:

  1. Clear the pythonBuiltin declarations with :syn clear pythonBuiltin and define your own with just the Python 3 ones: syn keyword pythonBuiltin abs all any [...]
  2. Override the Python 3 ones; e.g. :syn keyword pythonTwoBuiltin file.

The first method is arguably a bit "cleaner", but with the second method you will retain future additions to pythonBuiltin from the system's file. It will also allow you to do something like :hi link pythonTwoBuiltin pythonBuiltin to highlight the Python 2 builtins in case you find yourself editing Python 2 code.

I would personally opt for the second method, and added this to my vimrc:

" Remove Python 2 keywords.
augroup angering_zed_shaw
    autocmd Syntax python
        \ syn keyword pythonTwoBuiltin basestring cmp execfile file long
        \                              raw_input reduce reload unichr unicode
        \                              xrange apply buffer coerce intern
augroup end

Note you can't just add this to your vimrc file; you'll have to use an autocmd or the ~/.vim/after/syntax/python.vim file. See How can I add additional syntax highlighting rules in my local vimrc? for details on that.

  • I wish I could +2 for angering_zed_shaw :D Commented Jun 27, 2017 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.