You can achieve this easily using either the plugin matchit (distributed with vim, just use packadd! matchit
) or match-up (external plugin). The latter provides enhanced text objects and also enables highlighting of the words.
To define the matches for cmake, create a file .vim/after/ftplugin/cmake.vim
containing:
if exists('loaded_matchit')
let b:match_words = '\<if\>:\<elseif\>\|\<else\>:\<endif\>'
\ . ',\<foreach\>\|\<while\>:\<break\>:\<endforeach\>\|\<endwhile\>'
\ . ',\<macro\>:\<endmacro\>'
\ . ',\<function\>:\<endfunction\>'
let b:match_ignorecase = 1
let b:undo_ftplugin .= "| unlet b:match_words"
endif
This should work with either plugin.