https://github.com/exceedsystem/vscode-macros
This extension gives the macro features to your vscode.
https://github.com/exceedsystem/vscode-macros
javascript macro vscode vscode-extension
Last synced: about 1 month ago
JSON representation
This extension gives the macro features to your vscode.
- Host: GitHub
- URL: https://github.com/exceedsystem/vscode-macros
- Owner: exceedsystem
- License: mit
- Created: 2020-12-06T03:20:45.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-20T23:07:52.000Z (over 1 year ago)
- Last Synced: 2024-10-21T03:08:40.194Z (over 1 year ago)
- Topics: javascript, macro, vscode, vscode-extension
- Language: TypeScript
- Homepage: https://marketplace.visualstudio.com/items?itemName=EXCEEDSYSTEM.vscode-macros
- Size: 255 KB
- Stars: 41
- Watchers: 2
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# VSCode Macros
[日本語版README](README_ja.md)
## Add scripting macro functionality to your VSCode
Enhance your work efficiency and productivity by automating repetitive text editing operations.
### Features
* Write macros in JavaScript
* Utilize the VSCode extension API in your macros
(Some limitations apply due to API specifications)
* Use Node.js modules in your macros
* Run macros from the command palette or with custom keyboard shortcuts
* Organize macros into separate files for easy management
* Debug macros using VSCode's built-in debugger
### Demos
* Write and run your macro instantly

* Switch between macro files and apply multiple macros in combination

* Debug your macros with VSCode's debugger

### Setting up the extension
1. Create a folder in any location to store your macro files.

2. Open the folder with VSCode.

3. Create a new file and paste the example macro code below.
Macros are written in JavaScript(CommonJS).

```javascript
const vscode = require('vscode');
/**
* Macro configuration settings
* { [name: string]: { ... Name of the macro
* no: number, ... Order of the macro
* func: ()=> string | undefined ... Name of the body of the macro function
* }
* }
*/
module.exports.macroCommands = {
FooMacro: {
no: 2,
func: fooFunc
},
BarMacro: {
no: 1,
func: barFunc
}
};
/**
* FooMacro
*/
function fooFunc() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
// Return an error message if necessary.
return 'Editor is not opening.';
}
const document = editor.document;
const selection = editor.selection;
const text = document.getText(selection);
if (text.length > 0) {
editor.edit(editBuilder => {
// To surround a selected text in double quotes(Multi selection is not supported).
editBuilder.replace(selection, `"${text}"`);
});
}
}
/**
* BarMacro(asynchronous)
*/
async function barFunc() {
await vscode.window.showInformationMessage('Hello VSCode Macros!');
// Returns nothing when successful.
}
```
To create a new macro quickly, you can use VSCode's [snippet feature](https://code.visualstudio.com/docs/editor/userdefinedsnippets#\_create-your-own-snippets) with a snippet like this:
```json
{
"Create new VSCode macro": {
"prefix": "vscmacro",
"body": [
"const vscode = require('vscode');",
"",
"/**",
" * Macro configuration settings",
" * { [name: string]: { ... Name of the macro",
" * no: number, ... Order of the macro",
" * func: ()=> string | undefined ... Name of the body of the macro function",
" * }",
" * }",
" */",
"module.exports.macroCommands = {",
" $1: {",
" no: 1,",
" func: $2,",
" },",
"};",
"",
"/**",
" * Hello world",
" */",
"async function $2() {",
" const editor = vscode.window.activeTextEditor;",
" if (!editor) {",
" // Return an error message if necessary.",
" return 'Active text editor not found.';",
" }",
" const document = editor.document;",
" const selection = editor.selection;",
" const text = document.getText(selection);",
"",
" editor.edit((editBuilder) => {",
" editBuilder.replace(selection, `Hello world! \\${text}`);",
" });",
"}"
],
"description": "VSCode Macros Template"
}
}
```
4. Give the file a name of your choice (*.js) and save it in the macro folder.

5. Open VSCode's settings, search for "vscode macros", and enter the macro file path in the 'Macro File Path' text box.
> __NOTE:__ Version 1.3.0 and above support multi-root, workspace, and workspace folders.

* You can use environment variables in the macro file path, such as `{ENV_NAME}/path/to/macro.js`.
* For VSCode portable mode version 1.3.0 and later, use the `{VSCODE_PORTABLE}` environment variable.
> __NOTE:__ When using this extension in portable mode, set the relative path to the data directory up to version 1.2.0. In version 1.3.0 and later, use the environment variable instead of the relative path.
### Assigning frequently used macros to user commands
1. Open VSCode's settings, search for "vscode macros", then click `{Edit in settings.json}` in the `User Macro Commands` field.
> __NOTE:__ Version 1.3.0 and above support multi-root, workspace, and workspace folders.

2. Register the macro path and name in the JSON file as shown below. (Up to 10 commands)
```json
"vscodemacros.userMacroCommands": [
{
"path": "C:\\Temp\\macros\\FooMacro.js",
"name": "FooMacro"
},
{
"path": "C:\\Temp\\macros\\BarMacro.js",
"name": "BarMacro"
},
{
"path": "",
"name": ""
},
{
"path": "",
"name": ""
},
{
"path": "",
"name": ""
},
],
```
### Using your macros
You can run your macros from the command palette:
1. Press `{F1}` to open the command palette, type "run a macro", and press `{Enter}`.

2. Select the macro name from the list.

To switch to another macro file, use the "select a macro file" command.

You can assign your macros to `User Macro 01` through `User Macro 10`:
1. Press `{F1}` to open the command palette, then type "vscmacros".

2. Click the `{⚙}` icon of the user command you want to assign a shortcut key to.

* You can also run a `User Macro` from the `Run a User Macro` command.
(This is helpful if you forget your user macro settings.)

> __NOTE:__ Available in version 1.4.0 and above.
### Debugging your macros
You can debug your macros in the `extension development host` on VSCode:
1. Open the macro folder with VSCode.

2. Open the macro file and set breakpoints as needed.

3. Press `{F5}` and select `VS Code Extension Development (preview)` from the environment list.

For older VSCode versions, run the __Extension Development Host__ from the command palette:

4. In the __Extension Development Host__ window, press `{F1}` to open the command palette, then type 'run a macro'.

5. Select the macro you want to debug from the list.

6. The program will stop at the breakpoint, allowing you to debug.

### Macro examples
You can find examples of VSCode macros on GitHub Gist: