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());