How to add a space or a string into the substitute expression?












1















I am trying Vim capabilities and stuck with this task - addition the incrementing number to the end of each line.



Testing lines:



text
text
text
text
text


This command works partially:



:let n=1 | g/text/s/$/=n/ | let n+=1


Result:



text1 
text2
text3
text4
text5


But I want to have space between the added numbers and the 'text'.



The adding of space ' ' before the =doesn't work, because the = should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n:



:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected


So, the questions:




  1. Is it possible to insert a string in the substitute expression?


Like this (the n is the variable):



s/$/string=n/
s/$/'string'=n/


or this:



s/$/='string'n/



  1. Can I use multiple variables in the substitute expression by separating them from each other like in the bash?


Example:



s/$/={var_1}{var_2}{var_3}/


3. Do you know more suitable/simple way for solving this task?










share|improve this question







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Does your buffer contain lines other than text? And do the text lines start on line 0?

    – DJMcMayhem
    1 hour ago











  • @DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

    – MiniMax
    34 mins ago


















1















I am trying Vim capabilities and stuck with this task - addition the incrementing number to the end of each line.



Testing lines:



text
text
text
text
text


This command works partially:



:let n=1 | g/text/s/$/=n/ | let n+=1


Result:



text1 
text2
text3
text4
text5


But I want to have space between the added numbers and the 'text'.



The adding of space ' ' before the =doesn't work, because the = should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n:



:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected


So, the questions:




  1. Is it possible to insert a string in the substitute expression?


Like this (the n is the variable):



s/$/string=n/
s/$/'string'=n/


or this:



s/$/='string'n/



  1. Can I use multiple variables in the substitute expression by separating them from each other like in the bash?


Example:



s/$/={var_1}{var_2}{var_3}/


3. Do you know more suitable/simple way for solving this task?










share|improve this question







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Does your buffer contain lines other than text? And do the text lines start on line 0?

    – DJMcMayhem
    1 hour ago











  • @DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

    – MiniMax
    34 mins ago
















1












1








1








I am trying Vim capabilities and stuck with this task - addition the incrementing number to the end of each line.



Testing lines:



text
text
text
text
text


This command works partially:



:let n=1 | g/text/s/$/=n/ | let n+=1


Result:



text1 
text2
text3
text4
text5


But I want to have space between the added numbers and the 'text'.



The adding of space ' ' before the =doesn't work, because the = should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n:



:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected


So, the questions:




  1. Is it possible to insert a string in the substitute expression?


Like this (the n is the variable):



s/$/string=n/
s/$/'string'=n/


or this:



s/$/='string'n/



  1. Can I use multiple variables in the substitute expression by separating them from each other like in the bash?


Example:



s/$/={var_1}{var_2}{var_3}/


3. Do you know more suitable/simple way for solving this task?










share|improve this question







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am trying Vim capabilities and stuck with this task - addition the incrementing number to the end of each line.



Testing lines:



text
text
text
text
text


This command works partially:



:let n=1 | g/text/s/$/=n/ | let n+=1


Result:



text1 
text2
text3
text4
text5


But I want to have space between the added numbers and the 'text'.



The adding of space ' ' before the =doesn't work, because the = should be in the beginning of substitute expression, else it is not parsed as an expression, but inserted literally - text =n:



:let n=1 | g/text/s/$/ =n/ | let n+=1 ### doesn't work as expected


So, the questions:




  1. Is it possible to insert a string in the substitute expression?


Like this (the n is the variable):



s/$/string=n/
s/$/'string'=n/


or this:



s/$/='string'n/



  1. Can I use multiple variables in the substitute expression by separating them from each other like in the bash?


Example:



s/$/={var_1}{var_2}{var_3}/


3. Do you know more suitable/simple way for solving this task?







substitute






share|improve this question







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









MiniMaxMiniMax

1163




1163




New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • Does your buffer contain lines other than text? And do the text lines start on line 0?

    – DJMcMayhem
    1 hour ago











  • @DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

    – MiniMax
    34 mins ago





















  • Does your buffer contain lines other than text? And do the text lines start on line 0?

    – DJMcMayhem
    1 hour ago











  • @DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

    – MiniMax
    34 mins ago



















Does your buffer contain lines other than text? And do the text lines start on line 0?

– DJMcMayhem
1 hour ago





Does your buffer contain lines other than text? And do the text lines start on line 0?

– DJMcMayhem
1 hour ago













@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

– MiniMax
34 mins ago







@DJMcMayhem No, the buffer can contains any characters, the Python source code, for example. Also, this action can be required in the any line number.

– MiniMax
34 mins ago












2 Answers
2






active

oldest

votes


















3














For your answer specifically, you could get around this by concatenating a space with the number, i.e.



:let n=1 | g/text/s/$/=" ".n/ | let n+=1




If you want to do this to every line, there are some much shorter ways to do this. For example:



:%s/$/=" ".line('.')


Or if you only want to number the lines matching "text", then either of these:



:%s/text/=submatch(0)." ".line('.')
:%s/textzs/=" ".line('.')


You could even do the entire thing in normal mode. For example, you could do this:



gg<C-v>G$A 0<esc>gvg<C-a>


Where <C-v> means ctrl-v and <C-a> means ctrl-a






share|improve this answer


























  • Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

    – MiniMax
    32 mins ago











  • The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

    – MiniMax
    23 mins ago













  • @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

    – DJMcMayhem
    18 mins ago











  • The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

    – MiniMax
    9 mins ago





















1














I found the solution:



:let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1


Result



text 1 
text 2
text 3
text 4
text 5





share|improve this answer








New contributor




MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "599"
    };
    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
    });


    }
    });






    MiniMax is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f18603%2fhow-to-add-a-space-or-a-string-into-the-substitute-expression%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    For your answer specifically, you could get around this by concatenating a space with the number, i.e.



    :let n=1 | g/text/s/$/=" ".n/ | let n+=1




    If you want to do this to every line, there are some much shorter ways to do this. For example:



    :%s/$/=" ".line('.')


    Or if you only want to number the lines matching "text", then either of these:



    :%s/text/=submatch(0)." ".line('.')
    :%s/textzs/=" ".line('.')


    You could even do the entire thing in normal mode. For example, you could do this:



    gg<C-v>G$A 0<esc>gvg<C-a>


    Where <C-v> means ctrl-v and <C-a> means ctrl-a






    share|improve this answer


























    • Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

      – MiniMax
      32 mins ago











    • The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

      – MiniMax
      23 mins ago













    • @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

      – DJMcMayhem
      18 mins ago











    • The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

      – MiniMax
      9 mins ago


















    3














    For your answer specifically, you could get around this by concatenating a space with the number, i.e.



    :let n=1 | g/text/s/$/=" ".n/ | let n+=1




    If you want to do this to every line, there are some much shorter ways to do this. For example:



    :%s/$/=" ".line('.')


    Or if you only want to number the lines matching "text", then either of these:



    :%s/text/=submatch(0)." ".line('.')
    :%s/textzs/=" ".line('.')


    You could even do the entire thing in normal mode. For example, you could do this:



    gg<C-v>G$A 0<esc>gvg<C-a>


    Where <C-v> means ctrl-v and <C-a> means ctrl-a






    share|improve this answer


























    • Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

      – MiniMax
      32 mins ago











    • The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

      – MiniMax
      23 mins ago













    • @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

      – DJMcMayhem
      18 mins ago











    • The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

      – MiniMax
      9 mins ago
















    3












    3








    3







    For your answer specifically, you could get around this by concatenating a space with the number, i.e.



    :let n=1 | g/text/s/$/=" ".n/ | let n+=1




    If you want to do this to every line, there are some much shorter ways to do this. For example:



    :%s/$/=" ".line('.')


    Or if you only want to number the lines matching "text", then either of these:



    :%s/text/=submatch(0)." ".line('.')
    :%s/textzs/=" ".line('.')


    You could even do the entire thing in normal mode. For example, you could do this:



    gg<C-v>G$A 0<esc>gvg<C-a>


    Where <C-v> means ctrl-v and <C-a> means ctrl-a






    share|improve this answer















    For your answer specifically, you could get around this by concatenating a space with the number, i.e.



    :let n=1 | g/text/s/$/=" ".n/ | let n+=1




    If you want to do this to every line, there are some much shorter ways to do this. For example:



    :%s/$/=" ".line('.')


    Or if you only want to number the lines matching "text", then either of these:



    :%s/text/=submatch(0)." ".line('.')
    :%s/textzs/=" ".line('.')


    You could even do the entire thing in normal mode. For example, you could do this:



    gg<C-v>G$A 0<esc>gvg<C-a>


    Where <C-v> means ctrl-v and <C-a> means ctrl-a







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 15 mins ago

























    answered 1 hour ago









    DJMcMayhemDJMcMayhem

    10.6k12861




    10.6k12861













    • Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

      – MiniMax
      32 mins ago











    • The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

      – MiniMax
      23 mins ago













    • @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

      – DJMcMayhem
      18 mins ago











    • The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

      – MiniMax
      9 mins ago





















    • Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

      – MiniMax
      32 mins ago











    • The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

      – MiniMax
      23 mins ago













    • @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

      – DJMcMayhem
      18 mins ago











    • The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

      – MiniMax
      9 mins ago



















    Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

    – MiniMax
    32 mins ago





    Thanks, it works. I was trying concatenation, but either without quotes, like s/$/= .num/ or with quotes, but without dot: s/$/=' 'n/ :). No, the line number can be random, as well as its content. The 'text' string were picked just for example.

    – MiniMax
    32 mins ago













    The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

    – MiniMax
    23 mins ago







    The second solution should be :%s/$/=" ".line('.'), otherwise it replaces the text part to the line number, that is not what I want. Also, it numbers all lines in the buffer, empty lines included.

    – MiniMax
    23 mins ago















    @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

    – DJMcMayhem
    18 mins ago





    @MiniMax Yes, good catch. I know that will do it to every line, but that was before I realized you only wanted specific lines. I'll leave that part up in case it can help someone else, but I'll edit it to be more clear about what it's doing.

    – DJMcMayhem
    18 mins ago













    The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

    – MiniMax
    9 mins ago







    The third one is beautiful. I knew about incrementing number by Ctrl-a, but didn't know g CTRL-A. It will be good to add description for gv and g CTRL-A to the answer for future. (The information about them located in the :h gv and :h Ctrl-a).

    – MiniMax
    9 mins ago













    1














    I found the solution:



    :let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1


    Result



    text 1 
    text 2
    text 3
    text 4
    text 5





    share|improve this answer








    New contributor




    MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      1














      I found the solution:



      :let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1


      Result



      text 1 
      text 2
      text 3
      text 4
      text 5





      share|improve this answer








      New contributor




      MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        1












        1








        1







        I found the solution:



        :let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1


        Result



        text 1 
        text 2
        text 3
        text 4
        text 5





        share|improve this answer








        New contributor




        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        I found the solution:



        :let n=1 | g/text/s/$/=printf(" %d", n)/ | let n+=1


        Result



        text 1 
        text 2
        text 3
        text 4
        text 5






        share|improve this answer








        New contributor




        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 1 hour ago









        MiniMaxMiniMax

        1163




        1163




        New contributor




        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        MiniMax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            MiniMax is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            MiniMax is a new contributor. Be nice, and check out our Code of Conduct.













            MiniMax is a new contributor. Be nice, and check out our Code of Conduct.












            MiniMax is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Vi and Vim 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%2fvi.stackexchange.com%2fquestions%2f18603%2fhow-to-add-a-space-or-a-string-into-the-substitute-expression%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

            Magento 2 controller redirect on button click in phtml file

            Polycentropodidae