Vim Tips Wiki
Register
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 754 Printable Monobook Previous Next

created June 26, 2004 · complexity basic · author joerga · version 6.0


I was getting far too many errors creating ruby cgi-scripts due to misplaced 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

 TO DO 
Is the above useful for Vim 7?

It is not the same thing as matchparen
--Luc Hermitte 14:11, 7 September 2007 (UTC)

Need to clarify what the tip actually does (a reader shouldn't have to decode stuff to work this out). What's that ruby script for, and why does it start the tip? Is this tip superseded by rainbow_parenthesis?

Is rainbow_parenthesis just script#1561? The text on that page doesn't give much of a description. Someone needs to install it and tell readers what it does. Or, maybe we should delete this tip if no one wants to fix it by, say, July 2008. --JohnBeckett 01:58, 23 April 2008 (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


Advertisement