![]() ![]() Emacs will use that format whenever you insert the macro counter. anything acceptable to format for displaying numbers. This will prompt you for a formatting string, i.e. Then I pressed C-x C-k C-f ( kmacro-set-format). That is the default value, but personally I think it is a good habit to always begin by setting the initial value. These are the keys I pressed to perform these changes.įirst I used C-x C-k C-c to reset the counter to zero. This takes advantage of Emacs’ ability to let us only define a formatting string for the counter and to increment the counter by a value different than the default of one. In this example we not only change the format to hexadecimal, but we also change the values of the numbers themselves. ![]() We want to turn it into this: #define FOO 0x0000 #define BAR 0x0002 #define BAZ 0x0004 Let’s consider the following code: #define FOO 0 #define BAR 1 #define BAZ 2 It would not make much difference above, but it is something that I personally find useful when programming. We could tell Emacs to do this for us by setting the formatting string for the counter. In the previous example we manually typed the period and space after each insertion of the macro counter. Now we can keep pressing (or C-x e to) execute the macro for each name. Or instead of you can use C-x ) to end the macro note the mnemonic relation to C-x (. M-f to insert a period and space, move the point over the name, insert a newline, and then end the macro definition. So the next time we execute the macro it will insert a two, then three, and so on.įinally we press. Both will insert the counter value, our initial value of one, and then increment the counter by one. We can pressing again to do this, or C-x C-k C-i ( kmacro-insert-counter). The first thing we want to do is insert the current value of the counter to enumerate the current item. So now we begin defining the macro by pressing. If we assigned the counter value as part of the macro then it would reset each time, which is rarely what we want. Note well that we set the initial counter value before we define the macro. So we begin by pressing C-x C-k C-c, which will cause Emacs to prompt us for the value. ![]() By default the counter begins at zero this can be useful in programming, but unless you’re Edsger Dijkstra you probably want to begin many lists with the number one. As the name implies, this command lets us set the initial value for the counter. For example, C-x C-k C-c is bound to kmacro-set-counter. A mnemonic to help you remember this is that the names for the functions bound to those keys begin with the letter ‘k’. ‘point’ in Emacs terminology) is at the beginning of the string, these are the keys we press:Ĭ-x C-k C-c: Many macro-related key sequences in Emacs begin with C-x C-k. But by using counters we can create a macro that will automatically enumerate the list for us. It would not take that much time to do manually. We want to take this string of names Leonardo Raphael Donatello Michelangelo Our example in this article will be simple. Personally I find them useful anytime I need to enumerate a list, such as when writing documents like these blog posts, and when I am programming. Today I want to discuss counters, a useful feature of keyboard macros in Emacs.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |