Vim - how to replace one new line n with two n's

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












29















In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.



Input file content:



This is my first line.
This is second line.


Command that I tried:



:%s/n/nn/g


it replaces the string with unwanted characters as



This is my first line.^@^@This is second line.^@^@


Then I tried the following command



:%s/n/rr/g


It is working properly. Can you explain me why it is working fine with second command?










share|improve this question
























  • See also unix.stackexchange.com/a/206404

    – Stéphane Chazelas
    Dec 4 '15 at 11:35






  • 2





    FYI: There is also a Vi and Vim (beta) SE.

    – Dubu
    Dec 4 '15 at 12:55






  • 2





    Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

    – DJMcMayhem
    Dec 5 '15 at 4:02






  • 1





    Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

    – Anton Tarasenko
    Dec 7 '18 at 15:36















29















In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.



Input file content:



This is my first line.
This is second line.


Command that I tried:



:%s/n/nn/g


it replaces the string with unwanted characters as



This is my first line.^@^@This is second line.^@^@


Then I tried the following command



:%s/n/rr/g


It is working properly. Can you explain me why it is working fine with second command?










share|improve this question
























  • See also unix.stackexchange.com/a/206404

    – Stéphane Chazelas
    Dec 4 '15 at 11:35






  • 2





    FYI: There is also a Vi and Vim (beta) SE.

    – Dubu
    Dec 4 '15 at 12:55






  • 2





    Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

    – DJMcMayhem
    Dec 5 '15 at 4:02






  • 1





    Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

    – Anton Tarasenko
    Dec 7 '18 at 15:36













29












29








29


7






In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.



Input file content:



This is my first line.
This is second line.


Command that I tried:



:%s/n/nn/g


it replaces the string with unwanted characters as



This is my first line.^@^@This is second line.^@^@


Then I tried the following command



:%s/n/rr/g


It is working properly. Can you explain me why it is working fine with second command?










share|improve this question
















In vim editor, I want to replace a newline character (n) with two new line characters (nn) using vim command mode.



Input file content:



This is my first line.
This is second line.


Command that I tried:



:%s/n/nn/g


it replaces the string with unwanted characters as



This is my first line.^@^@This is second line.^@^@


Then I tried the following command



:%s/n/rr/g


It is working properly. Can you explain me why it is working fine with second command?







vim regular-expression vi-mode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '15 at 12:14









Michael Durrant

16.5k45121187




16.5k45121187










asked Dec 4 '15 at 10:21









RaghvendraRaghvendra

372139




372139












  • See also unix.stackexchange.com/a/206404

    – Stéphane Chazelas
    Dec 4 '15 at 11:35






  • 2





    FYI: There is also a Vi and Vim (beta) SE.

    – Dubu
    Dec 4 '15 at 12:55






  • 2





    Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

    – DJMcMayhem
    Dec 5 '15 at 4:02






  • 1





    Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

    – Anton Tarasenko
    Dec 7 '18 at 15:36

















  • See also unix.stackexchange.com/a/206404

    – Stéphane Chazelas
    Dec 4 '15 at 11:35






  • 2





    FYI: There is also a Vi and Vim (beta) SE.

    – Dubu
    Dec 4 '15 at 12:55






  • 2





    Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

    – DJMcMayhem
    Dec 5 '15 at 4:02






  • 1





    Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

    – Anton Tarasenko
    Dec 7 '18 at 15:36
















See also unix.stackexchange.com/a/206404

– Stéphane Chazelas
Dec 4 '15 at 11:35





See also unix.stackexchange.com/a/206404

– Stéphane Chazelas
Dec 4 '15 at 11:35




2




2





FYI: There is also a Vi and Vim (beta) SE.

– Dubu
Dec 4 '15 at 12:55





FYI: There is also a Vi and Vim (beta) SE.

– Dubu
Dec 4 '15 at 12:55




2




2





Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

– DJMcMayhem
Dec 5 '15 at 4:02





Side note, you don't have to use / as the separator. This is more readable: :%s;n;nn;g

– DJMcMayhem
Dec 5 '15 at 4:02




1




1





Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

– Anton Tarasenko
Dec 7 '18 at 15:36





Search for n, replace with rr, :% s/n/rr/g: stackoverflow.com/a/71334/911945

– Anton Tarasenko
Dec 7 '18 at 15:36










4 Answers
4






active

oldest

votes


















43














Oddly enough, n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@).



Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension r (like the C language) to mean the same as ^M, but the developers chose to make n mean null when replacing text. This is inconsistent with its use in searches, which find a newline.



Further reading:




  • Search and replace (Vim wiki)

  • vim replace character to n





share|improve this answer
































    7














    Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).






    share|improve this answer























    • Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

      – mckenzm
      Dec 4 '15 at 20:29


















    5














    vim use n to represent a null character in memory, that how vim handle file contain null character (while vi can not).



    The use of n only match end of line in the buffer, not the newline in the string when using in expression.



    See :h NL-used-for-Null and :h CR-used-for-NL for more details.






    share|improve this answer






























      1














      Tested with Neovim (nvim) and Vim versions



      NVIM v0.3.0
      VIM - Vi IMproved 8.1


      Remove a newline:



      cell
      allele
      rs2981578
      fgfr2


      Here, n matches newlines (to insert a newline however, use r):



      :'<,'>s/n/|/g

      cell|allele|rs2981578|fgfr2


      Add a newline:



      Nope:



      cell|allele|rs2981578|fgfr2

      :'<,'>s/n/|/g

      cell^@allele^@rs2981578^@fgfr2


      ^@ is a diacritic mark for LF (line feed; see
      http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)



      Yes:



      cell|allele|rs2981578|fgfr2

      :'<,'>s/|/r/g

      cell
      allele
      rs2981578
      fgfr2


      To address the OP's question (how to replace one n with two, nn), just add another r:



      cell
      allele
      rs2981578
      fgfr2

      :'<,'>s/n/rr/g

      cell

      allele

      rs2981578

      fgfr2


      ... i.e. match the original newline (n), and replace it with two newlines/carriage returns (rr).






      share|improve this answer

























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247329%2fvim-how-to-replace-one-new-line-n-with-two-ns%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        43














        Oddly enough, n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@).



        Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension r (like the C language) to mean the same as ^M, but the developers chose to make n mean null when replacing text. This is inconsistent with its use in searches, which find a newline.



        Further reading:




        • Search and replace (Vim wiki)

        • vim replace character to n





        share|improve this answer





























          43














          Oddly enough, n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@).



          Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension r (like the C language) to mean the same as ^M, but the developers chose to make n mean null when replacing text. This is inconsistent with its use in searches, which find a newline.



          Further reading:




          • Search and replace (Vim wiki)

          • vim replace character to n





          share|improve this answer



























            43












            43








            43







            Oddly enough, n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@).



            Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension r (like the C language) to mean the same as ^M, but the developers chose to make n mean null when replacing text. This is inconsistent with its use in searches, which find a newline.



            Further reading:




            • Search and replace (Vim wiki)

            • vim replace character to n





            share|improve this answer















            Oddly enough, n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@).



            Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension r (like the C language) to mean the same as ^M, but the developers chose to make n mean null when replacing text. This is inconsistent with its use in searches, which find a newline.



            Further reading:




            • Search and replace (Vim wiki)

            • vim replace character to n






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:40









            Community

            1




            1










            answered Dec 4 '15 at 10:29









            Thomas DickeyThomas Dickey

            54.1k5106178




            54.1k5106178























                7














                Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).






                share|improve this answer























                • Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                  – mckenzm
                  Dec 4 '15 at 20:29















                7














                Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).






                share|improve this answer























                • Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                  – mckenzm
                  Dec 4 '15 at 20:29













                7












                7








                7







                Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).






                share|improve this answer













                Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 4 '15 at 12:10









                Murray JensenMurray Jensen

                1,349176




                1,349176












                • Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                  – mckenzm
                  Dec 4 '15 at 20:29

















                • Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                  – mckenzm
                  Dec 4 '15 at 20:29
















                Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                – mckenzm
                Dec 4 '15 at 20:29





                Indeed, the very experienced will know this for the ever required conversion from CRLF as an alternative to dropping out and using dos2unix or similar. The Ctrl-V usage is a day 1 vi lesson.

                – mckenzm
                Dec 4 '15 at 20:29











                5














                vim use n to represent a null character in memory, that how vim handle file contain null character (while vi can not).



                The use of n only match end of line in the buffer, not the newline in the string when using in expression.



                See :h NL-used-for-Null and :h CR-used-for-NL for more details.






                share|improve this answer



























                  5














                  vim use n to represent a null character in memory, that how vim handle file contain null character (while vi can not).



                  The use of n only match end of line in the buffer, not the newline in the string when using in expression.



                  See :h NL-used-for-Null and :h CR-used-for-NL for more details.






                  share|improve this answer

























                    5












                    5








                    5







                    vim use n to represent a null character in memory, that how vim handle file contain null character (while vi can not).



                    The use of n only match end of line in the buffer, not the newline in the string when using in expression.



                    See :h NL-used-for-Null and :h CR-used-for-NL for more details.






                    share|improve this answer













                    vim use n to represent a null character in memory, that how vim handle file contain null character (while vi can not).



                    The use of n only match end of line in the buffer, not the newline in the string when using in expression.



                    See :h NL-used-for-Null and :h CR-used-for-NL for more details.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 4 '15 at 10:58









                    cuonglmcuonglm

                    105k25210307




                    105k25210307





















                        1














                        Tested with Neovim (nvim) and Vim versions



                        NVIM v0.3.0
                        VIM - Vi IMproved 8.1


                        Remove a newline:



                        cell
                        allele
                        rs2981578
                        fgfr2


                        Here, n matches newlines (to insert a newline however, use r):



                        :'<,'>s/n/|/g

                        cell|allele|rs2981578|fgfr2


                        Add a newline:



                        Nope:



                        cell|allele|rs2981578|fgfr2

                        :'<,'>s/n/|/g

                        cell^@allele^@rs2981578^@fgfr2


                        ^@ is a diacritic mark for LF (line feed; see
                        http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)



                        Yes:



                        cell|allele|rs2981578|fgfr2

                        :'<,'>s/|/r/g

                        cell
                        allele
                        rs2981578
                        fgfr2


                        To address the OP's question (how to replace one n with two, nn), just add another r:



                        cell
                        allele
                        rs2981578
                        fgfr2

                        :'<,'>s/n/rr/g

                        cell

                        allele

                        rs2981578

                        fgfr2


                        ... i.e. match the original newline (n), and replace it with two newlines/carriage returns (rr).






                        share|improve this answer





























                          1














                          Tested with Neovim (nvim) and Vim versions



                          NVIM v0.3.0
                          VIM - Vi IMproved 8.1


                          Remove a newline:



                          cell
                          allele
                          rs2981578
                          fgfr2


                          Here, n matches newlines (to insert a newline however, use r):



                          :'<,'>s/n/|/g

                          cell|allele|rs2981578|fgfr2


                          Add a newline:



                          Nope:



                          cell|allele|rs2981578|fgfr2

                          :'<,'>s/n/|/g

                          cell^@allele^@rs2981578^@fgfr2


                          ^@ is a diacritic mark for LF (line feed; see
                          http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)



                          Yes:



                          cell|allele|rs2981578|fgfr2

                          :'<,'>s/|/r/g

                          cell
                          allele
                          rs2981578
                          fgfr2


                          To address the OP's question (how to replace one n with two, nn), just add another r:



                          cell
                          allele
                          rs2981578
                          fgfr2

                          :'<,'>s/n/rr/g

                          cell

                          allele

                          rs2981578

                          fgfr2


                          ... i.e. match the original newline (n), and replace it with two newlines/carriage returns (rr).






                          share|improve this answer



























                            1












                            1








                            1







                            Tested with Neovim (nvim) and Vim versions



                            NVIM v0.3.0
                            VIM - Vi IMproved 8.1


                            Remove a newline:



                            cell
                            allele
                            rs2981578
                            fgfr2


                            Here, n matches newlines (to insert a newline however, use r):



                            :'<,'>s/n/|/g

                            cell|allele|rs2981578|fgfr2


                            Add a newline:



                            Nope:



                            cell|allele|rs2981578|fgfr2

                            :'<,'>s/n/|/g

                            cell^@allele^@rs2981578^@fgfr2


                            ^@ is a diacritic mark for LF (line feed; see
                            http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)



                            Yes:



                            cell|allele|rs2981578|fgfr2

                            :'<,'>s/|/r/g

                            cell
                            allele
                            rs2981578
                            fgfr2


                            To address the OP's question (how to replace one n with two, nn), just add another r:



                            cell
                            allele
                            rs2981578
                            fgfr2

                            :'<,'>s/n/rr/g

                            cell

                            allele

                            rs2981578

                            fgfr2


                            ... i.e. match the original newline (n), and replace it with two newlines/carriage returns (rr).






                            share|improve this answer















                            Tested with Neovim (nvim) and Vim versions



                            NVIM v0.3.0
                            VIM - Vi IMproved 8.1


                            Remove a newline:



                            cell
                            allele
                            rs2981578
                            fgfr2


                            Here, n matches newlines (to insert a newline however, use r):



                            :'<,'>s/n/|/g

                            cell|allele|rs2981578|fgfr2


                            Add a newline:



                            Nope:



                            cell|allele|rs2981578|fgfr2

                            :'<,'>s/n/|/g

                            cell^@allele^@rs2981578^@fgfr2


                            ^@ is a diacritic mark for LF (line feed; see
                            http://vimhelp.appspot.com/digraph.txt.html#digraphs-default)



                            Yes:



                            cell|allele|rs2981578|fgfr2

                            :'<,'>s/|/r/g

                            cell
                            allele
                            rs2981578
                            fgfr2


                            To address the OP's question (how to replace one n with two, nn), just add another r:



                            cell
                            allele
                            rs2981578
                            fgfr2

                            :'<,'>s/n/rr/g

                            cell

                            allele

                            rs2981578

                            fgfr2


                            ... i.e. match the original newline (n), and replace it with two newlines/carriage returns (rr).







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 5 at 16:21

























                            answered Jun 14 '18 at 18:31









                            Victoria StuartVictoria Stuart

                            17114




                            17114



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247329%2fvim-how-to-replace-one-new-line-n-with-two-ns%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?