Keyboard macros

Published April 01, 2011
Advertisement
I'm sure you've all watched someone who doesn't know keyboard shortcuts struggling to edit a document. It can be frustrating how long even simple things take, and hard not to start shouting advice: ctrl+shift+right, ctrl+x, end, ctrl+v!

But I occasionally see even master programmers, the kind with indubitable nerd maestro credentials, doing laborious manual editing tasks. Often this is because they don't know about or think to use a keyboard macro.

Key macros are fundamentally simple. You tell the editor to start recording, then press some keys, after which it can play back this sequence of keystrokes as many times as you like. This may not sound useful for anything more complicated than typing the same text many times in a row (for which purpose copy & paste would be simpler), but when combined with the clipboard, it opens up a wonderful new world of editor automation.

With a bit of practice macros can become so instinctive that any time you see a repetitive task coming up you just hit the record button, do the task once, then play back however many times are required, all without breaking your regular code editing stride.

Let's say we have this enum:

enum Animal { Cat, Dog, Camel, Llama, Shrew, } And we want to create a helper function like so:

string GetLocalizedAnimalName(Animal animal) { switch (animal) { case Animal.Cat: return ResourceManager.GetLocalizedString("Animal_Cat"); // likewise for each enum value } }We could copy & paste this first case statement, then go back and edit each enum name and string constant, but it would be faster and less error prone to use a key macro. First we copy the list of values from our enum definition, pasting them into the middle of our function:

string GetLocalizedAnimalName(Animal animal) { switch (animal) { Cat, Dog, Camel, Llama, Shrew, } }Now the magic part. In Visual Studio, place the cursor on the fifth line (the one containing "Cat") and type:

ctrl+shift+R (starts recording)
home
ctrl+shift+right arrow (selects the indentation whitespace)
delete
end
backspace (deletes the trailing comma, so the line now consists of the single string "Cat")
shift+home (selects the line)
ctrl+x
tab tab tab tab
Type this string: case Animal.
ctrl+v
colon
enter
Type this string: return ResourceManager.GetLocalizedString("Animal_
ctrl+v
Type this string: ");
enter
home
down arrow
ctrl+shift+r (stop recording)
ctrl+shift+p (play back macro)
ctrl+shift+p
ctrl+shift+p
ctrl+shift+p



Exercise for the reader: if Visual Studio did not have a built in "encapsulate field" refactoring function, how would you turn this code:

public int Elf; public string Name; protected float LivesOfTheCat;into a series of properties of the form:

public int Elf { get { return elf; } set { elf = value; } } int elf;?

aggbug.aspx?PostID=10148973

Source
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement