Keyboard Macro BetaWith this Visual Studio Code extension, you can record and playback your keyboard inputs.
IMPORTANT NOTE
YOU CAN RECORD (basically):
YOU CANNOT RECORD (mainly due to the lack of capabilities of VS Code API to capture them):
How to enable custom keybindings to be recordedIf you found your favorite keyboard shortcuts are not supported to be recorded, you could make them recordable by adding corresponding wrappers to your If you have a keyboard shortcut like:
the corresponding wrapper should look like below.
Add this keybinding rule below the original one in the The following are the details:
How to enable your favorite keymap extension to be recordedSee Keymap Wrappers. The
|
Keyword | Event to wait for to happen |
---|---|
'document' |
Changes in the text document of the active editor |
'selection' |
Changes of the selection (typically, cursor movement) in the active editor |
'clipboard' |
Changes of the clipboard text |
If multiple keywords are specified in an args.await
parameter, it means all of them are expected to happen.
If a target command does not always reproduce the specified side effects, it is okay since the playback mechanism has a timeout, which is 300 milliseconds.
How to customize shortcut keys for recording and playback
This is the default keybinding set for recording/playback of this extension. Copy and paste this into your keybindings.json
and modify it as you like.
{
"key": "ctrl+alt+r",
"command": "kb-macro.startRecording",
"when": "!kb-macro.recording"
},
{
"key": "ctrl+alt+r",
"command": "kb-macro.finishRecording",
"when": "kb-macro.recording"
},
{
"key": "ctrl+alt+p",
"command": "kb-macro.playback",
"when": "!kb-macro.recording"
},
{
"key": "ctrl+alt+p",
"command": "kb-macro.cancelRecording",
"when": "kb-macro.recording"
},
{
"key": "escape",
"command": "kb-macro.abortPlayback",
"when": "kb-macro.playing"
}
How to save the recorded sequence for future use
After you have successfully recorded a sequence, you may also save it for future use in another VS Code session.
- Open the Command Palette by
ctrl+shift+p
(cmd+shift+p
for Mac). - Search for the command
Keyboard Macro: Copy Macro as Keybinding
and execute it. It copies the sequence in keybinding JSON format to the clipboard. - Open your
keybindings.json
(Keyboard Shortcuts JSON). - Paste the JSON at the appropriate location (typically the last in the file).
- Fill the
key
property which is left blank with a new keystroke (e.g."key": "ctrl+alt+z"
).
Tips
Task | Recordable ways to do it |
---|---|
Move focus to editor | Instead of mouse, use ctrl+1 (cmd+1 for Mac) |
Switch editor tabs | Instead of ctrl+tab , use ctrl+pageup /ctrl+pagedown (alt+cmd+left /alt+cmd+right for Mac) |
Commands
For recording
Command name | Command ID | Function |
---|---|---|
Start Recording |
kb-macro.startRecording |
Start recording |
Finish Recording |
kb-macro.finishRecording |
Stop recording |
Cancel Recording |
kb-macro.cancelRecording |
Stop recording and discard the recorded sequence |
Copy Macro as Keybinding |
kb-macro.copyMacroAsKeybinding |
Copy the last recorded macro as keybinding JSON format to the clipboard |
For playback
Command name | Command ID | Function |
---|---|---|
Playback |
kb-macro.playback |
Perform playback of the last recorded sequence |
Abort Playback |
kb-macro.abortPlayback |
Abort currently-running playback |
Repeat Playback |
kb-macro.repeatPlayback |
Perform playback specified number of times |
Repeat Playback Till End of File |
kb-macro.repeatPlaybackTillEndOfFile |
Repeat playback until the cursor reaches the end of the file |
The kb-macro.repeatPlayback
command shows an input box to specify the number of times.
The kb-macro.playback
command has an optional repeat
argument to specify the number of times to repeat.
{
// Example: Repeat playback 5 times.
"key": "ctrl+alt+5",
"command": "kb-macro.playback",
"args": {
"repeat": 5
}
}
The kb-macro.playback
command has an optional sequence
argument to specify a command sequence to playback instead of the last recorded sequence.
{
// Example: Duplicate current line as a comment
"key": "ctrl+alt+up",
"command": "kb-macro.playback",
"args": {
"sequence": [
{ "command": "editor.action.addCommentLine" },
{ "command": "editor.action.copyLinesUpAction" },
{ "command": "editor.action.removeCommentLine" }
]
}
}
When clause context
when clause context |
True when |
---|---|
kb-macro.recording |
The macro recording is ongoing |
kb-macro.playing |
The macro playback is ongoing |
kb-macro.active |
The recording mechanism is active. If true, all wrapper keybindings should be enabled. |
Known issues and limitations
- As this extension will take a few days to catch up with each release of VS Code, keyboard shortcuts that are newly introduced or modified in the release may not work properly with this extension during that time.
- This extension is designed to work with VS Code on Windows, Linux, and macOS, but is tested mainly on Windows.
Design details
See DESIGN.md.
Change log
See CHANGELOG.md.