0

Background

When working on .c and .h files, I have a couple options turned on, namely:

filetype plugin on
filetype indent on
autocmd BufEnter *.c   setlocal tabstop=8 shiftwidth=8 softtabstop=8 textwidth=80 noexpandtab cindent cinoptions=:0,l1,t0,g0,(0

This enables a really useful feature for me: when I move the cursor over a bracket ([, ]), parenthesis ((, )), or brace ({, }) character, the matching "partner" (assuming my code is syntactically correct) is highlighted, and I can jump between the "partners" via the % key.


Problem

I edit a lot of GNU M4 files, and the most commonly used delimiter/bracket-like characters are the back-tick (`) and single-quote (') characters. Unfortunately, I cannot highlight and/or jump between pairs like I can with brackets, braces, etc.


Question

  1. Is it possible to enable the matching/highlighting/jumping-to-partner features I've described for arbitrary pairs of symbols, assuming that start/stop characters are not the same (i.e. backtick and single-quote, in my case)?
  2. Is it possible to enable the matching/highlighting/jumping-to-partner features I've described for arbitrary pairs of symbols, assuming that start/stop characters are the same (i.e. a pair of double-quotation marks)?
  3. Does VI/VIM have the ability to detect escaped symbols, i.e.: could it match a pair of double quotation marks ("), but know to ignore double-quotes with a backslash immediately to the left (i.e. \")?
1

For question 1, try this:

set matchpairs+=`:'

See :help 'matchpairs'

This requires the characters to be different, so it won't help you with question 2 (or indeed 3).

For these latter two requirements, the short answer is that Vim doesn't provide this feature by default, and it will be tricky to implement well because it requires a greater understanding of context (and, ideally syntax,) than Vim currently has.

As a related aside, if you want to match things that aren't single characters, see also :help matchit.

  • This, combined with the ShowPairs plugin just made my day a lot easier. I had to increase the max recursion depth of ShowPairs, but that's it. Thanks! – DevNull Mar 16 '18 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.