nordtheme / vim

An arctic, north-bluish clean and elegant Vim theme.
https://www.nordtheme.com/ports/vim
MIT License
2.51k stars 275 forks source link

How do I extend this plugin for variable names &c? #277

Closed norogoth closed 2 years ago

norogoth commented 2 years ago

I would like to add high-lighting for things like the "my" and the "$variable" below:

my $variable = 'my string';

How can I extend the syntax high-lighting to target elements like these? I can see where the gui colors are defined so I just need to know how to apply one of those colors.

arcticicestudio commented 2 years ago

Hi @norogoth :wave:

This project is "just" a theme that defines the style, e.g. the (fore/back)ground color or font style (bold etc.) for existing syntax group. These are either the ones builtin into Vim itself or ones that are defined by external scripts a.k.a. "plugins".

Therefore you need to know which language your syntax is from, e.g. the $ character is a well-known element for variables in PHP. Afterwards you should check if this language is already supported out-of-the-box by Vim or if you need to install a plugin.

The actual logic how to identify a language token is done by Vim through a parser that uses regular expressions to match elements. These regex pattern are given a name which, like described in the first paragraph, are the so called "syntax groups". As an example you can check out the definitions for PHP which are bundled with Vim.

If you'd like to create your own syntax groups for a new language, you'll have to define the regular expressions to match those elements. You can check out the official Vim syntax documentation to learn about this feature or check out guides like these:

Note that this task can be tricky and requires time and effort to learn how to write stable regular expressions. Upcoming ways of better syntax highlighting are projects like tree-sitter which are natively supported by other Vim distributions like Neovim.

norogoth commented 2 years ago

OK cool. So I am actually coding in Perl in Neovim. I found this documentation for Perl's built-in support for Perl. https://github.com/vim/vim/blob/34cc7d8c034f2bc5b57455577051db8d72e2b87c/runtime/syntax/perl.vim

So if I wanted to make "my" a light-orange or something, would I use perlStatementStorage ? If so, where would I put it in my nord-vim fork?

Thanks for great response-time and thorough answers.

arcticicestudio commented 2 years ago

The easiest and recommended way is to use Vim's autocmd feature and define an augroup that applies commands only for the nord theme. For details please see Nord Vim's official documentation about theme customization.

As an example for your use case the following should work:

augroup nord-theme-overrides
  autocmd!
  " Use `nord12` ("orange") as foreground color for Perl storage statements.
  autocmd ColorScheme nord highlight perlStatementStorage ctermfg=11 guifg=#d08770
augroup END
norogoth commented 2 years ago

Fantastic. Wonderful response. Worked immediately. Clear where to go from here.

norogoth commented 2 years ago

If you ever want to add something like this to the official nord vim this is what I did for Perl.

" MY OVERRIDES
augroup nord-theme-overrides
  autocmd!
  " Use `nord12` ("orange") as foreground color for Perl storage statements.
  autocmd ColorScheme nord highlight perlStatementStorage ctermfg=11 guifg=#EBCB8B
  autocmd ColorScheme nord highlight perlConditional ctermfg=11 guifg=#FBAFB6
  autocmd ColorScheme nord highlight perlRepeat ctermfg=11 guifg=#E28991
  autocmd ColorScheme nord highlight perlVarPlain ctermfg=11 guifg=#A6D2FD
  autocmd ColorScheme nord highlight perlSubName ctermfg=11 guifg=#EBCB8B
  autocmd ColorScheme nord highlight perlBracesDQ ctermfg=11 guifg=#EBCB8B
  autocmd ColorScheme nord highlight perlMethod ctermfg=11 guifg=#F5AB94
  autocmd ColorScheme nord highlight perlVarSimpleMember ctermfg=11 guifg=#B48EAD
augroup END

For the official setup you can do something more official but those groups are the most important ones I saw.

norogoth commented 2 years ago

Oh and obivously here I did not change the term colors.