How do I add "input" to my macro, to replace text in Notepad++?

First:

  1. Go to the "Plugins" drop-down menu.
  2. Open the Plugins Manager.
  3. Download PythonScript for Notepad++.
  4. Via the prompt, restart Notepad++.

Next:

  1. Go to Plugins in the drop-down menu.
  2. Click PythonScript -> new script.
  3. Save the script with a .py extension.

Copy and paste this code, then edit it:

#***   IMPORTS   ***
from Npp import *

#*** DEFINITIONS ***
#*** http://docs.python.org/release/1.4/tut/node70.html (.h triple-string variables)***
initial_string="""

  THIS IS A HUGE
  STRING
  WITH COMMENTS
  AND FUNCTIONS
  AND
    NESTING varr
    MISSPELLEDD WORDS
    VARIABLES Varr
  ALL KINDS OF CRAZY STUFF""" 

#***  I am using a case-sensitive replacement;     ***
#***  therefore, I use two replacement operations  ***

#***  First replacement, lowercase ***
print initial_string
user_replacement_string=notepad.prompt(notepad, 'Replace', "")

#*** Second replacement, uppercase ***
editor.replace("varr", user_replacement_string) 
user_replacement_string=notepad.prompt(notepad, 'Replace', "")
editor.replace("Varr", user_replacement_string)

Save.

Now, create a new file ctrl-n and test the script. Go to Plugins->PythonScript->Run Last Script. If it works, you can put it to action in the file you're working on.

For convenience, I made ctrl-shift-e a shortcut key for the Run Previous Script (#yourscriptname.py) command. In your drop-down menu, go to Settings->Shortcut Mapper. Click the Plugin commands tab. Near the end, Run Previous Script. Very handy.

Interestingly, you can use this script to duplicate itself for different patterns. I would really like to know how to make that possible. Please see the edit on my question.

IMPORTANT EDIT

The below code will make it so that your Notepad++ will create its own scripts. Run this script, and save the file in your c:/program files/Notepad++/plugins/config/PythonScript/scripts directory {Windows default}:

#imports
from Npp import *
#variables
pattern_string=notepad.prompt(notepad, 'Pattern String')
number_of_replacements=int(notepad.prompt(notepad, 'Number of Replacements'))
variable_to_replace = []
replacement_title = []
the_header = """#imports
from Npp import *

#definitions
"""
a = "pattern_string = """ + '"' + '""' + pattern_string + '""' + '"'
b = """

print pattern_string
user_replacement_string"""
c = """=notepad.prompt(notepad, 'Replace', "Replaces: """
d = '"' + """)
editor.replace(""" + '"'
e = """", user_replacement_string"""
f = ")"
#function
for i in range (0, number_of_replacements):
    replacement_title.append(str("Replacement Item ") + str(i+1))
    variable_to_replace.append(notepad.prompt(notepad, replacement_title))

print the_header + a
print b + str(i) + c + variable_to_replace[i] + d + variable_to_replace[i] + e + str(i) + f
#thanks Wolfpack08.

It will make it so that you can import strings that you often paste into your file, along with variables that you often have to replace. Write your input pattern with variables (i.e.,

This is my stupid input pattern foo, and I use the common variable bar.

When you run the script, you specify that input pattern in the first pop-up. You then specify the number of variables in the input pattern: foo and bar (e.g., "2" variables). In the resulting pop-up input boxes, you specify the variables in the order you would like to replace them in: ("foo", "bar").

I didn't put a 'default value' function in because I thought it would be annoying to replace what I can only imagine being truly random inside of lexis. I think it would be very useful to have default values for people who do a lot of data entry and need this kind of macro, though. If anyone makes a request for one, I will add it.


Important note: this breaks once for each array item that contains spaces within itself. It also breaks if the second variable contains the first variables (e.g., [(bird), (birds)] and [(var), (variables)] do not work in place of [(foo), (bar)]).

Assume you have a file you want to paste data into. I'll call it the "Left View"/Operative. Create a new file. Right click the new file's tab and click "Move to other view". I'll designate this the "Right View"/"Scratchpad". In the file, put down the pattern and the array of replacement strings, like this.

#Title: Scratchpad
#pattern
I replaced (foo) and (bar) in this pattern.

#array
toe, foot; nose, head; ...; m, n

Now, go to the drop-down menu, Macro. Click on "Record macro" button and execute the following actions, starting in the Scratchpad.

  1. Highlight the pattern in the scratchpad press ctrl+c.
  2. Place your cursor where you want each new string to be pasted in the operative (ideally, click the operative and press ctrl+end or ctrl+home). Press ctrl+v.
  3. In the scratchpad, set your cursor to the beginning of the array. Press ctrl+shift+right. Press ctrl+x.
  4. Click on the operative. Press ctrl+h. Input 'foo' into the find pattern. Highlight the replace pattern and press ctrl+v. Press alt+a.
  5. Click on the beginning of the array (should now start with a comma). Press ctrl+right. Press delete. Press ctrl+right. Press ctrl+x.
  6. In the operative, ctrl+h. Find string foo. Replacement string ctrl+v. Alt+a.
  7. In the scratchpad, place the cursor at the beginning of the array (should begin with a semicolon). Press ctrl+right. Press delete.

Now, Macro->stop recording. You can now replay the macro. It will paste the pattern, eat 2 of your array values up, and replace foo and bar with those array values, every time it loops through. That is, of course, assuming the variables are named well, and the array items do not contain spaces. It's a very nice solution because it helped me to get used to the utility of the macro function.