In Notepad++, how can you create a macro that will save the current file with a different name?

When I use the macro recorder in Notepad++, it doesn't seem to record any Save operation.

Ideally, I would like to have Notepad++ save the current file in the same location as it was opened, but delete the last 9 characters in the filename. For example, if the current file is:

c:\foo\bar123456789.c

I would like to have a Notepad++ macro save it as:

c:\foo\bar.c

(overwriting any file with that name)

That's the ideal goal, but even if I can just get a Notepad++ macro to come close to that, it will be an improvement over manually performing that operation.

1 Answer

You can run a python script within the PythonScript plugin.

If it is not yet installed, follow this guide

Create a script (Plugins >> PythonScript >> New Script)

Copy this code and save the file (for example replace.py):

import re # Define the new filename newFilename = notepad.getCurrentFilename() # remove up to 9 characters before extension newFilename = re.sub(r'.{1,9}(?=\.\w+$)', '', newFilename) # Save current file with new name notepad.saveAs(newFilename); 
  • Open the file you want to change
  • Run the script (Plugins >> PythonScript >> Scripts >> replace)
  • Done

Regex explanation:

.{1,9} # 1 upto 9 any characters (?= # positive lookahead, make sure we have after (without capturing) \. # a dot \w+ # 1 or more word characters $ # end of string ) # end lookahead 

If there is always 9 char to remove, use .{9} instead of .{1,9}

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy