History Report a problem
Article Edit this page Discussion

Highlighting source between matching curly braces

From Vim Tips Wiki

Jump to: navigation, search

[edit] Duplicate tip

This tip is very similar to the following:

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

Tip 754 Previous TipNext Tip

Created: June 26, 2004 Complexity: basic Author: joerga Minimum version: 6.0 Karma: -1/3 Imported from: Tip#754


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.

[edit] See also

[edit] 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


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Highlighting source between matching curly braces"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation