How to copy strings?



  • I have a bunch of scripts containing also text within strings like this

    clark
       "He mentioned that already a few weeks ago"
    

    What I want is a simple method to copy those text lines preferable with just a simple click rather than manually moving the mouse which gets bothersome, especially if you’re tired. In notepad++ whenever I double click those lines it marks the the whole code line unfortunately but I just want the text within those strings “”



  • @NiceGlassesDude said in How to copy strings?:

    I want … a simple method

    Hmm, well simple is in the eye of the beholder.

    I don’t think you’re going to get any solution that is native to Notepad++.

    A Pythonscript could do it, if you’re willing to install and use that plugin.



  • For example, this Pythonscript can do it, if you put your caret somewhere inside a double-quoted region and then run it:

    
    # -*- coding: utf-8 -*-
    
    from Npp import editor, notepad
    
    prematches = []
    postmatches = []
    
    cp = editor.getCurrentPos()
    
    editor.search(r'"', lambda m: prematches.append(m.span(0)[1]), 0, 0, cp)
    editor.search(r'"', lambda m: postmatches.append(m.span(0)[0]), 0, cp, editor.getTextLength(), 1)
    
    if len(prematches) != 0 and len(postmatches) != 0: editor.copyText(editor.getTextRange(prematches[-1], postmatches[0]))
    
    


  • @Alan-Kilborn

    Thank you very much. I never used Python, though. Which plugin do I need in notepad++ for this script?.



  • @NiceGlassesDude

    Gosh. I thought I was fairly explicit about it, but here you go:

    9cd0404a-6a87-43a2-8c36-a5fe0f5906f8-image.png



  • Hi @Alan-Kilborn, @NiceGlassesDude, All

    As an alternative method, OP’s requirement can also be accomplished with a combination of some internal features of Notepad++, as follows:

    Assuming the caret is inside a doble-quoted string

    1. Run a backward search in normal mode to find the opening quote.
    2. Move one character to the right to put the caret at the beginning of the wanted string.
    3. Run a search in regex mode to match all characters up to the closing quote.
    4. Copy the matched characters.

    The nice thing is that this method is macro recordable:

        </Macro>
        <Macro name="Copy quoted strings" Ctrl="no" Alt="no" Shift="no" Key="0">
            <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1601" wParam="0" lParam="0" sParam='&quot;' />
            <Action type="3" message="1625" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1702" wParam="0" lParam="256" sParam="" />
            <Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
            <Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
            <Action type="3" message="1601" wParam="0" lParam="0" sParam='[^&quot;]+(?=&quot;)' />
            <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
            <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
            <Action type="3" message="1701" wParam="0" lParam="1" sParam="" />
            <Action type="0" message="2178" wParam="0" lParam="0" sParam="" />
            <Action type="2" message="0" wParam="42002" lParam="0" sParam="" />
            <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
        </Macro>
    

    Now, you can record a macro following the steps stated above or copy the provided one. For the later approach, please do as follows:

    Open your active shortcuts.xml configuration file —as always, back-up it first—, with another editor like MS Notepad. Copy the macro and paste it in the macro section, save changes, close the file and restart Notepad++. Then you can assign a shortcut to the macro via the Configuration menu.

    As you surely noted, this method is nothing new or original, is quite similar to the Python script posted before, but could look simpler to non programmers users. Anyway, I agree with @Alan-Kilborn in that simplicity is in the eye of the beholder.

    Have fun!



  • @astrosofista

    Nice one.

    I had said:

    I don’t think you’re going to get any solution that is native to Notepad++.

    Sometimes it is good to be wrong.

    simplicity is in the eye of the beholder.

    True, but here’s the backstory: I pulled the posted script from something I already had that selects text either between pairs of single quotes or double quotes (and a few other paired-delimiters). Since the OP had a limited need (double quotes), and realizing that most readers that could find it useful have a better chance of understanding a simpler script, I hacked it down.

    I suppose my posted script has “extensibility” going for it, as opposed to the macro solution, that to me seems difficult to extend to even single quotes.



  • @Alan-Kilborn said in How to copy strings?:

    I suppose my posted script has “extensibility” going for it, as opposed to the macro solution, that to me seems difficult to extend to even single quotes.

    Yes, though my regex solves OP’s problem, lack of extensibility is one of its drawbacks - Regarding this issue, for the moment I run out of ideas, but don’t give up.

    Also, compared with your PS, my regex has another drawback, as it couldn’t preserve the original position of the caret. What a shame! ha ha

    Have fun!


Log in to reply