Recording, editing, and using macros

Macros can be very helpful; they are saved with the .sublime-macro extensions as a JSON-formatted file.

Before creating a new macro, we should understand what the use case is. If we find ourselves doing the same actions over and over again, we should use a macro for it. For example, when writing C# code, we always go to the end of the line to insert a semicolon and press Enter. How can we do this faster?

Recording a macro

To record a macro, simply press Ctrl + Q on Windows or Linux and Control + Q on OS X. We will notice that the status bar says Starting to record macro…, as shown in the following screenshot:

Recording a macro

Let's record the macro. Press Ctrl + right arrow + ; + Enter on Windows or Linux and Command + Right Arrow + ; + Return on OS X. Finally, press Ctrl + Q to stop recording.

Recording a macro

Playing a macro

To play the recorded macro, simply press Ctrl + Shift + Q on Windows or Linux and Control + Shift + Q on OS X. Sublime will always play the last recorded macro.

Saving and editing

We can also save the recorded macro and edit it manually. Let's go to Tools | Save Macro… and save it under Packages/User/semicolon.sublime-macro. When opened open it for editing, we should see the following:

[
   { "args": { "to": "eol" }, "command": "move_to" },
   { "args": { "characters": ";" }, "command": "insert" },
   { "args": { "characters": "
" }, "command": "insert" }
]

We can edit the macro to insert); instead of ; just by changing the value of "characters" in the second row to ");".

For the full list of commands, visit http://docs.sublimetext.info/en/sublime-text-3/reference/commands.html.

Binding a saved macro

We recorded, saved, and edited our macro. Now, we want to make it accessible for later use, and one way of doing this is by binding the macro to a shortcut key. We will do this by defining a new key binding to our macro.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset