Click here to Skip to main content
Click here to Skip to main content

VS IDE Editor Tip: Quick Macros

, 15 Oct 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
VS Editor Tip: How to use Quick Macros


How often do you edit a series of code fragments that are redundant? Where you will repeat the same steps over and over, adding the same formatting around a simple list of items, or making the same changes to each function in your file? Visual Studio's IDE contains a feature that will allow you to easily convert this repetitive task into a simple quick process. The feature is called Quick Macros.

This is a feature that I was not aware of how to use until I saw another keyboard savvy developer use it. I am writing this article to make sure that everyone on CodeProject is aware of this feature as well.


The Quick Macro is found under the Tools menu. In order to use the Quick Macro you will first need to record a macro (CTRL + SHIFT + R), then play it back (CTRL + SHIFT + P). The keyboard commands listed are the defaults for the Visual Studio IDE.

Record Quick Macro

The most important thing is to properly record the macro in order to make it work repeatedly without any intervention on your part. This will allow you to play back the macro as fast as you can type (CTRL + SHIFT + P).

In order to record the macro you must:

  1. Decide the common set of operations that need to be applied to each item.
  2. Decide the best plan of action to accomplish your task.
  3. Start recording by selecting Tools | Record Quick Macro or (CTRL + SHIFT + R).
  4. Enter all of the editor operations that you want to repeat in your quick macro.
  5. Stop recording by selecting Tools | Record Quick Macro or (CTRL + SHIFT + R).

What sort of things can you do with the quick macro? Basically anything that you can do with the keyboard. It is very helpful if you know all of the commands to help you navigate in your editor with the keyboard. I have found that I am able to use the most basic keyboard commands in order to accomplish some very creative macros:

  • Arrow Keys.
  • Home (odd number of times): Moves the cursor to the beginning of the indented text on the current line.
  • Home (even number of times): Moves the cursor to the beginning of the line.
  • SHIFT + Arrows: Select characters or lines.
  • SHIFT + HOME: Select from current position to the beginning of the line.
  • SHIFT + END: Select from current position to the end of the line.
  • CTRL + X or SHIFT + DELETE: Cut selected text.
  • CTRL + C or CTRL + INSERT: Copy selected text.
  • CTRL + V or SHIFT + INSERT: Paste selected text.
  • CTRL + SHIFT + Arrow: Select complete words on the current line.
  • CTRL + F: Bring up the find dialog and start a search.
  • F3: Search for the next instance of the previous search.

I am sure there are others that I use, but these are the most common, and I am able to accomplish most tasks with these keyboard commands.

Play Quick Macro

Playing back the macro is the easiest part. After all of the hard work that has been used to create the macro, you can sit back and type (CTRL + SHIFT + P) as many times as you need until your repetitive task is complete.


Here are a few examples to show where I have found this feature is useful, and step by step explanations on how to use this feature.

Example 1

Say you have created an enumeration with a different number of elements. Then you want to create a switch statement that contains a handler for each of these enumerations. For only a few items this would be a simple task, however if you have 10 items or more, this just becomes tedious.

enum QuickMacro 
    ITEM1 = 1, 
        // An unspecified number of items ...

Given this example, I would copy the ITEM list to be inside of the switch statement before you start recording the macro.

    ITEM1 = 1, 
        // An unspecified number of items ...

Place your cursor at the beginning of the first ITEM and start recording (CTRL + SHIFT + R).

  1. Type "case ".
  2. Hold (CTRL + SHIFT) and move the cursor to the right, this will select the entire variable ITEM1.
  3. Move the cursor to the right with the right arrow key and now you will be at the end of the variable ITEM1.
  4. Type ":" + return + "{" + return + tab + "break;" + return + backspace + "}".
  5. Type (SHIFT + END) + Delete, this will erase the rest of the text at the end of this line.
  6. Move the cursor down one line, and hit HOME to move the cursor to the beginning of the text for the next variable.

