0

The gruvbox colorscheme and syntax highlighting for C++ doesn't highlight some elements such as:

  • string
  • vector

I have this:

enter image description here

So I decided to change the colors manually using in init.vim

syn keyword Type string
syn keyword Type vector

This method didn't work.

When I run these commands myself while editing a file, I get the following change:

enter image description here

enter image description here

It works like a charm:

enter image description here

My goal is to automatically change colors for string, vector, cout and many more keywords from init.vim, because by default they are not highlighted by gruvbox.

  • neovim version (nightly-nvim from AUR):
NVIM v0.5.0-dev+1391-g1df8a34a7
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
  • neofetch:

enter image description here

In init.vim I have:

filetype plugin indent on
syntax on

I have also tried the after-directory option. I placed a new file called after_init.vim in these directories:

  • ~/.config/nvim/after/after_init.vim
  • ~/.config/nvim/after/syntax/after_init.vim
  • ~/.vim/after/after_init.vim
  • ~/.vim/after/syntax/after_init.vim

with:

syn keyword Type string
syn keyword Type vector
echon "im loaded after init.vim"

This also did not work.

I then also tried the autocommand option in init.vim:

augroup ft_c
    autocmd!
    autocmd Syntax c syn keyword Type string
    autocmd Syntax c syn keyword Type vector
augroup end

This didn't work.

5
  • 2
    Syntax highlighting depends on the contents of the buffer being parsed according to its file type. This doesn't happen until after your vimrc has been processed. There are a lot of posts here addressing this. I'll see if I can find a good one. .... Found one linked right there in the right margin: How can I add additional syntax highlighting rules in my local vimrc?
    – B Layer
    Commented Jun 16, 2021 at 15:11
  • okey, syn doesnt work in init.vim. i saw the option with after directory (check updated answer)
    – alexzander
    Commented Jun 16, 2021 at 16:24
  • clearly im missing something... or i dont know or understand enough nvim?
    – alexzander
    Commented Jun 16, 2021 at 16:32
  • Where did you get "after_init.vim"? There's no mention of a file like that in the linked answer or in the help section you cited.
    – B Layer
    Commented Jun 16, 2021 at 17:49
  • I've made some edits to clean up the post. Please don't post images of text, unless you're demonstrating something graphical; I've changed one image to text myself, since it only contained version information.
    – D. Ben Knoble
    Commented Jun 16, 2021 at 18:14

1 Answer 1

2

The files in the syntax directory should be named after the filetype detected by Vim. In the case of your C++ code, I'd expect it would detect it as cpp. You can check that with the following command:

:set filetype?
cpp

Or you can use :set ft? for short.

Once you confirm that the filetype is indeed cpp, you can create file ~/.vim/after/syntax/cpp.vim (for Vim) or ~/.config/nvim/after/syntax/cpp.vim (for NeoVim) with the syn commands you want to load after the built-in rules are loaded:

syn keyword Type string
syn keyword Type vector
3
  • 1
    solved the problem. thank you so much. if i want for python i have to set python.vim, correct?
    – alexzander
    Commented Jun 16, 2021 at 18:41
  • Yes! python is the correct filetype for Python.
    – filbranden
    Commented Jun 16, 2021 at 20:19
  • 1
    okey. thanks man, very much appreciate.
    – alexzander
    Commented Jun 17, 2021 at 8:33

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.