Notepad++ macros, a basic example

This post is part 2 of 5 in the series Efficient work using Notepad++

Notepad++ macrosMacros have become one of my favorite features in Notepad++. It is possible to record actions and play them back one or more times. You can save a lot of time not having to manually repeat the same process over and over. I always have Notepad++ open, so it is fast and easy to get the result I want in just a few seconds.

Notepad++ macros

This animation will show you how to use macros. You can find detailed descriptions and step-by-step instructions with pictures below.
Notepad++ Macros Animation

In short, click on Start Recording, do whichever actions are needed, click on Stop Recording and Playback the macro one or more times. In this example I want to transform an ordered list of names into a comma-separated list I can use in a T-SQL IN operator. It would not take a lot of time to rewrite this particular list manually, but it takes a fraction of the time using macros, and the more data you work with the more time you save.

Keep in mind that the actions will be played back exactly as they are recorded, so use CTRL to work on words rather than characters. Use CTRL+arrows to move the caret to the previous or next word. Use CTRL+SHIFT+arrows to mark words if you need to copy them. Use CTRL+BACKSPACE or CTRL+DELETE to delete entire words at once instead of using BACKSPACE or DELETE multiple times. Use HOME and END to move the caret to the start or end of a line. It is also a good idea to disable Word Wrap if you’re working on multiple lines.

Notepad++ macros 1
1. Place the caret at the start of line 1. Click Start Recording (use either the button on the toolbar or through the menu MacroStart Recording).

Notepad++ macros 2
2. In this picture the following actions have been done and recorded:
– CTRL+DELETE deleted the number 1
– CTRL+DELETE deleted the dot and spaces
– Single quote added
– END moved the caret to the end of the line
– Single quote and comma added
– DELETE deleted the line break and moved the next line up to the end of the current line
Click Stop Recording (use either the button on the toolbar or through the menu MacroStop Recording).

Notepad++ macros 3
3. Click Playback (use either the button on the toolbar or through the menu MacroPlayback) to replay the recorded macro once, as shown in this picture.

Notepad++ macros 4
4. Click Run a Macro Multiple Times (use either the button on the toolbar or through the menu MacroRun a Macro Multiple Times) to replay the recorded macro as many times as you specify or until the end of file, as shown in this picture.

Notepad++ macros 5
5. Delete the last comma and the list is ready to be pasted into a T-SQL IN operator. In this picture Word Wrap has been enabled again.

Who is Cathrine Wilhelmsen?

Cathrine is a Microsoft Data Platform MVP, BimlHero, speaker, blogger and chronic volunteer. She currently works as a Community Evangelist for PASS and coordinates all SQLSaturdays around the world, but has previously worked as a SQL Server Data Warehouse architect and Business Intelligence developer. She loves sci-fi, chocolate, cat gifs and smilies :)

39 thoughts on “Notepad++ macros, a basic example”

nice clean tutorial, many thanks.

Clean, clear and useful! Perfect, many thanks!

I have created a macro to replace few of the contents in my xml file. I tried the macro once on the active file and it worked fine. As i try the same on a different file, it doesnt work but the notepad icon in the taskbar flickers and the change wont get reflected. But wheni copy the content to a new page and tried, the macro worked out well. I want it to work in the same page without copying the content to a new page.

Kindly help.

Hi Hari. Are the files open in two different instances? By instances I mean in two separate windows with two different icons on the taskbar? It is not possible to record a macro in one instance and play it in another instance. In that case you would have to copy the content to a new tab in the instance where you recorded the macro to be able to play it. However, it is possible to record a macro in one tab and play it in another tab if they are in the same instance.

superb tutorial many thanks .

Hi Catherine–I just discovered Notepad++ and am well-impressed. It opens large datasets fast and lets you get right at the data, etc. What I was trying to do when I found your article was actually use some slightly more complex rules to parse the data–perhaps add a comma every time a ‘(‘ appears in the string for the first time… something along those lines. Do you know if this is possible in Notepad++… also, is there a way of seeing and editing the macro code you have generated, do you know? Thanks for the content.

Yes, you can use more complex rules with Regular Expressions (regex). Find / replace in Notepad++ supports Regular Expressions. I’m afraid I can’t help you with the actual regex, though, I only know the basics myself :)

