Community
    • Login

    Mass cursor movement via Python

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 2 Posters 36 Views
    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.
    • M Andre Z EckenrodeM
      M Andre Z Eckenrode
      last edited by

      As I’ve pointed out in one or more previous posts, I’m a very amateurish user of the Python Script v2.1.0.0 plugin, with the majority of my simple scripts made up only of regex replacement sequences, but on rare occasions I need to do something requiring a little more sophistication. I’m currently investigating ways to move my cursor x number of spaces/words/lines at a time, such as moving it up 5 lines. I see, in the Python Script docs, that I can use editor.lineUp() to move it up one line at a time, but of course am hoping for a way to move up all 5 lines in one go, so to speak, rather than having to repeat that line of code 5 times in my script. I figured a loop would be the most obvious solution, but my experience with them is pretty limited, so I consulted the PS docs further, as well as the NPP Community here, and our old sometimes-friend Google. I see multiple examples of simple, basic loops using for i in range(), which I tried as follows:

      for i in range(5)
          editor.lineUp()
      

      But the Python Script console outputs the following:

          for i in range(5)
                          ^
      SyntaxError: invalid syntax
      

      Am I doing something wrong?

      Not that I’m inclined to trust any AI information, but when I googled notepad++ pythonscript move caret one direction multiple times, Google’s AI Overview also suggests that I can just use editor.lineUp(5). It only cost me less than a minute to give that a whirl, so what the heck, but I faired no better with it (issued via the console rather than a saved script). Console output:

      >>> editor.lineUp(5)
      Traceback (most recent call last):
        File "<console>", line 1, in <module>
      ArgumentError: Python argument types in
          Editor.lineUp(Editor, int)
      did not match C++ signature:
          lineUp(class NppPythonScript::ScintillaWrapper {lvalue})
      

      Full debug info:

      Notepad++ v8.9 (32-bit)
      Build time: Dec 27 2025 - 14:27:12
      Scintilla/Lexilla included: 5.5.8/5.4.6
      Boost Regex included: 1_85
      TinyXML-2 included: 11.0.0
      nlohmann JSON included: 3.12.0
      Path: C:\Program Files (x86)\Notepad++\notepad++.exe
      Command Line:
      Admin mode: OFF
      Local Conf mode: OFF
      Cloud Config: OFF
      Periodic Backup: ON
      Placeholders: OFF
      Scintilla Rendering Mode: SC_TECHNOLOGY_DIRECTWRITE (1)
      Multi-instance Mode: monoInst
      asNotepad: OFF
      File Status Auto-Detection: cdEnabledNew (for current file/tab only)
      Dark Mode: OFF
      Display Info:
      primary monitor: 1920x1080, scaling 100%
      visible monitors count: 1
      installed Display Class adapters:
      0000: Description - Intel® HD Graphics 620
      0000: DriverVersion - 31.0.101.2111
      0001: Description - NVIDIA GeForce 940MX
      0001: DriverVersion - 30.0.15.1169
      OS Name: Windows 10 Enterprise (64-bit)
      OS Version: 22H2
      OS Build: 19045.6466
      Current ANSI codepage: 1252
      Plugins:
      BetterMultiSelection (1.5)
      ColumnsPlusPlus (1.3)
      ColumnTools (1.4.5.1)
      ComparePlus (2.2)
      DSpellCheck (1.5)
      ExtSettings (1.3.1)
      HTMLTag_unicode (1.5.5)
      mimeTools (3.1)
      MultiClipboard (2.1)
      MultiReplace (4.6.0.33)
      NppCalc (1.5)
      NppConverter (4.7)
      NppExport (0.4)
      NPPJSONViewer (2.1.1)
      NppTextFX (1.4.1)
      NppXmlTreeviewPlugin (2)
      PreviewHTML (1.3.3.3)
      PythonScript (2.1)
      RegexTrainer (1.2)
      SessionMgr (1.4.4)

      M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 0
      • M Andre Z EckenrodeM
        M Andre Z Eckenrode @M Andre Z Eckenrode
        last edited by

        Never mind on the use of for i in range(), I see now that I neglected to put a colon after it.

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @M Andre Z Eckenrode
          last edited by

          @M-Andre-Z-Eckenrode

          Instead of the .lineUp() approach, I’d try finding the exact position you want to move the caret to, and then setting the current position to that.

          You’d need to find what column you’re in, what line you’re on, and then apply math and go from there.

          M Andre Z EckenrodeM 1 Reply Last reply Reply Quote 0
          • M Andre Z EckenrodeM
            M Andre Z Eckenrode @Alan Kilborn
            last edited by

            @Alan-Kilborn

            Thanks for the suggestion, though I think .lineUp() is sufficient for my purposes in this case, since I will need to repeatedly go up the same number of lines, and column isn’t a concern because I’ll always be landing on blank lines.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors