Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim script: check if the current word is/isn't a C/C++ keyword

Tags:

vim

vim-plugin

I'm working on a small vim plugin which should highlight current word occurrences after cursor idle. I have a highlight part, and what I need is to check if the current word is not a C/C++ keyword.

like image 794
Davit Samvelyan Avatar asked Dec 04 '25 13:12

Davit Samvelyan


1 Answers

You can do that depending on current syntax highlight script. Say, this function returns if your cursor is on some access modifier (public, protected, etc), or some C++ type (say, bool, etc) :

function! IsCppAccessOrType()
   return match(synIDattr(synID(line("."), col("."), 1), "name"), '\v\CcppAccess|cppType') >= 0
endfunction

But, again, this will completely depend on current syntax script. Example above depends on standard syntax/cpp.vim provided with Vim 7.3 .


By the way, there is very useful trick to deal with syntax highlight stuff:

" Show syntax highlighting groups for word under cursor 
nnoremap <silent> <F10> :call <SID>SynStack()<CR>
function! <SID>SynStack()
   if !exists("*synstack")
      return
   endif
   echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

Add this to your vimrc, and when you press F10, stack of syntax items will be echoed ( see :help synstack for more details ).

I have found this trick somewhere on http://vimbits.com .

like image 193
Dmitry Frank Avatar answered Dec 07 '25 03:12

Dmitry Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!