My editor of choice is Notepad++. It has macro capabilities, but as much as I think, I don't see how can I use it for anything.

Do you use macros in your editor that have made your work easier? (for general use I mean)

  • well, none of the answers really gave me a big 'are you stupid? macros are for this' reason so guess it's not that big of a feature after all (in my case), specially considering almost all the examples could have been done by search/replace, but I see how maybe one day I could find it useful for something that's repetitive and hard to do with just search/replace, even though I don't see it right now. – Rodolfo Mar 28 '12 at 17:25

I don't know about Notepad++, but I use macros in Emacs to do pretty much any sort of easily repeated task. These tasks can actually be fairly complicated.

A recent example is this: I had a CSV file from a database and wanted to convert it to JSON using a uniform pattern. It looked something like this (I don't remember exactly what it was):

10, 85, 0.1345, Hostname1, Intel, Xeon
9, 74, 0.21453, Hostname2, Intel, Xeon

I needed to convert it to something like:

[{"a" : 10, "b" : 85, "c" : 0.1345, "hostname" : "Hostname1", "cpu" : "Intel", model: "Xeon"},

Using an sort of script to do this would have take too long, so all I did was convert the first row manually while recording my keystrokes and then replay them on the rest of the file. It worked really well--while the execution time was slow, it only took less than a minute of my time, which is very important for a one-off task!

Pretty much any sort of repeated manipulation like this is a great use case for a keyboard macro. I have definitely saved myself a lot of time thanks to Emacs macros!

  • I see how it could be useful for data manipulation, but I was more thinking of programming-wise, and more general use I guess. It's just that when I see editors being evaluated/compared, macro capabilities are always high in the list of needs, but I just don't see the big issue if you're writing code and thought I might be missing something – Rodolfo Mar 23 '12 at 14:15

Since you asked about the usage related to programming, and two other answers are related to data manipulation, one example which comes in mind is to change the coding style.

For example, you often see compact switch statements like this in C# code:

switch (value)
    case "Hello":           ret =   5;    break;
    case "World":           ret =   4.9;  break;
    case "Something other": ret = -19.99; break;

    // Fifteen other cases.

    default: ret = string.Empty; break;

While being, IMO, more readable, they don't comply to the default StyleCop ruleset. To make them compliant, you have to put one statement per line and, for example, add this if ret is not a variable, but a private field. Without automation, it would be cumbersome to do it by hand.

switch (value)
    case "Hello":
        this.ret = 5;

    case "World":
        this.ret = 4.9;

    case "Something other":
        this.ret = -19.99;

    // Fifteen other cases.

        this.ret = string.Empty;
  • by hands, it's to press ctrl-h and replace ":" with ":\r\n", then replace ";" and "break;" – Abyx Mar 28 '12 at 15:25

I use them for pre-formatting SQL. For example, I have one macro that will surround a line with single quotes and then put a comma at the end. When I paste 50-100 IDs I can apply the macro to the end of the file, then copy-paste the result into a query. This one is also useful for non-SQL purposes.

I have a few similar macros that turn a single line into an INSERT statement for specific tables. It makes creating INSERT scripts much easier.

Most of my macros involve formatting the begining or end of the line in some way, and usually for data, not actual code. The details of your daily tasks will determine if there is anything simple and repetitive enough to be turned into a macro. I've gone for long periods on projects where there simply weren't many tasks that were worthy of editor macros.

Not the answer you're looking for? Browse other questions tagged or ask your own question.