Stop recording with (CTRL + SHIFT + R). At this point your text will look like this:

    case ITEM1 :
        // An unspecified number of items ...

Play back the Quick Macro with (CTRL + SHIFT + P) until all of the items have case handlers. This is the final result.

    case ITEM1 :
    case ITEM2:
    case ITEM3:
        // An unspecified number of items ...
    case ITEMN:

Example 2

The second example requires copying, pasting and searching for text. This is a set of function pointer variable declarations that will be created from the original type definitions for the function pointers.

typedef HDC (*pGetDC)(HWND hWnd);
typedef HDC (*pGetDCEx)(HWND hWnd);
typedef HDC (*pGetWindowDC)(HWND hWnd);
typedef HWND (*CreateWindow)(LPCTSTR lpClassName, LPCTSTR lpWindowName, ...);
// Imagine all of the functions from User32.dll :-o
  1. Copy the typedef listing to where the member variables will be pasted.
  2. Move the cursor to the first line of the typedefs.
  3. If the text was preformatted we could simply move the cursor over to the beginning of the function pointer. However the text is not preformatted so we will need to search for the beginning of the pointer name. Notice how there is a * before each name. Use the * to find the beginning of the pointer name. Type (CTRL + F) + * + return.
  4. (SHIFT + HOME + HOME) + Delete + Delete. This will delete all of the text before the pointer name including the *. The text will now look like this:
  5. pGetDC)(HWND hWnd);
    typedef HDC (*pGetDCEx)(HWND hWnd);
    typedef HDC (*pGetWindowDC)(HWND hWnd);
    typedef HWND (*CreateWindow)(LPCTSTR lpClassName, LPCTSTR lpWindowName, ...);
  6. The next goal is to copy the name of the function pointer. We could select each of the 6 characters for the current pointer, however, not all of the pointers names are 6 characters long. Therefore we will need to do another search. Type (CTRL + F) + ")", This will find the end of the pointer name.
  7. Type (SHIFT + HOME) + (CTRL + C) This will copy the name of the variable to the clipboard.
  8. Move the cursor to the right once. This will move the cursor to the end of the variable name. Then Type (SHIFT + END) + Delete. This will delete all of the text after the pointer name.
  9. Type tab + tab + tab + "m_". This will add formatting and the beginning of the variable name.
  10. Paste the name of the variable with (CTRL + V) and type ";".
  11. Move the cursor to the next line and the beginning the text by hitting the HOME key.

End the recording session with (CTRL + SHIFT + R). Now play back the macro as many times that is needed with (CTRL + SHIFT + P). This is what the text will look like after playing back the macro (after formatting).

pGetDC        m_pGetDC;
pGetDCEx      m_pGetDCEx;
pGetWindowDC  m_pGetWindowDC;
CreateWindow  m_CreateWindow;


The only limitations that I have found so far is when I would like to increment an index or some other number on each new line. What I have done in this situation is used a default number like one, and then incremented by hand afterwards.


This is a nice feature that has one specific use, making repetitive editing tasks simpler. I think this feature is very convenient when you find the right place to use it. In some cases it may be faster and easier to simply type out the repetitive changes. However, this is a feature that I have gotten better at using over the years. So the more you practice with it hopefully the better you will become with it as well!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul M Watt
Architect L3 Communications
United States United States
I have been developing software for almost two decades. The majority of my expertise as well as my strongest language is C++ with the networking software as my domain of focus. I have had the opportunity to develop:
* Desktop applications (Data Layer, Business Layer, Presentation Layer)
* Application virtualization
* Web clients
* Mobile device management software
* Network Device drivers
* Embedded system software for
- IP routers
- ATM switches
- Microwave frequency radio/modems
* Distributed processing w/ parallel algorithms.
Over the years I have learned to value maintainable design solutions first. This has allowed me to adapt my projects to meet the challenges that inevitably appear during development, including:
* My own misjudgments
* Incomplete requirements
* Feature creep
* Poor decisions for which I have no control
I am a Mentor and frequent contributor to with tutorial articles that teach others about the inner workings of the Windows APIs.
I am the creator of an open source project on GitHub called Network Alchemy[^], which is a set of Network APIs that are focused on helping developers write robust network communication software.
I maintain my own repository and blog at[^], because code maintenance does not have to be a living hell.
Then for fun I will tinker with my ray-tracer when ever I upgrade my hardware to see what it is capable of doing.
Follow on   Twitter   LinkedIn

