Thursday 18 February 2016

Recording and Running (Playing) a Macro in Notepad++


So, Let’s try something interesting in Notepad++

There is an interesting feature in Notepad++, called Macros. Now I assume that we know the meaning of a Macro or what a Macro does.

In simple words, Macro is a small program which executes a task repeatedly. Confused??

OK, Let’s make it a bit easier.

A Macro can also be a small program, or script, that automates common tasks. These scripts are usually run within programs and can often be created by the user. For example, a user might record a macro for Microsoft Word that inserts his entire address when he presses a custom key combination.

So for using a macro, it has to be recorded or written in form of a program.

For Notepad++, we will record the Macro. This means we will record the Keystrokes. Recording of these keystrokes will be called a Macro. Then we will use that macro and it will execute the same keystrokes again.

I hope it makes sense.

Anyways, Let’s take an example:

Here is a screenshot of the file, on which I will work


In this text file, I will like to remove everything except the First English Text between two slashes. For example for 1st line, I will keep only “CCIS Hopewell”, for 2nd line, I will keep only “Hopewell JCT” and so on.
Finally the file will look like this:


How cumbersome is this, if this file has 10000 lines.
So we will remove the extra text only from first line, record our operations and replay those operations on every line. Sound good…
MAKE SURE, WE WILL RECORD ONLY KEYSTROKES, NOT THE MOUSE MOVEMENTS.
Now some important Keys / Key Combinations:
Home: Moves the cursor to start of the line.
Shift + --> Select one character towards right
Shift + Ctrl + --> Select one word
End: Moves the cursor to the end of the line
Ctrl + --> Moves the cursor from the end of line to the next line.

Lets go back to the file


So, what we have to do on the first line to retain “CCIS Hopewell” and delete everything.

  1. Go to the Start of the line (put cursor anywhere on the line and Home)
  2. Select up to / (Shift + Ctrl + -->). This length up to first  / is same for every line, so same operation will be repeated for every line.
  3. Delete
  4. Now look for the next / by Ctrl+F
  5. From here select up to the end of the line (Shift+End)
  6. Delete
  7. Press Ctrl + -->  to bring the cursor to next line.

If this operation can be recorded and repeated for every line, same thing will happen to each line.

Let’s do that.

Open the File in Notepad. Do step 1. Cursor will be on the start of the line.


Go to Macro -> Start Recording

Do Step 2.


Do Step 3.

 


 

Do Step 4.


Do Step 5.


 Do Step 6.

 

Do Step 7 (Cursor will be on the next line, now)


Go to Macro -> Stop Recording.

So we have done the operation on first line and recorded this operation. If this can be automatically repeated for all the line, it will be easy and done in seconds.

Let’s try this recorded macro for next two lines.

Go to Macro - > Run a Macro Multiple times.


 Run 2 times as shown below:


Press Run


Wow !

So it is tested. Let’s run it till the end of the file.

Go to Macro - > Run a Macro Multiple times.

Select Run until the end of the file


Press Run. Here is the result