WebStorm 2021.2 Help

Macros

Macros provide a convenient way to automate repetitive procedures you do frequently while writing code. 宏提供了一种方便的方法来自动化您在编写代码时经常执行的重复过程。You can record, edit and play back macros, assign a shortcut to them, and share them.您可以录制、编辑和播放宏,为宏指定快捷方式,并共享宏。

  • Macros can be used to combine a sequence of editor-related actions within a file.宏可用于在文件中组合一系列与编辑器相关的操作。

  • You cannot record button clicks, navigating to popups, and accessing tool windows, menus, and dialogs.您不能录制按钮单击、导航到弹出窗口以及访问工具窗口、菜单和对话框。

A temporary macro can be used without a name. 可以在没有名称的情况下使用临时宏。For permanent macros, assign unique names.对于永久宏,请指定唯一的名称。

Record a macro录制宏

  1. Open the Edit menu, point to Macros, and click Start Macro Recording.打开“编辑”菜单,指向“”,然后单击“开始宏录制”。

  2. Perform necessary actions that you want to be recorded.执行您希望记录的必要操作。

  3. Press the Stop button or select Edit | Macros | Stop Macro Recording.the Stop button或选择“编辑”|“宏”|“停止宏录制”

  4. In the Enter Macro Name dialog, specify the name for the new macro and click OK.在“输入宏名称”对话框中,指定新宏的名称,然后单击“确定”。

    If the macro is intended for temporary use only, you can leave the name blank.如果宏仅用于临时用途,则可以将名称留空。

Play back a macro回放宏

  • To play back a temporary macro, open the Edit menu, point to Macros, and click Play Back Last Macro.要回放临时宏,请打开“编辑”菜单,指向“”,然后单击“回放上一个宏”。

  • To play back a named macro, open the Edit menu, point to Macros, and click the necessary macro name.要播放命名宏,请打开“编辑”菜单,指向“”,然后单击必要的宏名称。

Edit macros编辑宏

  • Open the Edit menu, point to Macros, and click Edit Macros.打开“编辑”菜单,指向“”,然后单击“编辑宏”。

Bind a keyboard shortcut to a macro将键盘快捷键绑定到宏

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Keymap.在“设置/首选项”对话框Ctrl+Alt+S中,选择“键映射”。

  2. Create a new keymap or select an existing keymap from the list of keymaps.创建新的键映射”或从键映射列表中选择现有的键映射。

  3. Expand the Macros node and select the macro for which you want to create a keyboard shortcut.展开“”节点并选择要为其创建键盘快捷键的宏。

  4. Right-click the macro and choose Add Keyboard Shortcut in the context menu.在宏上单击鼠标右键,然后在关联菜单中选择“添加键盘快捷键”。

  5. In the Enter Keyboard Shortcut dialog, press the key combination to be used as a shortcut.在“输入键盘快捷键”对话框中,按要用作快捷键的组合键。

    The keystrokes are immediately reflected in the First Stroke field. 击键立即反映在"第一次击键”字段中。Optionally, select the Second stroke checkbox and specify the second stroke. (可选)选中“第二次击键”复选框并指定“第二次击键”。As you press the keys, the Preview field displays the keystrokes you pressed, and the Conflicts field displays warnings, if the keystrokes are already in use.当您按键时,“预览”字段将显示您按下的按键,如果按键已在使用中,“冲突”字段将显示警告。

  6. Click OK using the mouse pointer to assign the shortcut to the macro.使用鼠标指针单击“确定”将快捷方式指定给宏。

Example: Create a macro for reformatting a file on save示例:创建宏以在保存时重新格式化文件

This example shows how to create a macro that reformats the current file Ctrl+Alt+L and saves your project when you press Ctrl+S.此示例显示如何创建宏,在按Ctrl+S重新格式化当前文件Ctrl+Alt+L并保存项目。

  • Record a macro with the reformat and save actions.使用“重新格式化”和“保存”操作录制宏

  • Bind the Ctrl+S shortcut to the created macro.Ctrl+S快捷方式绑定到创建的宏。

Record the macro录制宏

  1. Open any file in the editor.在编辑器中打开任何文件。

  2. From the main menu, select Edit | Macros | Start Macro Recording.从主菜单中,选择“编辑”|“宏”|“开始宏录制”

  3. Press Ctrl+Alt+L to reformat code (Code | Reformat Code). Ctrl+Alt+L重新格式化代码(“代码”|“重新格式化代码”)。Then press Ctrl+S to save all changes (File | Save All). 然后按Ctrl+S保存所有更改(“文件”|“全部保存”)。WebStorm will show the performed actions in the status bar.WebStorm将在状态栏中显示执行的操作。

    Macro recording
  4. Stop recording by clicking the Stop button or selecting Edit | Macros | Stop Macro Recording.通过单击the Stop button或选择“编辑”|“宏”|“停止宏录制”来停止录制。

  5. In the Enter Macro Name dialog, specify the name for the new macro and click OK.在“输入宏名称”对话框中,指定新宏的名称,然后单击“确定”。

    Enter Macro Name dialog

Assign a shortcut for the new macro为新宏指定快捷方式

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Keymap.在“设置/首选项”对话框Ctrl+Alt+S中,选择“键映射”。

  2. Expand the Macros node and select the created Reformat and Save macro.展开“”节点,然后选择创建的“重新格式化和保存”宏。

  3. Right-click the macro and choose Add Keyboard Shortcut in the context menu.在宏上单击鼠标右键,然后在关联菜单中选择“添加键盘快捷键”。

    Add Keyboard Shortcut
  4. In the Enter Keyboard Shortcut dialog, press Ctrl+S to be used as the shortcut and click OK.在“输入键盘快捷键”对话框中,按Ctrl+S键作为快捷键,然后单击“确定”。

  5. WebStorm will warn you that the shortcut is assigned to another action. WebStorm将警告您快捷方式已分配给其他操作。Click Remove to remove the Ctrl+S shortcut for the File | Save All action. 单击“删除”以删除“文件”|“保存所有”操作的Ctrl+S快捷方式。You can always reassign it later if necessary.如有必要,您随时可以稍后重新分配它。

  6. Click OK to apply the changes.单击“确定”应用更改。

Now, when you press Ctrl+S, WebStorm will invoke the new macro: reformat the current file and save your project.现在,当您按Ctrl+S时,WebStorm将调用新宏:重新格式化当前文件并保存项目。

Last modified: 15 June 2021