Hard-Insert CRLF in Textfiles after n characters?

Jackie78
2012-12-08
2012-12-30
  • Jackie78
    Jackie78
    2012-12-08

    Hi,

    I would like to automatically insert linefeeds after a variable number of characters (not only visually, but real linefeeds in the file), optionally only after spaces (i.e. only after whole words).

    How can that easy task be achieved? I found no such option anywhere...

    Thanks

     
    Last edit: Jackie78 2012-12-08
  • cchris
    cchris
    2012-12-29

    TextFX will do this for you after the typing, through its Reformat paragraph commands.

    CChris

     
  • THEVENOT Guy
    THEVENOT Guy
    2012-12-30

    Hello Jackie78,

    I saw the post of cchris ( Christian Cuvier ) about the use of the
    TextFX plugin. It could be a good solution, but you can simply
    use the Search-Replacement fonction of Notepad++, in Regular
    Expression Mode, to do the job !

    In a new file :

    • Type the last sentence of your post :

    How can that easy task be achieved? I found no such option anywhere..

    • Go back at the beginning of the file ( CTRL + Origin )

    • Open the Search-Replacement windows ( CRTL + H )

    • Click the radio button " Regular expression " in Search Mode part

    • Type in the Search dialog .{6}(?!\R)

    • Type in the Replacement dialog $0\r\n

    • Click, to end, the " Replace All " button

      => we obtain the text below, with a column of SIX characters long :

    How ca
    n that
    easy
    task b
    e achi
    eved?
    I foun
    d no s
    uch op
    tion a
    nywher
    e..

    Notes :

    - The number 6, inside parenthesis, can be any natural number > 0
    
    - The EOL character, in the Replacement dialog, must be  \n  ONLY,
       for a UNIX file or  \r  ONLY, for a MAC file
    

    The form (?!\R) is called a LookAhead and means : find, at least, six
    characters which are not followed by an EOL ( \r\n or \n or \r )

    In other words, this search-replacement means :

    Find a string of seven or more characters and then, rewrite the FIRST
    six characters of that string, followed by EOL character(s)

    For example the text below :

    1
    12
    123
    1234
    12345
    123456
    1234567
    12345678
    123456789
    1234567890
    12345678901
    123456789012

    will be changed in :

    1
    12
    123
    1234
    12345
    123456
    123456
    7
    123456
    78
    123456
    789
    123456
    7890
    123456
    78901
    123456
    789012

    To achieve the same thing, you can also use this formulation :

    • Type in the Search dialog .{6}(?!\R)\K

    • Type in the Replacement dialog \r\n

    The \K form means that everything before \K is forgotten. So,
    all this means : After the next six characters, add EOL
    character(s) to the text


    If you prefer to split sentences in words, once again, just use
    a Search-Replacement, in Regular expression search mode !

    • Type, again, in a NEW file, the last sentence of your post :

    How can that easy task be achieved? I found no such option anywhere..

    • Go back at the beginning of the file ( CTRL + Origin )

    • Open the Search-Replacement windows ( CRTL + H )

    • Click the radio button " Regular expression " in Search Mode part

    • Type in the Search dialog (.+?)[ \t]+

    • Type in the Replacement dialog \1\r\n

    • Click, to end, the " Replace All " button

      => we obtain the text below :

    How
    can
    that
    easy
    task
    be
    achieved?
    I
    found
    no
    such
    option
    anywhere..

    Notes :

    - I suppose that a list of combined SPACES ( \x20 ) and TABULATIONS
        ( \x09 ) stands for a delimiter, but you can add other characters !
    
    - I suppose that any character, which is NOT a delimiter, can be a
        character of a word.
    

    All this means : find the SHORTEST string of characters, before a string of
    combined SPACE(S) and/or TABULATION(S), and then, rewrite ONLY that string,
    followed by EOL character(s)


    To end, if you prefer to use the strict definition of character
    of word ( so a word character is [A-Za-z0-9_] ) :

    • Type, again, in a NEW file, the last sentence of your post :

    How can that easy task be achieved? I found no such option anywhere..

    • Go back at the beginning of the file ( CTRL + Origin )

    • Open the Search-Replacement windows ( CRTL + H )

    • Click the radio button " Regular expression " in Search Mode part

    • Type in the Search dialog (\w+)\W+

    • Type in the Replacement dialog \1\r\n

    • Click, to end, the " Replace All " button

      => we obtain the text below :

    How
    can
    that
    easy
    task
    be
    achieved
    I
    found
    no
    such
    option
    anywhere

    This last Search-Replacement means : Find a string of word characters,
    followed by a string of non-word characters and then, rewrite ONLY
    the string of word characters, followed by EOL character(s)

    I hope theses explanations will be useful for your needs !

    Best Regards !

    guy038

    P.S. You can also find some documentation, about the new PRCE Regular Expressions, used by N++, since the
    6.0 version, at the two adresses below :

     http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
    
     http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
    
     The FIRST one concerns the syntax of regular expressions in the SEARCH part
    
     The SECOND one concerns the syntax of regular expressions in the REPLACEMENT part