We write macros for TODO and FIXME in Sublime Text, or how a little code can save a lot of time
The Great Master fought with Chaos. And the harder he fought, the more thoughts came to him. When sensible thoughts came, he wrote them down, preceded by the magic word TODO. He also wrote down thoughts about stupid, but for such thoughts he had another magic word - FIXME. And I must say that from the Beginning of Times to defeat Chaos there were no stronger spells than these two.
I suggest speculating on how to make your life easier and get a simple tool with hot keys to insert comments into the TODO and FIXME code in the popular Sublime Text editor .
What do we want to get
We need to immediately determine what we want to get after calling our insert command. We assume that the cursor is on some line of code for which we want to insert a TODO comment . You can add a comment at the end of our line. But this solution has two significant drawbacks. Firstly, the line may be too long, and by inserting a comment, we can easily violate the agreement on the maximum line length. Even if there is no such agreement, working with such a string will be inconvenient. Secondly, if we want to use the automatic generator of the TODO file in the process of building the application , which contains a list of all the TODO and FIXME all that he met in the projectcomments with links to files and line numbers in them, it may turn out that he requires these comments to be at the beginning of lines, as does the todo-webpack-plugin that I use.
So, the comment should appear above the line to which we want to add TODO . That is, after launch, our team should:
- move the cursor to the beginning of the line,
- insert a blank line
- move the cursor to the beginning of this new line,
- insert characters that specify a comment for this file type
// для JS, SCSS и др.
# для python и др.
- Etc. - somewhere and ';' is used.
- insert the TODO: instruction and a space after it.
After completing these steps, the cursor will be exactly where it is appropriate to enter a description.
Writing a macro
To create our tool it is logical to use the macro creation tools available in the editor. We simply turn on the macro recording, perform some actions in the editor, stop the recording, save it to a file and assign hot keys, according to which our recorded sequence of actions - our macro - will be restarted when we need it.
Let's open some file with the code and put the cursor on the line for which we want (or pretend we want, but actually just write a macro) add TODO . Turn the record: the Tools> the Record the Macro or a combination of keys to the Ctrl + the Alt + the Q . Dale act on the points discussed above.
- Home - move the cursor to the beginning of the line.
- Enter - insert an empty string.
- Up arrow - place the cursor at the beginning of this empty line.
- Ctrl + / - insert a comment, using the command available in the editor, which correctly puts the comment in accordance with the contents of the code.
- Enter the text TODO: and a space after it.
We stop recording with the same keyboard shortcut Ctrl + Alt + Q or Tools> Stop Recording Macro . Further on Tools> Save Macro ... we open a dialogue of record of a macro in a file. Set the file name InsertTodo , leaving the proposed extension .sublime-macro . Assign a hotkey. To do this, open Preferences> Key Bindings . The Key Bindings - Default tab contains a list of all the default keyboard shortcuts, and the commands associated with them. We need it to find an unoccupied keyboard shortcut that would suit us. It would be convenient to use Ctrl + Shift + T , but this hotkey in Linux is intercepted by the system and opens a terminal. CombinationCtrl + Shift + D is also busy - duplicates the line. I settled on Ctrl + Shift + = - it turned out to be free.
The “Key Bindings - User” tab is used to define custom keyboard shortcuts. There we will write our new combination:
[
{
"keys": ["Ctrl+shift+="],
"command": "run_macro_file",
"args": {"file": "Packages/User/InsertTodo.sublime-macro"}
}
]
This is if the file was empty. If something other than brackets is already written in it, then we put a comma there after the last curly bracket, and from the next line we add our object:
[
// Что-то...
{
// Последняя запись
}, // Здесь нужна запятая
{
"keys": ["Ctrl+shift+="],
"command": "run_macro_file",
"args": {"file": "Packages/User/InsertTodo.sublime-macro"}
}
]
I think that the meaning of this entry is clear enough. The key key holds the key combination. command defines the command that will be launched by this combination. In our case, it is run_macro_file , which can run macros. args stores the arguments passed to the command. In our case, the path to the file of our macro is passed.
We save, close the window with the settings files and experience what happened. We make sure that everything works correctly in Python , in JS and in HTML .
What's inside
Now you can take a look and look at the InsertTodo.sublime-macro file itself :
[
{
"args":
{
"extend": false,
"to": "bol"
},
"command": "move_to"
},
{
"args":
{
"characters": "\n"
},
"command": "insert"
},
{
"args":
{
"by": "lines",
"forward": false
},
"command": "move"
},
{
"args":
{
"block": false
},
"command": "toggle_comment"
},
{
"args":
{
"characters": "T"
},
"command": "insert"
},
{
"args":
{
"characters": "O"
},
"command": "insert"
},
{
"args":
{
"characters": "D"
},
"command": "insert"
},
{
"args":
{
"characters": "O"
},
"command": "insert"
},
{
"args":
{
"characters": ":"
},
"command": "insert"
},
{
"args":
{
"characters": " "
},
"command": "insert"
}
]
Ah - not good! Each character of the string "TODO:" is displayed by its command. Fix:
[
{
"args":
{
"extend": false,
"to": "bol"
},
"command": "move_to"
},
{
"args":
{
"characters": "\n"
},
"command": "insert"
},
{
"args":
{
"by": "lines",
"forward": false
},
"command": "move"
},
{
"args":
{
"block": false
},
"command": "toggle_comment"
},
{
"args":
{
"characters": "TODO: "
},
"command": "insert"
}
]
We save and verify that they did not break anything. Everything works.
And now FIXME
Let's assemble a similar tool for inserting FIXME. To do this, create the InsertFixme.sublime-macro file in the same directory and copy the contents from InsertTodo.sublime-macro into it , redirecting “TODO:” to “FIXME:” in the last command :
...
{
"args":
{
"characters": "FIXME: "
},
"command": "insert"
}
]
Bind hotkeys. The combination of Ctrl + Shift + - I turned out to be free.
[
...
{
"keys": ["Ctrl+shift+="],
"command": "run_macro_file",
"args": {"file": "Packages/User/InsertTodo.sublime-macro"}
},
{
"keys": ["Ctrl+shift+-"],
"command": "run_macro_file",
"args": {"file": "Packages/User/InsertFixme.sublime-macro"}
}
]
All. Now we are testing and rejoicing that we have such simple tools at hand that, despite their simplicity, will help us save a lot of our precious time!
References:
- Sublime Text Unofficial Documentation - Macros
- Macros in Sublime Text 3
- Ninja Cheat Sheet Sublime Text 3