Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)

share|improve this question

closed as not constructive by tdammers, Bernard, Morons, Joel Etherton, ChrisF Mar 28 '12 at 20:32

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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
add comment

3 Answers

up vote 3 down vote accepted

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!

share|improve this answer
    
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
add comment

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;
        break;

    case "World":
        this.ret = 4.9;
        break;

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

    // Fifteen other cases.

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

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.

share|improve this answer
add comment

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