Can I make NotePad++ auto-replace a string?



  • Whenever I type “–” (two dashes), I’d like to make Notepad++ to replace them with an en-dash (–). Is this possible?



  • No, there is no built-in option for this.
    Don’t know what is your use case. Just guessing - if you need en-dash often then just bind a key which insert en-dash.
    It can be done e.g. with Autohotkey app, or in Notepad++ - e.g. with Pythonscript plugin.



  • @Henry-Vistbacka

    As @Mikhail-V suggests, you could do such a thing if you are willing to install and use the PythonScript plugin. Maybe there are other ways as well, but all would involve installing something extra, so why not the PS plugin? :-)

    Here’s some quick sample PS code, modify/experiment/enhance as you like:

    search_text = '--'
    replacement_text = 'en-dash'  # <----- this is just placeholder, put what you want here
    
    def callback_sci_CHARADDED(args):
        if chr(args['ch']) == search_text[-1]:
            cp = editor.getCurrentPos()
            search_text_length = len(search_text)
            start_of_search_text_pos = cp - search_text_length
            if editor.getTextRange(start_of_search_text_pos, cp) == search_text:
                editor.beginUndoAction()
                editor.deleteRange(start_of_search_text_pos, search_text_length)
                editor.insertText(start_of_search_text_pos, replacement_text)
                editor.endUndoAction()
                end_of_search_text_pos = start_of_search_text_pos + len(replacement_text)
                editor.setCurrentPos(end_of_search_text_pos)
                editor.setSelection(end_of_search_text_pos, end_of_search_text_pos)
                editor.chooseCaretX()
    
    editor.callback(callback_sci_CHARADDED, [SCINTILLANOTIFICATION.CHARADDED])
    

Log in to reply