<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Any way to create a shortcut to add prefix and suffix to a word?]]></title><description><![CDATA[<p dir="auto">Say there is a word <em><strong>testword</strong></em> and i want to select it and convert it into <em><strong>ABCtestwordXyZ</strong></em>… is there a way i can define a macro or some other shortcut to do so?<br />
I have a use case where i need to do this quite often in a certain type of document so a shortcut would come quite handy.</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/19459/any-way-to-create-a-shortcut-to-add-prefix-and-suffix-to-a-word</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 02:47:22 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/19459.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 25 May 2020 16:27:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 15:29:52 GMT]]></title><description><![CDATA[<p dir="auto">Okay, here’s another (and probably last) try.<br />
Basically, if there’s no active selection and the caret isn’t touching a word when the script is run, it means you want to modify the current prefix/suffix setup.<br />
Otherwise, the current prefix/suffix is applied to the current selection/word.</p>
<pre><code># -*- coding: utf-8 -*-

from Npp import editor

class T19459a(object):

    def __init__(self):

        self._prefix = ''
        self._suffix = ''

    def update_prefix_suffix_info(self):

        separator = '-'
        while True:
            user_input = notepad.prompt(
                'Enter PREFIX{s}SUFFIX :\r\n(PREFIX or SUFFIX (not both) maybe be empty, but must still use {s})'.format(s=separator),
                '', '')
            if user_input == None: return  # Cancel
            parms_list = user_input.strip().split(separator)
            if len(parms_list) == 2:
                (prefix, suffix) = parms_list
                if len(prefix) + len(suffix) == 0: continue
                break
        self._prefix = prefix
        self._suffix = suffix

    def run(self):

        already_ran_input_prompt_this_cycle = False
        if len(self._prefix + self._suffix) == 0:
            self.update_prefix_suffix_info()
            already_ran_input_prompt_this_cycle = True

        word_at_caret_or_selection = ''
        (sel_start, sel_end) = (editor.getSelectionStart(), editor.getSelectionEnd())
        if editor.getSelections() == 1 and sel_start != sel_end:
            word_at_caret_or_selection = editor.getTextRange(sel_start, sel_end)
        else:
            start_of_word_pos = editor.wordStartPosition(editor.getCurrentPos(), True)
            end_of_word_pos = editor.wordEndPosition(start_of_word_pos, True)
            if start_of_word_pos != end_of_word_pos:
                word_at_caret_or_selection = editor.getTextRange(start_of_word_pos, end_of_word_pos)
                editor.setSelection(end_of_word_pos, start_of_word_pos)

        if len(word_at_caret_or_selection) == 0:
            if not already_ran_input_prompt_this_cycle: self.update_prefix_suffix_info()
        elif len(self._prefix + self._suffix) &gt; 0:
            editor.beginUndoAction()
            editor.replaceSel(self._prefix + word_at_caret_or_selection + self._suffix)
            editor.endUndoAction()

if __name__ == '__main__':

    try:
        t19459a
    except NameError:
        t19459a = T19459a()

    t19459a.run()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/54318</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54318</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 15:29:52 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 13:35:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18107">@phenomenal11</a> said in <a href="/post/54292">Any way to create a shortcut to add prefix and suffix to a word?</a>:</p>
<blockquote>
<p dir="auto">because i do have multiple words that i need to input.</p>
</blockquote>
<p dir="auto">One thing you haven’t made clear: whether this means (1) “I have exactly N words, <code>testword</code> and <code>anotherword</code> and … and <code>wordN</code>, which need prefixes and suffixes added”, or whether this means (2) “one time I run the script, it will be with <code>testword</code>, the next time, it will be with <code>randomword</code>, and I will not know before-the-fact what <code>testword</code> and <code>randomword</code> will or won’t be”.</p>
<p dir="auto">In case (1), it would be easy to tweak the regex to look for a variety of words: <code>\b(testword|anotherword|wordN)\b</code>.</p>
<p dir="auto">In case (2), as <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@Alan-Kilborn</a> said, “isn’t substantially differerent from a non-scripted <em>Replace</em> operation”.</p>
<p dir="auto">Your further clarification while I was writing this, “There is no option to replace only that instance of the word(or line)…” shows that you really seem to want a specialized search/replace dialog with pre-defined values rather than having to type them in.  The easiest thing to do is to have a file somewhere that pre-defines your replace expression, and so you paste in the replacement, then modify the search to your heart’s content.  But if you would prefer a custom dialog, <a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@Alan-Kilborn</a>  is kindly providing that for you.</p>
<p dir="auto">I highly recommend you study the code he’s already provided, and start thinking about how <em>you</em> might modify it to suit your needs.  When Alan comes back with a reply with customized code, you can compare his results to what you came up with.  Even if you don’t know Python all that well, at least try to come up with pseudo-Python to do it: that is, think algorithmically, and try to define the steps that you as a human would have to take to accomplish the job.  This will help you in the future, if you later decide you need to tweak the stored prefix and suffix in the code, or if you need to change your rule for when your word matches or doesn’t.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54315</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54315</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 26 May 2020 13:35:27 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 13:31:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18107">@phenomenal11</a></p>
<p dir="auto">Yes, okay, I knew I had some “disconnects” there about what was needed.  :-)</p>
<p dir="auto">I’ll rework it a bit.  Check back later.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54314</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54314</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 13:31:23 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 13:28:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@Alan-Kilborn</a><br />
Hi Alan, I followed your instructions and the script works!!<br />
I get a pop box asking what prefix and suffix i need to enter and it changes the word perfectly. Also tested on an entire line and it works for that as well.</p>
<p dir="auto"><em>But</em> if that word(or line) is repeated multiple times in the file it just replaces all of them. There is no option to replace only that instance of the word(or line)…</p>
<p dir="auto"><em>Also</em> is there a way i can pre-define what prefix and suffix need to be entered so that regardless of the selected word it adds same prefix and suffix?</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54312</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54312</guid><dc:creator><![CDATA[phenomenal11]]></dc:creator><pubDate>Tue, 26 May 2020 13:28:30 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 13:23:03 GMT]]></title><description><![CDATA[<p dir="auto">Actually, thinking about it some more, this scripted approach isn’t substantially different from a non-scripted <em>Replace</em> operation!  LOL</p>
<p dir="auto">I guess I await clarification from the OP!</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54310</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54310</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 13:23:03 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 12:50:46 GMT]]></title><description><![CDATA[<p dir="auto">So how does one use a (Python) script?</p>
<ul>
<li>Obtain the <em>Pythonscript</em> plugin (via _Plugins Admin)</li>
<li>Create a new script via <em>Plugins</em> &gt; <em>Pythonscript</em> &gt; <em>New Script</em></li>
<li>Give it a (file)name (I chose <code>PrefixOrSuffixAWord.py</code> for this one)</li>
<li>Paste the above script text into the file tab; save it</li>
<li>To run it, menu to <em>Plugins</em> &gt; <em>Pythonscript</em> &gt; <em>Scripts</em> and then pick the script from the list shown</li>
</ul>
<p dir="auto">Instructions for how to do a keyboard shortcut for a script can come after we determine if it works well for the need.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54309</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54309</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 12:50:46 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 12:46:27 GMT]]></title><description><![CDATA[<p dir="auto">Here’s a first-cut of a Pythonscript to prefix/suffix a word; there may be some tweaking we need to do to it, depending on my “confusion” about the spec :-) :</p>
<pre><code># -*- coding: utf-8 -*-

from Npp import editor

class T19459(object):
    def __init__(self):
        sep = '-'
        word_at_caret_or_selection = ''
        sel_start = editor.getSelectionStart()
        sel_end = editor.getSelectionEnd()
        if editor.getSelections() == 1 and sel_start != sel_end:
            word_at_caret_or_selection = editor.getTextRange(sel_start, sel_end)
        else:
            start_of_word_pos = editor.wordStartPosition(editor.getCurrentPos(), True)
            end_of_word_pos = editor.wordEndPosition(start_of_word_pos, True)
            if start_of_word_pos != end_of_word_pos:
                word_at_caret_or_selection = editor.getTextRange(start_of_word_pos, end_of_word_pos)
        while True:
            user_input = notepad.prompt(
                'Enter PREFIX{s}word{s}SUFFIX :\r\n(PREFIX or SUFFIX maybe be empty, but must still use {s})'.format(s=sep),
                '', sep + word_at_caret_or_selection + sep if len(word_at_caret_or_selection) &gt; 0 else '')
            if user_input == None: return  # Cancel
            parms_list = user_input.strip().split(sep)
            if len(parms_list) == 3:
                (prefix, word, suffix) = parms_list
                if len(word) == 0: continue
                if len(prefix) + len(suffix) == 0: continue
                break
        editor.beginUndoAction()
        editor.replace(word, prefix + word + suffix)
        editor.endUndoAction()

if __name__ == '__main__': T19459()
</code></pre>
]]></description><link>https://community.notepad-plus-plus.org/post/54308</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54308</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 12:46:27 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 12:42:59 GMT]]></title><description><![CDATA[<p dir="auto">So a bit of research shows there is a script method for prefixing/suffixing entire lines, if that is of interest.  See <a href="https://community.notepad-plus-plus.org/topic/16337/feature-request-modify-lines-dialog-for-adding-prefix-suffix-to-lines/">HERE</a>.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54307</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54307</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 12:42:59 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 11:59:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18107">@phenomenal11</a> said in <a href="/post/54298">Any way to create a shortcut to add prefix and suffix to a word?</a>:</p>
<blockquote>
<p dir="auto">Will i be able to execute these said scripts using a keyboard shortcut?</p>
</blockquote>
<p dir="auto">Yes.<br />
Scripts are runnable via selecting them in a menu (always), keyboard-shortcut (optionally, you must pick the keycombo), or toolbar button (optionally).</p>
<blockquote>
<p dir="auto">And i’d say a python script …</p>
</blockquote>
<p dir="auto">A good choice. :-)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54304</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54304</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Tue, 26 May 2020 11:59:07 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 11:17:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3841">@PeterJones</a><br />
:-D<br />
Do you know when you are getting old?<br />
If someone reply’s with something like:<br />
I haven’t heard of those languages and one of those is <strong>perl</strong>.<br />
:-D</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54301</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54301</guid><dc:creator><![CDATA[Ekopalypse]]></dc:creator><pubDate>Tue, 26 May 2020 11:17:01 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Tue, 26 May 2020 08:30:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/7377">@Alan-Kilborn</a> Hi Alan,</p>
<p dir="auto">Sorry for the late reply. Will i be able to execute these said scripts using a keyboard shortcut? If yes, then please do guide me :)<br />
And i’d say a python script because i have not heard of the other 2 languages…</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54298</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54298</guid><dc:creator><![CDATA[phenomenal11]]></dc:creator><pubDate>Tue, 26 May 2020 08:30:24 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Mon, 25 May 2020 18:37:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18107">@phenomenal11</a></p>
<p dir="auto">You will probably have to resort to scripting, which can do that stuff and more.<br />
Let us know if you want to go in that direction.<br />
And your preference for Python, Perl, or even Lua for a scripting language choice.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54293</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54293</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Mon, 25 May 2020 18:37:17 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Mon, 25 May 2020 17:34:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/3841">@PeterJones</a> Hi Peter,</p>
<p dir="auto">Thanks for taking the time to help me.<br />
Unfortunately, this method won’t work for me because i do have multiple words that i need to input.<br />
But i might have a way to bypass this requirement so i ask you another question: Say i want to add a prefix and a suffix to only the bookmarked lines how would i achieve that using find/replace. I saw a couple of solutions on google but they apply the change to <em>all</em> of the lines.<br />
Thanks</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54292</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54292</guid><dc:creator><![CDATA[phenomenal11]]></dc:creator><pubDate>Mon, 25 May 2020 17:34:44 GMT</pubDate></item><item><title><![CDATA[Reply to Any way to create a shortcut to add prefix and suffix to a word? on Mon, 25 May 2020 16:57:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.notepad-plus-plus.org/uid/18107">@phenomenal11</a>,</p>
<p dir="auto">There is a section in the usermanual with a detailed description of macros for search/replace functions:  <a href="https://npp-user-manual.org/docs/searching/#searching-actions-when-recorded-as-macros" rel="nofollow ugc">https://npp-user-manual.org/docs/searching/#searching-actions-when-recorded-as-macros</a></p>
<p dir="auto">However, all you likely need is to use <strong>Macro &gt; Start Recording</strong>, then <code>Ctrl+H</code> to open the replace dialog, then fill out and run the <strong>Replace</strong> or <strong>Replace All</strong>, then <strong>Macro &gt; Stop Recording</strong> and <strong>Macro &gt; Save Current Recorded Macro</strong></p>
<p dir="auto"><em>Note that this assumes that your <code>testword</code> is always the same <code>testword</code> (or at least can always match the same regular expression)… if it’s not, you’ll just need to learn how to run the replace macro and fill out the regex quickly.</em></p>
<p dir="auto">For an example, assuming that <code>\b(testword)\b</code> will match your desired search term, and <code>ABC${1}XyZ</code> will be the correct replacement, if you record that macro and save it, then <code>%AppData%\Notepad++\shortcuts.xml</code> will contain something similar to</p>
<pre><code>    &lt;Macro name="ex19459b" Ctrl="no" Alt="no" Shift="no" Key="0"&gt;
        &lt;Action type="3" message="1700" wParam="0" lParam="0" sParam="" /&gt;
        &lt;Action type="3" message="1601" wParam="0" lParam="0" sParam="\b(testword)\b" /&gt;
        &lt;Action type="3" message="1625" wParam="0" lParam="2" sParam="" /&gt;
        &lt;Action type="3" message="1602" wParam="0" lParam="0" sParam="ABC${1}XyZ" /&gt;
        &lt;Action type="3" message="1702" wParam="0" lParam="768" sParam="" /&gt;
        &lt;Action type="3" message="1701" wParam="0" lParam="1608" sParam="" /&gt;
    &lt;/Macro&gt;
</code></pre>
<p dir="auto">and when you run that macro from the <strong>Macro</strong> menu, it will replace a single instance of the <code>testword</code>.  If you recorded with <strong>Replace All</strong> instead of <strong>Replace</strong>, then the last action would have had <code>lparam="1609"</code> instead.</p>
<p dir="auto">Using <strong>Macro &gt; Modify Shortcut / Delete Macro</strong> will allow you to add a keyboard shortcut to that particular macro by selecting the <code>ex19459b</code> (*) row in the dialog, then clicking on <strong>Modify</strong> and assigning the desired keyboard shortcut.  (<strong>edit</strong>: or when you originally saved and named the macro, you could have assigned the keyboard shortcut then.)</p>
<p dir="auto">*: I chose that name <code>ex19459b</code> because <code>ex</code> means example, <code>19459</code> is the topic number in the URL of your question here, and <code>b</code> because I made a mistake while recording the first version of the macro)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/54291</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/54291</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Mon, 25 May 2020 16:57:42 GMT</pubDate></item></channel></rss>