Prerequisite: Using Notepad++ macros is using Notepad++. This is for capable Notepad++ users.

Notepad++ Secrets: Macros

© 2012, Martin Rinehart

Programmer's editor Notepad++ macros are not written in a computer language. They are "written" by turning the macro recorder on, typing and then turning the recorder off. You can then playback to repeat what you typed. You can playback once, or a fixed number of times, or until you reach the end of the file. Last, you can save your keystrokes with a name and an optional keyboard shortcut.

Why a Macro?

Countless times during editing, you need to repeat an operation. Notepad++ macros are so simple that it is usually easier to record a macro the first time and replay it the second time. It is almost never sensible to repeat an operation three or more times. Let's begin with an example.

You typed a long line. It got too long so you wrapped it over two lines. Then you shortened it and now you want to unwrap it from two lines, back to one. Notepad++ will join two lines (Edit / Line Operations / Join Lines, or just Ctrl+J) but you properly indented the wrapped portion so there are spaces or tab characters on its left. Ctrl+J will leave these in the middle of the joined lines. Not what you want.

Design Your Macro

What you want is this: with the cursor in one line, press a keyboard shortcut. The second line will be joined at the end of the current line with leading whitespace turned into a single space character. Nice. How?

Plan Your Macro

How, given a design, can you make that happen from the keyboard?

If you were editing, you'd start somewhere on the first line. By pressing End you go to the end of that first line. Then type a space. Now press the down arrow and Home to get to the leftmost non-blank character in the line below. Hold the shift down and press Home again to get to the left edge with the leading whitespace selected. Press Delete to lose the whitespace. Press Backspace to lose the EOL character(s) from the line above, doing the join.

Record Your Macro

Red dot icon for "macro record" starting. The "start recording" icon is a red dot. It may look like the one on the left.

Red dot icon for "macro record" starting. If you use a Notepad++ just wide enough for 80 character files, it may look more like this red dot. (That's wallpaper, golden fall leaves, to the right of the "click here for more options" button.)

Either way, click the red button.

Black square for "stop recording". When you are recording, the red button is disabled and the black "stop recording" icon is enabled.

You type whatever keystrokes you like and click the black square when you are done. Simple!

This is not a warning!

This may be the first warning in the history of printed warnings about ignoring warnings.

That red circle looks distinctly like a warning. Danger! Clicking here will record your keystrokes!

Nothing could be less dangerous. If you fumble-finger a mess in the middle of your macro, just start over. No one will ever know. (A brief prayer to the unsung genius who gave us Ctrl+Z, and thanks to Don who implemented it so nicely, would be in order.) To discard your macro, simply record another one. There's really no danger here.

Join, Your Way!

Ready to try one? Type something like this into Notepad++:

Join this line
        to the next!

Put your insertion point somewhere in the first line, turn on macro recording and enter these keystrokes:

  1. End
  2. space
  3. down arrow
  4. Home
  5. Shift+Home
  6. Delete
  7. Backspace

Now turn off the macro recorder. Ready to try it out? Start by splitting your test line into two, ready to join again.

If your Notepad++ is wide enough to show all the icons you'll have an easier time than we will (by a wee bit). Use the icons. If you need to click the "show more options" button you'll get this menu:

Extra menu used for first playback.

With your cursor again somewhere in the "Join this line" text, playback your macro. (Use Alt+M to do this from the Macro menu. Use Ctrl+Shift+P for simple macro playback.) Your line is magically joined. Got it?

Saving Your Macro

We use our line-joining macro all the time. Ctrl+Delete seems a good choice. Suit yourself if you've got an idea you prefer. Using either the icon or the extra choices menu (saving is not available on the Macro dropdown from the main menu) give your macro a name and, optionally, a keyboard shortcut:

Saving the line-joining macro.

That warning on the bottom, "This will disable ...", will disappear if you choose an unused keyboard shortcut.

Now split that poor test line into two or three pieces. Use your new keyboard shortcut to join them. Fast and easy! And yes, that's permanently built into your Notepad++, an editor which will just get better if you take the time to tell it what you want.

Another Macro

We've stopped using simple Save in favor of our own Ctrl+S. The difference is that our Ctrl+S does an Edit/Blank Operations/Trim Trailing Space, and then saves. It's a bit old-fashioned, but "a byte saved is a byte earned" as Ben Franklin used to say. Yes, you have to write that Ctrl+S on your own.

If your macro doesn't fire when you press Ctrl+S it's probable that you have another Ctrl+S defined in your Shortcut Mapper. See Shortcuts (also available on the menu above) for a bit more on this.

Here's a small hint to help you along. Notepad++ author Don Ho didn't always underscore his keyboard shortcut letters. If you think one might work, give it a try. Ctrl+S should take a minute or less to "program". Good luck!


Feedback: MartinRinehart at gmail dot com.

# # #