Visual Studio Code does not have a built-in macro recorder, but you can achieve macro-like automation using extensions and keybinding triggers to record and replay sequences of commands. This lets you automate repetitive editing tasks like formatting code, inserting boilerplate, or navigating files—saving minutes per day across hundreds of edits. You’ll need the official VS Code extension marketplace and an internet connection to install the required extension. No external tools or complex setups are needed.

What You'll Need

  • Visual Studio Code (version 1.70 or later)
  • Internet connection to install extensions
  • VS Code Extensions Marketplace access (enabled by default)
  • Basic familiarity with VS Code’s command palette and keybindings

Step-by-Step

  1. Open Visual Studio Code and click the Extensions icon in the left sidebar (it looks like a square with four smaller squares) or press Ctrl+Shift+X (Cmd+Shift+X on macOS).
  2. In the Extensions search bar, type "macros" and press Enter.
  3. Look for the extension titled "Macro Recorder" by "alefragnani" and click the green "Install" button.
  4. After installation, VS Code may prompt you to reload. Click "Reload Required" to restart the editor and activate the extension.
  5. Once reloaded, open any file you want to record a macro for—this ensures the context is active during recording.
  6. Press Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette.
  7. Type "Macro Recorder: Start Recording Macro" and select it from the list. You’ll see a status bar message at the bottom saying "Recording macro...".
  8. Perform the sequence of actions you want to record: for example, select text, press Ctrl+Shift+L to split selection into lines, type a keyword, then press Enter twice. Avoid using mouse clicks—stick to keyboard commands for reliability.
  9. When done, press Ctrl+Shift+P again and type "Macro Recorder: Stop Recording Macro". A notification will confirm the macro was saved.
  10. Press Ctrl+Shift+P again and type "Macro Recorder: Save Macro". Enter a name for your macro, like "FormatAndComment", then press Enter.
  11. To replay the macro, press Ctrl+Shift+P and type "Macro Recorder: Run Macro", then select your saved macro name from the list.
  12. For faster access, bind your macro to a key combination: go to File > Preferences > Keyboard Shortcuts (or Code > Preferences > Keyboard Shortcuts on macOS), type the macro name in the search box, click the pencil icon next to it, and press your desired key combo (e.g., Ctrl+Alt+M).
  13. Test the keybinding in a new file to ensure it works as expected. If it fails, check that all commands in the macro are valid in the current file type.

Common Mistakes

  • Using mouse actions during recording—these are often not captured reliably. Always use keyboard commands.
  • Recording commands that depend on specific cursor positions or selections without ensuring the context is consistent.
  • Trying to record macros in files that don’t support the commands used (e.g., recording a TypeScript-specific refactor in a plain text file).
  • Not saving the macro after recording; the temporary macro is lost when you stop recording if you don’t explicitly save it.
  • Binding a macro to a key combination already used by another command, causing conflicts.
  • Expecting the macro to work across different file types without testing each one.

Tips

  • Keep macros short—aim for 5–10 commands max. Long macros are harder to debug and more likely to break.
  • Use the Command Palette to verify each command you plan to record works standalone before recording.
  • Save macros with descriptive names like "InsertHeaderComment" or "WrapInTryCatch" so you can find them later.
  • Test macros on sample files before using them on real code to avoid accidental changes.
  • Store frequently used macros in a shared settings file by exporting your VS Code settings (File > Preferences > Settings > Settings Sync) so you can reuse them on other machines.
  • Use the extension’s "List Macros" command to see all saved macros and their keybindings in one place.

Troubleshooting

  • If the macro doesn’t run, check the Command Palette for "Macro Recorder: List Macros" to see if your macro appears. If not, you likely forgot to save it after recording.
  • If a keybinding isn’t working, open Keyboard Shortcuts and search for the macro name. If it’s listed but unresponsive, another extension may be overriding it—try changing the key combination.
  • If the macro behaves differently than recorded, restart VS Code. Sometimes the extension state gets corrupted.
  • If you see "Command not found" errors when running a macro, one of the recorded commands is not available in the current language mode (e.g., a Python formatter called in a JSON file). Edit the macro by going to File > Preferences > Extensions > Macro Recorder > Settings and click "Edit in settings.json" to remove or replace the problematic command.
  • If the extension stops recording entirely, disable and re-enable it from the Extensions panel, then restart VS Code.
  • Ensure you’re not running VS Code in a restricted environment (like corporate machines with blocked extensions) where the marketplace is inaccessible.

Frequently Asked Questions

Can I record mouse clicks with the Macro Recorder extension?

No. The extension only records keyboard commands and editor actions triggered by commands. Mouse movements and clicks are not captured reliably and are not supported.

Do macros work across different file types?

Not always. Macros depend on the context—commands like "Format Document" may not exist in non-code files. Always test your macro in each file type you plan to use it in.

Can I share my macros with my team?

Yes. Macros are stored in your VS Code settings.json file. You can export your settings via Settings Sync or manually copy the "macros" section from settings.json to another user’s file.

Is there a limit to how many macros I can save?

There is no hard limit, but performance may degrade with hundreds of macros. Keep only those you actively use.

Can I edit a macro after saving it?

Yes. Open the Command Palette, type "Macro Recorder: Edit Macro", select your macro, and modify the list of commands in the JSON that opens.

Will macros work in remote or SSH sessions?

Yes, if you have the Macro Recorder extension installed on the remote machine or dev container. The extension runs locally on the VS Code server side, so it works in Remote-SSH, Containers, and WSL environments as long as it’s installed there.