Bracket Commands VS Code Extension
This Visual Studio Code extension adds a set of commands to make working with
bracket pairs easier. You can quickly wrap selections in your favorite bracket
types, cycle through bracket pairs, and remove the nearest pair without
disturbing nested structures.
Features
Add brackets
The extension contributes four commands that wrap the current selection (or
multiple selections) in the chosen bracket type:
| Command |
Description |
Bracket Commands: Add Parentheses |
Wraps selections in () |
Bracket Commands: Add Square Brackets |
Wraps selections in [] |
Bracket Commands: Add Curly Braces |
Wraps selections in {} |
Bracket Commands: Add Angle Brackets |
Wraps selections in <> |
Bracket Commands: Add Any Bracket |
Prompts for any bracket pair (choose from existing or custom) |
You can bind these commands to any key of your choosing via
Preferences > Keyboard Shortcuts. When invoked, each selection is replaced
with the same text surrounded by the appropriate open and close character.
Cycle bracket types
Bracket Commands: Cycle Bracket Type identifies the nearest bracket pair
surrounding the cursor and replaces it with the next pair in the configured
sequence. For example, if your configured pairs are (), [], {} and <>
(the default), running the command while inside a set of parentheses will
convert them to square brackets. Run it again to cycle to curly braces, and so
on.
Remove brackets
Five commands let you remove bracket pairs:
Bracket Commands: Remove Parentheses
Bracket Commands: Remove Square Brackets
Bracket Commands: Remove Curly Braces
Bracket Commands: Remove Angle Brackets
Bracket Commands: Remove Nearest Bracket Pair
Add any bracket pair
Bracket Commands: Add Any Bracket displays a pick list of your configured
bracket pairs. Choose one to wrap the current selection or pick Custom. to
enter your own two-character pair. This allows you to quickly insert less
common pairs without changing the configuration.
Each command searches to the left and right of the cursor for the nearest
matching bracket pair. Only brackets of the requested type are considered
except for the nearest command, which will remove whichever pair is closest.
Nested pairs of other types are ignored. For example, if your text contains
{ [ <cursor> ] } and you execute Remove Curly Braces, the command will
ignore the square brackets and remove the surrounding curly braces instead.
Extension settings
You can control which bracket types are cycled or removed when using
Cycle Bracket Type or Remove Nearest Bracket Pair via the
bracketCommands.pairs setting. Set this to an array of two-character strings
in your settings.json. The default value is:
"bracketCommands.pairs": ["()", "[]", "{}", "<>"]
The order of the entries determines the cycling sequence.
Requirements
None.
Packaging and publishing
- Run
npm install to install dependencies.
- Compile the TypeScript using
npm run compile.
- Package the extension with
npx vsce package.
- Publish with
npx vsce publish after confirming the publisher and version.
The files included in the VSIX are controlled by .vscodeignore.
License
This project is released under the MIT License.
Development notes
The core functionality is implemented in src/extension.ts. Commands are
registered using vscode.commands.registerTextEditorCommand, which provides
direct access to the active editor and an edit builder. According to
VS Code API guidelines, you make edits by invoking TextEditor.edit with a
callback that receives an editBuilder. Inside the callback, the extension
replaces selections with bracketed versions or deletes the identified brackets.
The scanning algorithm for removing and cycling brackets searches left and
right from the cursor for the nearest matching open and close characters while
respecting nesting of the same bracket type. Other bracket types are skipped
so that commands for specific types only operate on the requested pair.
Known issues
None.
Release notes
1.0.1