Regex: Copy first 5 lines from all open documents

  • hi, I want to copy just the first 5 lines from all open documents, for this I made the regex below:

    \A(?:.*\n){5} or

    \A^.*(?:\R.*){5} or


    But none of the variants is good because the operation is redundant, keeps repeated on every 5 lines. So I need strictly the first 5 lines !

    cand anyone help me?

  • @Vasile-Caraus

    Your best option might be to do a replacement rather than a find–I’m not sure how you’d easily “copy” the results otherwise.

    I’d try:

    Find what zone: (?-s)((?:.*?\R){5})(?s).*
    Replace with zone: \1
    Search mode: Reg Exp (obviously!)
    Action: Press Replace All in All Open Docs button
    Post-replace actions: Go to all of the tabs individually, select-all, copy, Undo (the Replace All)…then paste the clipboard contents wherever you are accumulating the results

    Probably not what you are looking for, though…

  • I believe the best option to search and find, which I tested again and works very good, is this one:


    I am using grepWIn to search and find and copy the first 5 lines from a lot of files :)

  • Hi, @vasile-caraus, @Scott-sumner and All,

    Indeed, in that matter, N++ totally fails to solve this simple goal :-(( And this, for two main reasons :

    • Firstly, because the N++ regex engine does not handle properly, backward assertions ( as \A , \<, \b before a word,… )

    • Secondly, because the results, in the Find result windows, display a single line , even if a multi-lines amount of text had been matched !

    For instance, as a work-around, with the François-R Boyer regex engine, and N++ v6.9.0, I used, successively, the following 5 regexes :

    • (?-s)\A(.*\R){0}\K.*\R , which, correctly, match the first line of any opened file

    • (?-s)\A(.*\R){1}\K.*\R , which, correctly, match the second line of any opened file

    • (?-s)\A(.*\R){2}\K.*\R , which, correctly, match the third line of any opened file

    • (?-s)\A(.*\R){3}\K.*\R , which, correctly, match the fourth line of any opened file

    • (?-s)\A(.*\R){4}\K.*\R , which, correctly, match the fifth line of any opened file

    After 5 clicks, on the Find All in All Opened Documents button, I, then, copied these 5 search results in a new tab.

    Finally, after a classical sort, depending on the file name and the number of the line matched, I could get the 5 first lines of all my opened files ! Quite boring !

    Here, Vasile, it should be interesting to use, instead, a simple Lua or Python script, for nice and immediate results ! Of course, some external tools, as you said, can realize this work, quite well, too !

    Best Regards,


    You may look this alternative solution, in the last part of this post :

Log in to reply