Paste preserving the indentation + Whitespace conversion issue.



  • Hi. So, today i copy pasted some code into existing code in np++ and the indents got erased off, so is there a way to paste something and still keep the indentation that was originally there?

    Also, I did the convert spaces to tabs operation. And because my tabs are displayed in the length of 2 spaces and not 4, I had 2 times the number of indents that I should have had.

    Basically, notepad++ can not tell if the text is formatted with two spaces or 4 spaces etc. How can we make it know this so it can always get the spaces to tabs operation right? (disappointing ;( )



  • Hi, @salviasage, and All,

    I don’t, exactly, figure out what you mean ?

    For instance, assuming any line, written with Microsoft Notepad, beginning with a tabulation character \t (which is 8 positions width, by default, in Notepad ) you’ll get, after pasting in N++, a line beginning with a same tabulation character of 4-positions width , as the default N++ tab-size is 4. Logical ! Indeed, we, simply, copy the \t character, not a n spaces area, whatever n !

    So, it’s up to you to :

    • Firstly, modify the N++ tab size to the default tab-size value of the software whose you’re copying data from

    • Secondly, modify, again, the N++ tab-size to any value that you like to, in order to normalize the leading blanks of your text


    I, personally, haven’t noticed any issue, yet, regarding the TAB to Space and Space to TAB operations ! Could you provide a real short example which shows the problem.

    Best Regards,

    guy038



  • Okay Check out this piece of autohotkey code.

    Loop
        {
        ControlSend,,1 {3}, MEmu
        Sleep 3000
        ControlSend,,1 {4}, MEmu
        Sleep 3000
        ControlSend,,1 {5}, MEmu
        Sleep 3000
        ControlSend,,1 {6}, MEmu
        Sleep 3000
        }
    return
    

    I copy paste code into the indented area inside the brackets. then it looks like this:

    Loop
        {
    ControlSend,,1 {3}, MEmu
    Sleep 3000
    ControlSend,,1 {4}, MEmu
    Sleep 3000
    ControlSend,,1 {5}, MEmu
    Sleep 3000
    ControlSend,,1 {6}, MEmu
    Sleep 3000
        }
    return
    

    You see, the indentation has been wiped. I want to be able to preserve the indentation in these scenarios when I copy paste something in them. So, upon paste it will look like this instead:

    Loop
        {
        ControlSend,,1 {3}, MEmu
        Sleep 3000
        ControlSend,,1 {4}, MEmu
        Sleep 3000
        ControlSend,,1 {5}, MEmu
        Sleep 3000
        ControlSend,,1 {6}, MEmu
        Sleep 3000
        }
    return
    

    Now, check out this code, it uses 4 spaces to indent, My notepad++ has its tab length said to 2, so I do the convert leading spaces into tabs operation, and I get 2 tabs per indent instead when it should be 1 tab per indent.

    Of course, I get the correct number of indents if I manually first see that the indents are 4 spaces, then I go to the menu to change my tab length to 4 spaces and then do the operation.

    But, I just think it would be so much easier if notepad++ could just handle this for us instead.
    So, is there a way to add this into NP++ maybe with pythonscript etc?

    This is useful because not everyone uses the same indentation style, and notepad++ could be “Smart” about it and handle it for you.



  • Hi, @salviasage, and All,

    I still miss something or I probably not be fully awoken :-(( Seemingly, you’re discussing of two separate problems :


    • Regarding the Edit > Blank Operations > Space to TAB… option, it’s quite normal that any block, of Autoit lines, with four leading spaces will be changed into the same block, with two tabulation characters, IF your current N++ tab-size for, either, all languages or AutoIt language only, is set to Tabulation of width = 2 !

    Obviously, this conversion just changes the blank characters, according to your present preferences, doesn’t it ?


    • Regarding the pasting operation, do you mean that :

      • Assuming your current tab-size ( Tabulation with width = 2 )

      • Assuming the simple AutoIt code, within a file, with AutoIt language set, with the two braces indented with two tabulation characters of width = 2:

    Loop
    		{
    		}
    return
    
    • and some “AutoIt” text, without any leading indentation, below, copied from an other file :
    ControlSend,,1 {3}, MEmu
    Sleep 3000
    ControlSend,,1 {4}, MEmu
    Sleep 3000
    ControlSend,,1 {5}, MEmu
    Sleep 3000
    ControlSend,,1 {6}, MEmu
    Sleep 3000
    
    • When pasted between the two curly braces,above, do you expect this text to be automatically indented, with your current tab-size, as below ? ( So each indented line begins with two tabulation characters of witdh = 2 )
    Loop
    		{
    		ControlSend,,1 {3}, MEmu
    		Sleep 3000
    		ControlSend,,1 {4}, MEmu
    		Sleep 3000
    		ControlSend,,1 {5}, MEmu
    		Sleep 3000
    		ControlSend,,1 {6}, MEmu
    		Sleep 3000
    		}
    return
    

    If so, I suppose that some scripting should be necessary, as it’s not the default N++ pasting behaviour !

    Cheers

    guy038

    After posting, I realized that text, containing tabulations chars, inserted, on our Node BB site, in an inverted background block of text is automatically changed into a text with 4 spaces per initial tabulation !

    So, unfortunately, if you paste the block code, just above, in N++, it will not correspond to my initial text, as all my indented lines began with two tabulation characters :-((



  • Yes exactly. It is not default N++ behaviour. I know that.

    It’s just sad that the indentation gets wiped out upon pasting. I want an option to be able to preserve it on pasting code instead.

    And as for the conversion operation, Notepad++ simply does not know how many indents there are. It just replaces the spaces with tabs instead according to the length you have set your tabs to be.

    This is undesirable, as you may get more or less indentation levels then what should be when you do the spaces to tabs operation, (this will effectively break your code in python if you do the spaces to tabs conversion)

    Find some python code and test the operation yourself if you want to have a better understanding of what I am saying.



  • Do you have any plugins that may auto-indent code?
    What application do you copy from?
    Did you consider that this app may copy un-indented text to the clipboard and NP++ just pastes whatever in there?



  • I don’t have plugins for autoindenting code aside from n++ automatic indent on new line behaviour.
    I copy the code from discord and without spaces thats true, and paste into notepad++ but the text in notepad++ has the indents, and they get wiped.

    I cant just copy the whole block for this reason, Now, I copy things that have indents line by line, which of course has more steps than just being able to copy paste and keep the indents.

    That’s all.



  • Pasting is not identical to typing the text “symbol by symbol”.
    The auto-indent logic is triggered by the “enter” key press. This does not happen with paste and 99% of users will not want it to happen since pasting already indented text between source files, which is what code developers do, will become a mess.
    Paste just inserts at the cursor a bulk of text: symbols, spaces, tabs and new-line symbols. Then NP++ redo syntax highlighting from the insertion place.

    The semi-easy solution is: copy all the lines, select them, and indent them as a block.
    You may even be able to create a macro for that.



  • How to do that?



  • I don’t see anything wrong with the way Notepad++ pasting works.

    It is not often that pasted code is indented to the same level as the code being pasted into; I do this type of pasting A LOT. What would be best here would be if Scintilla kept the pasted text selected (if it is whole-line text that is either being inserted at the caret point, and–optionally-- is replacing a previous selection of whole lines). If it were kept selected, a quick TAB or Shift+TAB press(es) would adjust it to the right indent level quickly.

    Otherwise, we have to be careful that we don’t talk about potential features that do too much mind-reading. Notepad++ isn’t an IDE that is really good at one language-it’s a general purpose tool that tries to do a lot of things acceptably well. It largely succeeds.



  • @SalviaSage said:

    How to do that?

    Select lines with shift-arrows.
    Click tab to indent or shift-tab to unindent.



  • One of the neat tricks about a stream selection that contains one or more line-endings is that you don’t have to have a selection covering an integral number of lines in order to tab-indent (or [shift-]tab-outdent) the lines touched. An example:

    Imgur

    Note that in the beginning, only part of the first line and part of the last line are selected. A tab (or shift+tab) press affects the lines as if they were entirely selected, and in fact after the first tab keypress they are entirely selected.



  • That’s nice. I didn’t know you could do that. That effectively solves my problem. Although I may not be able to preserve the indents, I can then just select my lines and press tab.

    This of course doesn’t work for those infidel space indenters, Which is why I need the convert spaces to tabs working correctly.

    Look at my example above. If a space indenter indented with 4 spaces and I have my tab length set to 2 spaces, when I do the operation, I get 2 tabs when I should get 1. Can this be fixed with a script?



  • Hi, @salviasage, and All,

    To my mind, it’s quite normal that N++ replace a 4-spaces indented text with 2-tabulations of size 2, because, from your preferences, you decided :

    • To use tabulation characters instead of spaces

    • To define that each tabulation will take 2 positions

    Indeed, if you expect a unique tabulation, taking 4 positions, it would, then, contradict your preferences !!

    Cheers,

    guy038



  • that would be the case if it was you who was also indenting with 2 spaces, but you are not the only one who codes, you get a code from other people who may indent with 4 spaces, 6 spaces, 8 spaces or whatever it is that they decided to indent with.

    Then, you want to convert that to your own style, and you cannot do this automatically, you still have to manually do it, by going into the preferences and changing your tab length to whatever the other coder formatted with, then you open another code formatted with a different set of spaces, then you do the same etc.

    At least, it would be nice if we had like a tab length : 2 option in the status bar, where you can quickly set your tab length.

    We can easily see what EOL the file is formatted with and convert the EOLs from the statusbar.
    I really like that EOL option offered there on the status bar, and is perfect.
    But, the same is simply not the case for the indents. That area, I think can be improved upon programmatically.

    If anyone knows a script to make these conversions more automatic, please let me know.


Log in to reply