Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
created June 26, 2004 · complexity basic · author joerga · version 6.0
I was getting far too many errors creating ruby cgi-scripts due to missplaced curly braces, so here's my solution.
The following ruby script generates a pattern (see below) used for highlighting a codesection from the curly brace under the cursor to the matching brace. It currently finds 12 recursions, you can generate larger patterns reaching deeper by changing the parameter (n=12) in method "generate".
# ruby script def next_iteration(text) text.gsub( /#{Regexp.quote("a%(x)*b")}/, "a%(%(a%(x)*b)|%(x))*b" ) end def generate( text, n ) 1.upto(n){ |i| text = next_iteration( text ) } text end def to_pattern(text) text.gsub(/x/,"\\n|[^ab]").gsub(/a/,"\\{").gsub(/b/,"\\}") end ax = generate("a%(x)*b", 12).sub(/b$/,"") # a = starting brace, b = ending brace, x = characters in between puts to_pattern( "/\\v%(%##{ax}b)|%(#{ax}%#b)/" )
"a" and "b" are synonyms for the starting/ending brace, "x" holds the characters in between, if any. Using recursion, the original pattern "a(x)*b" is expanded to the monstrous pattern seen below.
Note: Use %() for grouping in the pattern, as the normal (captivating) variant cannot be used that often in one pattern.
Append the following to your vimrc:
highlight ShowMatches guibg=darkgrey guifg=white au! Cursorhold * exe 'match ShowMatches /\v%(%#\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(\n|[^\{\}])*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(%(\{%(\n|[^\{\}])*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*\})|%(\n|[^\{\}]))*%#\})/' set ut=30
Up to now, creating a pattern for mixing curly braces and brackets hasn't been very successful. The resulting patterns grow far too fast.
See also
Comments
TODO: Is the above useful for Vim 7?
- It is not the same thing as matchparen
- --Luc Hermitte 14:11, 7 September 2007 (UTC)
I tried this and in certain C files I am getting ERROR
E363: pattern caused out-of-stack error
It crashes Vim 6.3
Work around for Visual Mode (with mouse):
1. Do not add the ruby script to your vimrc
2. If you want to see all the code inside a curly brace, double-click on the opening { and vim will highlight everything until the matching closing }.
If the cursor is at a bracket, just type v%
v selects visual mode), % finds a matching bracket.
Otherwise, type %v%
The first % finds a bracket, then v% selects to the matching bracket.
Type vaB or viB to select the whole block with braces (for aB), or only inside the braces (for iB).