1

I want to configure and customize my own syntax highlight for Vim editor in Linux Ubuntu 20.04. In my home directory, I created two files, ~/.vimrc and ~/.vim/colors/mycolors.vim. Here is the full contents of these files:

~/.vimrc

colorscheme mycolors
syntax enable

filetype plugin indent on

" Rules for syntax highlighting for each line type
" Defining custom highlight groups
" For Bash
autocmd FileType sh
    \ syn match RedString '".*"' containedin=ALL

" For Node.js
autocmd FileType javascript
    \ syn match NodeJSString "'.*'" containedin=ALL

" For C
autocmd FileType c
    \ syn match BlueString '".*"' containedin=ALL

" For C++
autocmd FileType cpp
    \ syn match GreenString '".*"' containedin=ALL


" Syntax highlighting rules for comments, classes, objects and so on in Node.js
autocmd FileType javascript
    \ syn keyword NodeComments jscomment contained
    \ syn keyword NodeClass jsclass contained
    \ syn keyword NodeObject jsthis contained
    \ syn keyword NodeFunction jsfunction contained

~/.vim/colors/mycolors.vim

" Vim color file
" Maintainer:   Bram Moolenaar <[email protected]>
" Last Change:  2001 Jul 23

" This is the default color scheme.  It doesn't define the Normal
" highlighting, it uses whatever the colors used to be.

" Set 'background' back to the default.  The value can't always be estimated
" and is then guessed.
highlight clear Normal
set bg&

" Remove all existing highlighting and set the defaults.
hi clear

" Load the syntax highlighting defaults, if it's enabled.
if exists("syntax_on")
    syntax reset
endif

" User configurations ==========================================

highlight Normal                ctermfg=102        ctermbg=232
highlight Comment               ctermfg=6          ctermbg=0
highlight Function              ctermfg=13         ctermbg=0

" Syntax highlighting rules for each files types
highlight RedString             ctermfg=red
highlight NodeJSString          ctermfg=137
highlight BlueString            ctermfg=blue
highlight GreenString           ctermfg=green

" Syntax highlighting rules for comments, classes, objects and functions in Node.js
highlight jscomments            ctermfg=11
highlight jsclass               ctermfg=79
highlight jsthis                ctermfg=44
highlight jsfunction            ctermfg=229

When I create any JavaScript file the above configuration didn't work. Does anyone know why my syntax highlighting is not working? What am I doing wrong?

Thank you in advance!

P.S.

JavaScript Example:

app.js

#!/usr/bin/env node

fs = require('fs');

console.log('Hello from Node.js');

class Person {
        constructor(name, age)
        {
                this.name = name;
                this.age = age;
        }

        sayHello()
        {
                console.log('Hello, my name is ${this.name} and I am ${this.age} years old');        
                console.log("Hello, my name is ${this.name} and I am ${this.age} years old");
        }
}

const person1 = new Person("Alice", 30);
person1.sayHello();


// read data from file
const data = fs.readFileSync("./data.txt");

/* printing the data from file to terminal  */
console.log(data.toString());

1 Answer 1

1

You have this line in your colorscheme:

highlight jscomments ctermfg=11

but the jscomments highlight group is not defined anywhere so you can't expect any text with the highlight group jscomments to be highlighted in bright yellow. The same applies to jsclass, jsthis, and jsfunction.

On the other hand, the following lines work as expected because Normal, Comment, and Function are default highlight groups that don't need to be defined:

highlight Normal   ctermfg=102 ctermbg=232
highlight Comment  ctermfg=6   ctermbg=0
highlight Function ctermfg=13  ctermbg=0

And the following line works, too:

highlight NodeJSString ctermfg=137

because the NodeJSString highlight group is defined in your vimrc:

syn match NodeJSString "'.*'" containedin=ALL

The problem seems to be that you don't quite understand how :help :syn-keyword works. In your vimrc, you have this command:

syn keyword NodeComments jscomment contained

which says:

  • create a NodeComments keyword-based highlight group,
  • for the keyword jscomment,
  • when it appears within another highlight group set to contain NodeComments specifically or ALL highlight groups.

FWIW, the line above "works"…

  • if you also define a suitable "parent", like:

    syn region FOO start=/§§/ end=/¶¶/ contains=NodeComments
    
  • and if you also set suitable style attributes in your colorscheme:

    highlight NodeComments ctermfg=11
    

works

But something tells me that this is not what you are trying to achieve, here, and we have no idea what you are actually trying to achieve because you kept that from the question. Therefore I am not sure what to suggest other than reading the documentation more carefully and providing more context if you really want our help.


Following-up…

Languages typically have a few keywords that are best expressed as a list. :help :syn-keyword is used for highlighting those language-level keywords so its value is a list of keywords to match literally.

For example, JavaScript (or rather ECMAScript) has a limited set of keywords and the built-in JavaScript syntax script at $VIMRUNTIME/syntax/javascript.vim has several lines like the one below to match all the built-in keywords:

syn keyword javaScriptConditional if else switch

With that line, every switch in your code will be matched as a javaScriptConditional keyword.

To come back to the sample you provided, the following command:

syn keyword NodeComments jscomment contained

is rather pointless because all you can expect it to do is highlight the word jscomment. Again, maybe that's what you want to do but I very much doubt it.

That "keyword" mechanism works well for fixed lists of keywords and that's about it. Since you can't realistically hope to list every possible comment or function name you must use a different regexp-based mechanism, like :help :syn-match (good for function names) or :help :syn-region (good for comments).

I would suggest you read the documentation more carefully and refer to existing syntax scripts before trying random things. Also, filling up your vimrc with autocommands is a terrible way to define/override the syntax of a language. ~/.vim/syntax/foo.vim or ~/.vim/after/syntax/foo.vim would be more appropriate.

3
  • First of all, I would like to thank you for trying to understand and help. I will try to explain it in more detail. I'm trying to configure custom colors in my ~/.vimrc file, which will be set up in the ~/.vim/colors/mycolors.vim file using keywords defined in the ~/.vimrc file. For example, let's say my keyword in the ~/.vimrc file is "NodeSystemFunctions," and its highlighting will be applied to function names like "require" and "log."
    – morris
    Aug 23, 2023 at 17:08
  • For example: If I define a keyword in the ~/.vimrc file, then I want to use with this keyword to change the colors of specific words that will be used in the Vim editor. That is, the names of functions will be the color that I defined for *.js files. Thank you so much romainl
    – morris
    Aug 23, 2023 at 17:21
  • Tank you romainl
    – morris
    Aug 23, 2023 at 17:37

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