MACRO: Insert fixed length HTML comment markers

I write all of our apps' help, and those files are pure HTML (with a smattering of Javascript). I like to use comments to set out sections of the code to make it easier to go back and find what I want while editing. After much testing, I found that this small square block - - works perfectly (at least in Menlo and some other fixed-width fonts) to set off my comments from the surrounding code, as it forms a solid line:

<!-- ■■■■■ SOME COMMENT HERE ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ -->

Years ago, I had written a macro to prompt for the comment text, then fill it out to a defined length (90 characters). While this worked, it couldn't handle making all comment headers the same length (i.e. fill to 90 characters).

If my comments started in column zero, it worked fine. But anything indented was too long—I want all my comment blocks to end in the same column. I've now updated the macro to do just that, so it no longer matters where I insert the comment. Here's how it looks in action:

An animated GIF resides within

commenter3

For the version I've shared, I've set everything up with variables: You can set the width of the comment (default 90), the characters before the text (<!-- ■■■■■), the fill character (), and the end of comment characters (-->).

The macro checks to make sure you only set one fill character, and it won't insert a comment if it won't fit in the space available. If you don't like upper case comments, you can remove the action (just after the input block) that uppercases the text.

Download Macro(s): Insert comment block.kmmacros (20 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
    • This macro has only been tested with Visual Studio Code and BBEdit.
System information
  • macOS 14.5
  • Keyboard Maestro v11.0.3

The macro definitely works with BBEdit and Visual Studio Code, but I don't know about other editors. It relies on a keyboard text selection technique—Shift-Command-Left Arrow—that works in both those editors to select all the text from the current location to the start of the line. If another editor supports that same shortcut, it should also work there.

Note, too, that this only works if you use your editor the proper way: You have it configured to use spaces for tabs. If you use actual tab characters instead, well, you're doing it wrong :slight_smile: and the macro won't do its calculations correctly.

There's a lot more that could be done with this (supporting comments in other languages, inserting a comment block, etc., but I leave all that as an exercise to anyone who wants to take it on. In its current form, it does everything I need.

-rob.

1 Like