12

In vi it's very helpful to be able to place the cursor on a '(' or '{' or '[' character, press the '%' key, and move to the matching ')', '}', or ']'.

But this does not work for me with angle brackets ( '<' and '>' ), even though page 130 of my "Learning the vi Editor" O'Reilly book (6th edition) says it should! I am using Centos 7.2 and my 'vi' editor is actually vim 7.4.160.

Is this a version-specific thing? Or is there some switch that I can set/clear to make this work? It'd be handy for trying to make sense of HTML and Javascript.

2
  • What is 'matchpairs' set to? You can find out by doing: :verbose set matchpairs?. See :h 'matchpairs' for more information Commented Sep 28, 2016 at 17:09
  • When I try that I see: " matchpairs=(:),{:},[:] ", which looks promising. How can I alter this? Commented Sep 28, 2016 at 17:15

1 Answer 1

14

'matchpairs' controls what characters form pairs which % will work upon. You can add angle brackets by doing the following command as suggested by :h 'matchpairs':

:set matchpairs+=<:>

Since 'matchpairs' is a buffer local setting it would be best do this for the filetype you want. An example of for cpp filetype which can be added to your vimrc file:

augroup AngleBrackets
    autocmd!
    autocmd FileType cpp set matchpairs+=<:>;
augroup END

However my preferred method of setting filetype specific options is to use the after-directory. Add the following to ~/.vim/after/ftplugin/cpp.vim:

set matchpairs+=<:>

Note: These examples use cpp as the filetype. You can use a different filetype to fit your needs.

If you truly want to make this a global change no matter the filetype add the following to your vimrc file:

setglobal matchpairs+=<:>

For more help see:

:h 'matchpairs'
:h local-options
:h 'filetype'
:h :autocmd
:h after-directory
:h setglobal
1
  • Thanks again. It looks like my 'vi' has a hard time displaying help text, I will see if I can improve that. Commented Sep 28, 2016 at 17:28

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.