Is there a command to split lines in Vim?












9















In Vim, the J key joins two lines together. Is there a similar, built-in, key combination to split lines with a newline (at the cursor position, or similar)?



Alternatively, what would be the most robust way to define a key combination to do that (in normal mode, not insert mode)?










share|improve this question

























  • See also: stackoverflow.com/questions/624821/vim-split-line-command

    – dreftymac
    Jan 7 '17 at 15:36
















9















In Vim, the J key joins two lines together. Is there a similar, built-in, key combination to split lines with a newline (at the cursor position, or similar)?



Alternatively, what would be the most robust way to define a key combination to do that (in normal mode, not insert mode)?










share|improve this question

























  • See also: stackoverflow.com/questions/624821/vim-split-line-command

    – dreftymac
    Jan 7 '17 at 15:36














9












9








9


1






In Vim, the J key joins two lines together. Is there a similar, built-in, key combination to split lines with a newline (at the cursor position, or similar)?



Alternatively, what would be the most robust way to define a key combination to do that (in normal mode, not insert mode)?










share|improve this question
















In Vim, the J key joins two lines together. Is there a similar, built-in, key combination to split lines with a newline (at the cursor position, or similar)?



Alternatively, what would be the most robust way to define a key combination to do that (in normal mode, not insert mode)?







vim vi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 11 '17 at 11:31









Peter Mortensen

8,376166185




8,376166185










asked Jun 24 '13 at 15:12









Andrew FerrierAndrew Ferrier

88921333




88921333













  • See also: stackoverflow.com/questions/624821/vim-split-line-command

    – dreftymac
    Jan 7 '17 at 15:36



















  • See also: stackoverflow.com/questions/624821/vim-split-line-command

    – dreftymac
    Jan 7 '17 at 15:36

















See also: stackoverflow.com/questions/624821/vim-split-line-command

– dreftymac
Jan 7 '17 at 15:36





See also: stackoverflow.com/questions/624821/vim-split-line-command

– dreftymac
Jan 7 '17 at 15:36










5 Answers
5






active

oldest

votes


















10














No, there's no built-in command for that.



When I want to split on a <Space>, I do r<CR>.



--- EDIT ---



@keith-nicholas' comment reminded me about this question. FWIW I came up with an hopefully "universal" method in the mean time:



