Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 2.6k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Henry VistbackaH Offline
      Henry Vistbacka
      last edited by

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

      Scott SumnerS 1 Reply Last reply Reply Quote 0
      • Mikhail VM Offline
        Mikhail V
        last edited by Mikhail V

        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.

        1 Reply Last reply Reply Quote 1
        • Scott SumnerS Offline
          Scott Sumner @Henry Vistbacka
          last edited by

          @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])
          
          1 Reply Last reply Reply Quote 2

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors