Community
    • Login

    Looking to a way to copy and paste code in a way that matches the indentation at the spot it is pasted

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 4 Posters 231 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.
    • V
      VertyyBird
      last edited by

      So one issue I’ve had with N++ for ages is the hassle it gives when I want to paste in a block of code. If the pasted code doesn’t have the same exact indentation scheme that my document has, it makes a mess and I have to go and fix each line individually.

      For example, here I pasted in a template of all common HTML meta tags. The source that I copied from has an indentation level of 0. I put my cursor in N++ at an indentation level of 1 (2 spaces), which is inline with the rest of the tags in my head section. I would expect N++ to “add” indentation to the lines I’m pasting so that it would all be in line with the rest of the code, but instead it puts it all at indentation level 0.

      Is there a known “fix” or shortcut for this? I’ve searched high and low for a while and couldn’t find anything helpful. Thanks!

      Pictures for reference

      Before pasting:

      After pasting:

      What I expect it to do when pasting:

      1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson
        last edited by

        You could try and find a plugin for pretty-printing HTML. XMLTools can sort of do this, but I’ve tried it and I don’t particularly like its pretty-printing style.

        A reasonable alternative would be to use PythonScript, import lxml and read the documentation for the lxml standard library module to find a way to reformat HTML in whatever way pleases you most. You could even have the PythonScript script run every time you save a file, before it saves (NPPN_FILEBEFORESAVE callback IIRC).

        I have long since abandoned hope that Notepad++ will automatically indent code in any way that is both consistent and desirable, and I mostly just use regular expressions and plugins to indent things.

        1 Reply Last reply Reply Quote 2
        • Alan KilbornA
          Alan Kilborn
          last edited by

          There are some approached discussed HERE.

          dr ramaanandD 1 Reply Last reply Reply Quote 1
          • dr ramaanandD
            dr ramaanand @Alan Kilborn
            last edited by dr ramaanand

            @VertyyBird It would be simpler to select the Regular expression mode, put the 2 lines you want to find in the, “Find” field and hit Replace after typing $0\r\n(new line1)\r\n(new line2)\r\n(new line3)\r\n(new line4)\r\n(new line5)\r\n(new line6)\r\n(new line7)\r\n(new lin8)\r\n(new line9)\r\n(new lline10)\r\n(new line11)\r\n(new line12)\r\n(new line13)\r\n(new line14)\r\n(new line15)\r\n(new line16)\r\n(new line17)\r\n(new lin18)\r\n(new line19)\r\n(new lline20)\r\n(new line21)\r\n(new line22)\r\n(new line23)\r\n(new line24)\r\n(new line25)\r\n(new line26)\r\n(new line27)\r\n(new lin28)\r\n(new line29)\r\n(new lline30) in the “Replace” field - the \r\n(new line) puts the new line on the next line. I am presuming that you have to add some 30 new lines

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