function! BreakHere()
s/^(s*)(.{-})(s*)(%#)(s*)(.*)/12r146
call histdel("/", -1)
endfunction

nnoremap <key> :<C-u>call BreakHere()<CR>





share|improve this answer





















  • 2





    Using r<CR> is simply clever! Thanks!

    – Marcelo
    Apr 8 '17 at 13:33











  • that's great! super annoying going into insert to insert lines

    – Keith Nicholas
    Jul 13 '17 at 4:06













  • I found s<CR> keeps indentation whereas r<CR> does not.

    – py4on
    Mar 1 at 11:41



















4














a Enter Esc to split to the right of the cursor, or i Enter Esc to split to the left.






share|improve this answer



















  • 1





    That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

    – Tonny
    Jun 24 '13 at 15:23



















1














You could define your own using map. To define z as the command for example:



:map z i<CTRL+m>





share|improve this answer































    1














    The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode):




    • In visual mode, it will split whatever is selected.

    • In normal mode, you follow gq with a motion.


    For example, gql will split one line to the currently set width. To set the width of the split lines to be different from your current setting, you can use



    :set textwidth=<n>


    Where n=number of characters you want in a line, e.g., 10, and change back to your normal width when you're done.



    Got this information from a Youtube video by Kholidfu that shows how to join and split lines in normal mode using a motion: Vim Tutorial - Join and Split Lines.






    share|improve this answer

































      0














      You can record a macro:



      in normal mode type "q+" to start the record.
      press "i", the macro you want to record.
      then press "q" again to stop recording.



      to use the macro go to normal mode and type "@+letter".



      in my case I used the "b" to use this macro:
      to record type in normal mode "qbiq"
      to use type in normal mode "@b"






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fsuperuser.com%2fquestions%2f611410%2fis-there-a-command-to-split-lines-in-vim%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        No, there's no built-in command for that.



        When I want to split on a <Space>, I do r<CR>.



        --- EDIT ---



        @keith-nicholas' comment reminded me about this question. FWIW I came up with an hopefully "universal" method in the mean time:



        function! BreakHere()
        s/^(s*)(.{-})(s*)(%#)(s*)(.*)/12r146
        call histdel("/", -1)
        endfunction

        nnoremap <key> :<C-u>call BreakHere()<CR>





        share|improve this answer





















        • 2





          Using r<CR> is simply clever! Thanks!

          – Marcelo
          Apr 8 '17 at 13:33











        • that's great! super annoying going into insert to insert lines

          – Keith Nicholas
          Jul 13 '17 at 4:06













        • I found s<CR> keeps indentation whereas r<CR> does not.

          – py4on
          Mar 1 at 11:41
















        10














        No, there's no built-in command for that.



        When I want to split on a <Space>, I do r<CR>.



        --- EDIT ---



        @keith-nicholas' comment reminded me about this question. FWIW I came up with an hopefully "universal" method in the mean time:



        function! BreakHere()
        s/^(s*)(.{-})(s*)(%#)(s*)(.*)/12r146
        call histdel("/", -1)
        endfunction

        nnoremap <key> :<C-u>call BreakHere()<CR>





        share|improve this answer





















        • 2





          Using r<CR> is simply clever! Thanks!

          – Marcelo
          Apr 8 '17 at 13:33











        • that's great! super annoying going into insert to insert lines

          – Keith Nicholas
          Jul 13 '17 at 4:06













        • I found s<CR> keeps indentation whereas r<CR> does not.

          – py4on
          Mar 1 at 11:41














        10












        10








        10







        No, there's no built-in command for that.



        When I want to split on a <Space>, I do r<CR>.



        --- EDIT ---



        @keith-nicholas' comment reminded me about this question. FWIW I came up with an hopefully "universal" method in the mean time:



        function! BreakHere()
        s/^(s*)(.{-})(s*)(%#)(s*)(.*)/12r146
        call histdel("/", -1)
        endfunction

        nnoremap <key> :<C-u>call BreakHere()<CR>





        share|improve this answer















        No, there's no built-in command for that.



        When I want to split on a <Space>, I do r<CR>.



        --- EDIT ---



        @keith-nicholas' comment reminded me about this question. FWIW I came up with an hopefully "universal" method in the mean time:



        function! BreakHere()
        s/^(s*)(.{-})(s*)(%#)(s*)(.*)/12r146
        call histdel("/", -1)
        endfunction

        nnoremap <key> :<C-u>call BreakHere()<CR>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 13 '17 at 11:23

























        answered Jun 24 '13 at 15:23









        romainlromainl

        18k23346




        18k23346








        • 2





          Using r<CR> is simply clever! Thanks!

          – Marcelo
          Apr 8 '17 at 13:33











        • that's great! super annoying going into insert to insert lines

          – Keith Nicholas
          Jul 13 '17 at 4:06













        • I found s<CR> keeps indentation whereas r<CR> does not.

          – py4on
          Mar 1 at 11:41














        • 2





          Using r<CR> is simply clever! Thanks!

          – Marcelo
          Apr 8 '17 at 13:33











        • that's great! super annoying going into insert to insert lines

          – Keith Nicholas
          Jul 13 '17 at 4:06













        • I found s<CR> keeps indentation whereas r<CR> does not.

          – py4on
          Mar 1 at 11:41








        2




        2





        Using r<CR> is simply clever! Thanks!

        – Marcelo
        Apr 8 '17 at 13:33





        Using r<CR> is simply clever! Thanks!

        – Marcelo
        Apr 8 '17 at 13:33













        that's great! super annoying going into insert to insert lines

        – Keith Nicholas
        Jul 13 '17 at 4:06







        that's great! super annoying going into insert to insert lines

        – Keith Nicholas
        Jul 13 '17 at 4:06















        I found s<CR> keeps indentation whereas r<CR> does not.

        – py4on
        Mar 1 at 11:41





        I found s<CR> keeps indentation whereas r<CR> does not.

        – py4on
        Mar 1 at 11:41













        4














        a Enter Esc to split to the right of the cursor, or i Enter Esc to split to the left.






        share|improve this answer



















        • 1





          That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

          – Tonny
          Jun 24 '13 at 15:23
















        4














        a Enter Esc to split to the right of the cursor, or i Enter Esc to split to the left.






        share|improve this answer



















        • 1





          That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

          – Tonny
          Jun 24 '13 at 15:23














        4












        4








        4







        a Enter Esc to split to the right of the cursor, or i Enter Esc to split to the left.






        share|improve this answer













        a Enter Esc to split to the right of the cursor, or i Enter Esc to split to the left.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 24 '13 at 15:17









        ScottScott

        15.9k113990




        15.9k113990








        • 1





          That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

          – Tonny
          Jun 24 '13 at 15:23














        • 1





          That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

          – Tonny
          Jun 24 '13 at 15:23








        1




        1





        That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

        – Tonny
        Jun 24 '13 at 15:23





        That is it. Just insert a single newline. Alternatively: use r+<return> to replace the character under the cursor with a newline. Would be nice to have a single letter command for it, but as far as I know there isn't one.

        – Tonny
        Jun 24 '13 at 15:23











        1














        You could define your own using map. To define z as the command for example:



        :map z i<CTRL+m>





        share|improve this answer




























          1














          You could define your own using map. To define z as the command for example:



          :map z i<CTRL+m>





          share|improve this answer


























            1












            1








            1







            You could define your own using map. To define z as the command for example:



            :map z i<CTRL+m>





            share|improve this answer













            You could define your own using map. To define z as the command for example:



            :map z i<CTRL+m>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 24 '13 at 15:23









            suspectussuspectus

            3,67161931




            3,67161931























                1














                The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode):




                • In visual mode, it will split whatever is selected.

                • In normal mode, you follow gq with a motion.


                For example, gql will split one line to the currently set width. To set the width of the split lines to be different from your current setting, you can use



                :set textwidth=<n>


                Where n=number of characters you want in a line, e.g., 10, and change back to your normal width when you're done.



                Got this information from a Youtube video by Kholidfu that shows how to join and split lines in normal mode using a motion: Vim Tutorial - Join and Split Lines.






                share|improve this answer






























                  1














                  The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode):




                  • In visual mode, it will split whatever is selected.

                  • In normal mode, you follow gq with a motion.


                  For example, gql will split one line to the currently set width. To set the width of the split lines to be different from your current setting, you can use



                  :set textwidth=<n>


                  Where n=number of characters you want in a line, e.g., 10, and change back to your normal width when you're done.



                  Got this information from a Youtube video by Kholidfu that shows how to join and split lines in normal mode using a motion: Vim Tutorial - Join and Split Lines.






                  share|improve this answer




























                    1












                    1








                    1







                    The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode):




                    • In visual mode, it will split whatever is selected.

                    • In normal mode, you follow gq with a motion.


                    For example, gql will split one line to the currently set width. To set the width of the split lines to be different from your current setting, you can use



                    :set textwidth=<n>


                    Where n=number of characters you want in a line, e.g., 10, and change back to your normal width when you're done.



                    Got this information from a Youtube video by Kholidfu that shows how to join and split lines in normal mode using a motion: Vim Tutorial - Join and Split Lines.






                    share|improve this answer















                    The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode):




                    • In visual mode, it will split whatever is selected.

                    • In normal mode, you follow gq with a motion.


                    For example, gql will split one line to the currently set width. To set the width of the split lines to be different from your current setting, you can use



                    :set textwidth=<n>


                    Where n=number of characters you want in a line, e.g., 10, and change back to your normal width when you're done.



                    Got this information from a Youtube video by Kholidfu that shows how to join and split lines in normal mode using a motion: Vim Tutorial - Join and Split Lines.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 13 '17 at 15:24









                    Greenonline

                    1,2743923




                    1,2743923










                    answered Jun 13 '17 at 14:12









                    vdicarlovdicarlo

                    111




                    111























                        0














                        You can record a macro:



                        in normal mode type "q+" to start the record.
                        press "i", the macro you want to record.
                        then press "q" again to stop recording.



                        to use the macro go to normal mode and type "@+letter".



                        in my case I used the "b" to use this macro:
                        to record type in normal mode "qbiq"
                        to use type in normal mode "@b"






                        share|improve this answer




























                          0














                          You can record a macro:



                          in normal mode type "q+" to start the record.
                          press "i", the macro you want to record.
                          then press "q" again to stop recording.



                          to use the macro go to normal mode and type "@+letter".



                          in my case I used the "b" to use this macro:
                          to record type in normal mode "qbiq"
                          to use type in normal mode "@b"






                          share|improve this answer


























                            0












                            0








                            0







                            You can record a macro:



                            in normal mode type "q+" to start the record.
                            press "i", the macro you want to record.
                            then press "q" again to stop recording.



                            to use the macro go to normal mode and type "@+letter".



                            in my case I used the "b" to use this macro:
                            to record type in normal mode "qbiq"
                            to use type in normal mode "@b"






                            share|improve this answer













                            You can record a macro:



                            in normal mode type "q+" to start the record.
                            press "i", the macro you want to record.
                            then press "q" again to stop recording.



                            to use the macro go to normal mode and type "@+letter".



                            in my case I used the "b" to use this macro:
                            to record type in normal mode "qbiq"
                            to use type in normal mode "@b"







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 7 at 11:14









                            luizhjluizhj

                            1




                            1






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • 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%2fsuperuser.com%2fquestions%2f611410%2fis-there-a-command-to-split-lines-in-vim%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

                                Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                Aardman Animations

                                Are they similar matrix