Matching Paired Punctuation | CodeGuru

Matching Paired Punctuation

At least in my opinion, the largest feature lacking from the Visual Studio editing environment is emacs-style paren-matching. While it is impossible (so far as I know) to do *real* highlighting back to the matching punctuation pair (in VStudio-macro-land, changes of selection aren’t reflected in a screen update until end-of-macro), it is possible to get […]

Written By
CodeGuru Staff
CodeGuru Staff
May 2, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

At least in my opinion, the largest feature lacking from the Visual
Studio editing environment is emacs-style paren-matching. While it is
impossible (so far as I know) to do *real* highlighting back to the
matching punctuation pair (in VStudio-macro-land, changes of selection
aren’t reflected in a screen update until end-of-macro), it is
possible to get the next best thing.

As is documented in each of the ‘NewRightPunctuation’ functions, simply
rebind each of the right paren, right bracket, and right curly brace
keys to these new functions. They will insert the character, call the
Visual Studio function to match on back to the correct character,
blink around it ‘a bit’, and then hop on back to the correct point to
continue typing.

The only caveat? The number of times to blink around is *very*
machine-dependant. Fifty seems to work well on a P2-400; alter to your
own preferences. It’s in the MatchEnclosing() function’s call to
SwapAroundCharacter(n) — just change ‘n’.

Sub NewRightParen()
‘DESCRIPTION This should be bound to ‘)’ in order to gain the matching
 InsertCharAndMatchEnclosing(“)”)
End Sub
Sub NewRightBracket()
‘DESCRIPTION This should be bound to ‘]’ in order to gain the matching
 InsertCharAndMatchEnclosing(“]”)
End Sub
Sub NewRightCurlyBrace()
‘DESCRIPTION This should be bound to ‘}’ in order to gain the matching
 InsertCharAndMatchEnclosing(“}”)
End Sub
Sub InsertCharAndMatchEnclosing(ch)
 ActiveDocument.Selection = ch
 MatchEnclosing()
End Sub
Sub MatchEnclosing()
‘DESCRIPTION Blinks around the matching punctuation and then hops on back
 lCurrentColumn = ActiveDocument.Selection.CurrentColumn
 lCurrentLine = ActiveDocument.Selection.CurrentLine
 ExecuteCommand “GoToMatchBrace”
 SwapAroundCharacter(50)
 ActiveDocument.Selection.MoveTo lCurrentLine, lCurrentColumn
End Sub
Sub SwapAroundCharacter(numIterations)
 nIterator = 0
 While nIterator < numIterations
 nIterator = nIterator + 1
 ActiveDocument.Selection.CharRight dsExtend
 ActiveDocument.Selection.CharLeft dsExtend
 Wend
End Sub

Again, to install these, simply save them into a macro file, load it


into Visual Studio, then go to Tools->Customize. If you select the


Keyboard tabbie and then change the category to Macros, the NewRight*


functions should appear. I

d recommend changing the Editor field to


Text and then going on ahead and binding the right bracket, right


curly, and right paren keys to each of the appropriate macros.

Date Posted May 2, 1999

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.