You can save your recorded macros and view / edit the steps in a file called shortcuts.xml located in the %AppData%\Notepad++\ folder. (In Windows 7 / 8 the full path is most likely C:\Users\\AppData\Roaming\Notepad++\shortcuts.xml)

Hope that helps :)

Thanks for the great tips !

Immensely helpful. Thanks!

I regularly paste large data sets with repeating rows from Excel, in which I need to find the number of unique values. I use the “TextFX Tools/Sort lines case insensitive” command with the “Sort outputs only UNIQUE” option set so that duplicates are weeded out. However, when I attempt to record the macro (select all, click TextFX, click TextFX Tools, click Sort lines case insensitive), running it inserts a bunch of ASCII characters instead of running. Do macros work with menu selections? Or am I stuck with just keyboard actions?

Hi David. If you’re already using Excel I would actually recommend using Excel function instead of Notepad++ macros. The Data → Remove Duplicates function in Excel will tell you how many duplicates have been removed and how many unique values are left.

It is possible to use menu selections in macros. You can for example start recording, select all the text, click Edit → Line Operations → Sort Lines in Ascending Order, stop recording and play the macro (but this won’t remove duplicates). However, I get the same problem with TextFX as you do. It could be because TextFX is a plugin and handles data differently than macros, but I’m afraid I don’t know the reason why it doesn’t work.

Thank you so much!

Hi,
Thanks for the post. I have a question.
Which software did you use to make the gif picture on the top of the page? I tried some software like: Free Video to GIF Converter, KickMyGraphics and Cute Video to GIF Converter. But none of them were up to my expectations(good quality and less file size.)

Hi Arvind, I used a free program called GifCam. You can find it here: http://blog.bahraniapps.com/gifcam/

Really awesome piece of software! Thanks again Cathrine, wish you good luck.

Awesome Tutorial. Thanks a lot

Hi Catherine,

Thanks for the useful tutorial.

Regards,
said

Dear Catherine

Can u suggest me if there any macro codes available to convert notepad text into table format in excel?

Many thanks Catherine!!! So useful and saves me a lot of time.

Thanks Catherine, this kind of technique I was looking for a long time in notepad!!!!

Super presentation! Thanks!

Just stumbled onto your notepad++ tutorial. Macro question regarding searching an open text file…line by line and display in the ‘Find result’ window only if multiple keywords are on that line. Macro works, but I can only get it to output using a single keyword present on the line. i.e.) Basically, If keyword1 and keyword2 and keyword3 present on current line then output in ‘Find result’ window.

Simple and useful!

Thanks Catherine, Nice tutorial.

Do i need to save the macro? for some reason playback is not accessible for me. I am trying do simple find and replace.

Hello Catherine – I need to create a macro for editing a xml file. I will copy a string from Line1, search where all it is present in the file and then Replace the content. I have to perform this operation on from Line 2 – 10. I did the command properly but the macro is not working.

Hi Santhosh, I have not been able to create macros with values that are copied and pasted during recording. For your task, it sounds like you will have to do multiple find and replace values manually.

Hi Catherine-I need a macro to use ‘value’, multiple times.to a series of numbers. I have no idea how to set up the macro in Notepad ++. Could you please help me out for this?
Thanks in advance!

