-1

I'm trying to add additional syntax highlighting to a plugin I'm using for syntax highlighting. I've tried adding the additional syntax file fstrings.vim to the ~/.config/nvim/after/syntax folder but my nvim is not picking it up.

Things I've tried:

  1. Check if ~/.config/nvim/after and/or ~/.config/nvim/after/syntax is added to my runtime path.
  • Only ~/.config/nvim/after is added to my path - checked with :set rtp >> runtimepath=~...,...,~/.config/nvim/after - however I don't see the file loaded in :scriptnames. How can I get the file to load?
  1. Change location of the file to ~/.config/nvim/after instead of ~/.config/nvim/after/syntax
  • This does not work either.
  1. Manually load the file.
  • This actually does work surprisingly. I can manually change the syntax of the file using :set syntax=fstrings however, all the original syntax highlighting provided by the plugin disappears, and only the highlighting provided (see the print statement in the image below) by the fstrings.vim is used. This leads me to believe the path is set up correctly however nvim does not want to load it during startup and/or ignores it because of the plugin being used.

enter image description here

I know the /after/ folder is used to apply settings after previous settings, rather than overwriting. I've already tried the solution here and here but I've not been able to make it work. Any help on how to debug this would be appreciated.

" ~/.config/nvim/after/syntax/fstrings.vim

syn match pythonEscape +{{+ contained containedin=pythonfString,pythonfDocstring
syn match pythonEscape +}}+ contained containedin=pythonfString,pythonfDocstring

syn region pythonfString matchgroup=pythonQuotes
      \ start=+[fF]\@1<=\z(['"]\)+ end="\z1"
      \ contains=@Spell,pythonEscape,pythonInterpolation
syn region pythonfDocstring matchgroup=pythonQuotes
      \ start=+[fF]\@1<=\z('''\|"""\)+ end="\z1" keepend
      \ contains=@Spell,pythonEscape,pythonSpaceError,pythonInterpolation,pythonDoctest

syn region pythonInterpolation contained
      \ matchgroup=SpecialChar
      \ start=+{{\@!+ end=+}}\@!+ skip=+{{+ keepend
      \ contains=ALLBUT,pythonDecoratorName,pythonDecorator,pythonFunction,pythonDoctestValue,pythonDoctest

syn match pythonStringModifier /:\(.[<^=>]\)\?[-+ ]\?#\?0\?[0-9]*[_,]\?\(\.[0-9]*\)\?[bcdeEfFgGnosxX%]\?/ contained containedin=pythonInterpolation
syn match pythonStringModifier /![sra]/ contained containedin=pythonInterpolation

hi link pythonfString String
hi link pythonfDocstring String
hi link pythonStringModifier PreProc
6
  • 2
    Welcome to Vi and Vim! It looks like you're trying to extend the syntax for Python files, correct? If so, you should consider that those files end up with filetype detected as 'python' (see :set ft? on a Python file), so you should write your extended syntax rules in a file named syntax/python.vim or perhaps syntax/python_fstrings.vim (the file name can use _ as a separator, the first component matching the filetype name.) Does that seem to be what you're looking for? Let me know if that's the case and I'll turn this into an answer.
    – filbranden
    Jan 11, 2021 at 22:48
  • Hi @filbranden :) Exactly right. I am trying to extend the syntax of just .py files to had additional settings. I tried what you suggested by renaming the file python_strings.vim but it still did not add the functionality to the file. What is interesting again is if I call it explicitly set syntax=python_fstrings all the syntax settings are overwritten except for the f strings ... just like before. So the file is being detected, just not loaded when a .py file is present.
    – Chef1075
    Jan 11, 2021 at 23:19
  • If all you want is support for f-strings in Python syntax highlighting, then consider using the vim-polyglot plug-in, which includes enhanced syntax highlighting for many languages, including f-string support in Python.
    – filbranden
    Jan 11, 2021 at 23:42
  • 1
    Humm ... confirmed my file and path is ~/.config/nvim/after/syntax/python_fstrings.vim and called set syntax=python and nothing changed within the file. I've looked into vim-polyglot which uses github.com/vim-python/python-syntax as the python syntax. I'm more interested in understanding why I cannot manually add additional syntax. Having plugins is nice, but I'd like to know how everything works :) It might be the order of how everything is defined. How could I debug that?
    – Chef1075
    Jan 11, 2021 at 23:49
  • 1
    Yes! Renaming to syntax/python.vim worked! Gah ... that was a whole round about. Thanks for all your help @filbranden!
    – Chef1075
    Jan 12, 2021 at 2:29

1 Answer 1

2

The syntax rules are typically named after the 'filetype' set for the buffer, which in turn is typically detected (based on file extension, file name, or file contents) and usually set automatically.

If you want to extend syntax highlighting for Python, then you should typically name your script after the "python" filetype.

When Vim loads syntax rules for Python, it will for files named syntax/python.vim or syntax/python/*.vim (the ftplugin rules also allow for python_*.vim, but it turns out that's not the case for syntax.)

So you should name your file ~/.config/nvim/after/syntax/python.vim (or ~/.config/nvim/after/syntax/python/fstrings.vim) in order to have it loaded for Python files and after the bundled runtime rules for Python are loaded.


Another option is to set the 'syntax' option to include multiple types, separated by a dot. For example, :setlocal syntax=python.fstrings will first load syntax/python.vim and then syntax/fstrings.vim. This would be more useful if you had a piece of syntax that's somewhat generic and can make sense for multiple separate languages (for example, some syntax highlighting to a documentation format inside comments?) It doesn't seem to be very applicable in this case, where f-strings are essentially a Python syntax. Maintaining a 'syntax' setting separate from the 'filetype' one can also be tricky, it needs to be set at the right location (not too early or too late) to avoid getting clobbered by the default setting.


While troubleshooting such an issue of a script of yours not being loaded, you can use the :scriptnames command to check whether your *.vim script was actually loaded by Vim at some point. Repeat that before and after opening a file of the specific filetype you're after to confirm it was triggered by that filetype detection.

Your Answer

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

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