1

I already have syntax highlighting for javascript, but what I'd like to do is extend it with some additional rules.

What I've been trying to start, is using highlight "Prototype" with a cyan background.

I tried following some other answers, but they completely replace the syntax highlighting, but I want to simply add on to the current syntax highlighting.

Ideally this would be separated into their appropriate files, ie. highlighting rules in the syntax folder or whatever is appropriate.


This is what I currently have and it doesn't highlight Prototype on my attack.js file:

~/.vim/syntax/js.vim:

syn match sophiePrototype "Prototype"
highlight sophiePrototype ctermfg=cyan guifg=#00ffff

2 Answers 2

4
  1. The correct filetype for JavaScript is javascript, so your file, which corresponds to a non-existing js filetpe, will never be sourced automatically. It should be:

    ~/.vim/syntax/javascript.vim
    
  2. Vim sources syntax scripts from three standard location in a specific order:

    ~/.vim/syntax/javascript.vim
    $VIMRUNTIME/syntax/javascript.vim
    ~/.vim/after/syntax/javascript.vim
    

    Because you want your syntax highlighting to extend the existing one, you should use the last location:

    ~/.vim/after/syntax/javascript.vim
    
  3. Given your use case, :help :syn-keyword seems more appropriate than :help :syn-match.

    syn keyword sophiePrototype Prototype
    
  4. It is considered best practice to separate the definition of the visual attributes of syntax highlighting from the definition of the highlight groups themselves.

    If your goal is to have Prototype marked as some kind of "keyword", then you should probably use the default Keyword highlight group, which is likely to already be handled by your colorscheme:

    syn keyword Keyword Prototype
    

    If your goal is to give your keywords their own highlight group while still having them look like an existing highlight group, you can use a link:

    syn keyword sophiePrototype Prototype
    highlight link sophiePrototype SomeOtherHighlightGroup
    

    If your goal is really to use a new highlight group that doesn't look like anything else for Prototype, then you should move that :highlight command to a proper colorscheme or, barring that, to your vimrc, as per :help colorscheme-override:

    augroup my_colorschemes
        autocmd!
        autocmd Colorscheme * highlight sophiePrototype ctermfg=cyan guifg=#00ffff
    augroup END
    
7
  • I was wondering why you hadn't answered this :-)
    – Friedrich
    Nov 23, 2023 at 7:42
  • Wrong time zone ` ¯\_(ツ)_/¯ `.
    – romainl
    Nov 23, 2023 at 7:47
  • This is amazing! Thanks! It works as the question I asked. Dec 7, 2023 at 17:10
  • One side question I have is, how do I "add-to" a "contains" list? I want to do "Prototype" as something contained within a comment, but I don't want to remove the older "contains" items, like "TODO". Dec 7, 2023 at 17:13
  • 1
    Please ask separate questions. It will make things easier to handle for everyone.
    – romainl
    Dec 7, 2023 at 18:51
2

Your solution is already pretty close. However, there are three minor issues with it:

  1. The filetype for JavaScript is called "javascript", not "js". That's what your file is supposed to be called.
  2. To change the background, you would use the *bg options, i.e. ctermbg and guibg.
  3. To extend default syntax highlighting rather than replacing it, put a file in your "after" directory as explained in :help after-directory. Vim will load regular JavaScript syntax first.

The following lines in ~/.vim/after/syntax/javascript.vim will highlight "Prototype" with a cyan background:

syn match sophiePrototype "Prototype"
highlight sophiePrototype ctermbg=cyan guibg=#00ffff
1
  • 1
    This was also helpful. It explains the errors I was making and some additional details. Dec 7, 2023 at 17:23

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.