Hi Catherine – I’m using Google Translate to do some quick and dirty translation of Madcap Flare HTML5 documentation output. I followed Jose Sermeno’s helpful blog entry Plugging Topics into Google Translate (http://www.madcapsoftware.com/blog/2012/06/07/topics-into-google-translate/) and can select the appropriate languages to which to translate. I save the translated pages individually and bring them into Notepad++ where I hit them with a macro that does a series of search and replaces to correct the HTML so that it plays nicely within Flare (changing <!DOCTYPE… to <?xml…, removing etc). I end the macro with Ctrl-A Ctrl C and manually paste the corrected output over the appropriate language file in Flare. It’s quick and dirty and CHEAP!!! But I have a lot of individual HTML topics to deal with.

Within the downloaded Google Translate file in Notepad++, I can see the name of the original source file within a comment. However, when I attempt to locate and isolate the name with a “()” and “\2” replace statement, cut it to the clipboard (no path, just the name ie Catherine.html) and paste it within the Save As dialog within a recorded macro, it is not getting recorded. I works fine when I am recording – the Save As dialog appears and I can paste appropriately into it – but it does not play back correctly on the next file.

What is the appropriate way to do a save-as within the macro, if at all?

Thank you so much!!!!

Hi Thomas, I haven’t had any success with copying and pasting in macros, unfortunately :/ You can record the macro, but it gets all scrambled when you play it. Perhaps others who read this post have any suggestions?

Thanks for the quick reply!!!!! Wish me luck!

BTW, I also posted here: https://forums.madcapsoftware.com/viewtopic.php?f=13&t=28153&p=123628#p123628

Me again, maybe you or someone can suggest a better Notepad++ forum?

thank you so much for your simple and lucid illustrations..

Hi you there, Hari! Sure you can help me.
Look! I’m translanting some giants dialogs of games and need to run a macro ou script, whatever, just to save all the misspellings in a separate new file, so I can revise, correct and fix several mistakes just in one time.
But I think I need some support to do that, I’m a newbie in this matter.
I even downloaded a macro file but don’t know how to use it.
Would u pls help me get this thing done?
Ps: I wanna use in the Notepad++
Here’s the ‘macro’: holp it work for I’m intending to.

‘THE MACRO STARTS HERE
Sub ListSpellingErrors()
Dim myDoc As Document
Dim myErrorCount As Integer
Dim e As Integer
Dim myOption As Integer
‘*****************
‘CHOOSE ONE OF THE FOLLOWING
‘THREE OPTIONS:
‘OPTION 1
‘List the errors at the end of
‘your document.
‘OPTION 2
‘List the errors in a new
‘document.
‘OPTION 3
‘List the errors in the document
‘in the next window, such as a new,
‘blank document you’ve already created,
‘or a MultiMacro list.
‘Specify the number of your option
‘in the following line:
myOption = 1
‘*****************
‘Macro specifies the current document:
Set myDoc = ActiveDocument
‘Macro tells Word the document hasn’t
‘already been spell-checked
‘(whether it has or not):
myDoc.SpellingChecked = False
‘But if you want Word to remember
‘spell-checking you’ve already done,
‘put an apostrophe in front
‘of the command above, which turns
‘off the command.
‘Macro counts the number of errors
‘in the document:
myErrorCount = myDoc.SpellingErrors.Count
If myOption = 1 Then
Selection.EndKey Unit:=wdStory
ElseIf myOption = 2 Then
Documents.Add
ElseIf myOption = 3 Then
If Windows.Count >= 2 Then
WordBasic.NextWindow
Else
MsgBox “Only one document open.”
GoTo EndMacro
End If
End If
‘Now type the misspellings into
‘the specified location:
For e = 1 To myErrorCount
Selection.TypeText Text:=myDoc.SpellingErrors(e)
Selection.TypeParagraph
Next e
If myOption = 3 Then
WordBasic.NextWindow
End If
EndMacro:
End Sub
‘THE MACRO ENDS HERE

Any help, thanks earlier and best regards

Sorry very much but It was to be addressed to Catherine!!!

Perfect! This solution just answered my need! Thank you! It was great!

Pingback: Notepad++ Review – A Powerful, Free Code Editor Packed With Features - PallaviGupta

Pingback: Notepad++ Review – A Powerful, Free Code Editor Packed With Features - WordPress Community | powered by Mpress Studios

Share Your Thoughts?