Comments and Discussions

Generalerror in open project or classwizard Pinmembermohsen nourian4-Mar-07 7:05 
Generalnext step is save macros Pinmemberfeline_dracoform17-Sep-04 0:56 
i didn't know that you could edit the quick macro's. that is definitely worth knowing Smile | :)
recently i was doing a series of three linked edit's, several hundred times in a long file OMG | :OMG:
i simply recorded each one as a quick macro, and then selected:
tools -> macros -> save temporary macro
for each one.
putting 1, 2 or 3 at the front of the macro names made sure they were listed in the correct order, since they are shown in alphabetical order in the macro explorer window.
of course, as i used the macro's i found i had to make minor changes, mainly add an extra "press down arrow key" at the end of the macro's to move through the lines of code correctly.
if you don't know the VB macro language, and i don't, then the solution is this:
record a new quick macro that is the action you want to add to the saved macro, then save it out as "temp", or some such helpful name Smile | :) now edit it - you can edit the macro's via the right click menu in the macro explorer.
the new temp macro contains the VB code you need to add to the existing macro, so simply copy and paste it into the macro you want to edit Smile | :) so much easier than trying to second guess what the command is going to be called.
macros are definitely very helpful, but i find it takes a fair bit of practise to really get the hang of them. you need to think in quite general terms when setting up your movement commands, so the macro will work on more than one line of code. definitely time well spent though, since it can save you so much work Smile | :)
a good article, making this rarely used feature nice and clear Big Grin | :-D
Generalnumber increment solution ;) Pinmemberfeline_dracoform17-Sep-04 0:45 
GeneralHelp in Macro Command PinsussAnonymous9-Sep-04 17:40 
GeneralNice job! PinmemberShog920-Oct-02 8:03 
QuestionIs It available even we close the session and open the next time?? PinmemberR e n j i 19-Oct-02 8:27 
AnswerRe: Is It available even we close the session and open the next time?? PinmemberPaul Watt (kilowatt)19-Oct-02 15:02 
GeneralSome other useful keyboard commands PinmemberAnthony_Yio18-Oct-02 0:08 
GeneralRe: Some other useful keyboard commands PinmemberPaul Watt (kilowatt)18-Oct-02 9:34 
GeneralRe: Some other useful keyboard commands PinmemberBob Kaye18-Oct-02 16:51 
GeneralRe: Some other useful keyboard commands PinmemberAnthony_Yio20-Oct-02 17:06 
GeneralGetting even more use from quick macros PinmemberPatrick Burrell17-Oct-02 4:41 
GeneralRe: Getting even more use from quick macros PinmemberPaul Watt (kilowatt)17-Oct-02 5:39 
GeneralUsing regular expressions in search/replace PinmemberSteve S16-Oct-02 0:36 
GeneralRe: Using regular expressions in search/replace PinmemberPaul Watt (kilowatt)16-Oct-02 5:38 
GeneralRe: I love quick macros.... PinmemberPaul Watt (kilowatt)16-Oct-02 8:15 
GeneralNice one Kilo... PineditorNishant S15-Oct-02 20:40 
GeneralRe: Nice one Kilo... PinmemberPaul Watt (kilowatt)16-Oct-02 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150302.1 | Last Updated 16 Oct 2002
Article Copyright 2002 by Paul M Watt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid