Converting multiple assignment statements to single comma separated assignmentAlign a block of code on the...

Why don't I get the correct limit of a sequence, regardless of how I arrange it (the sequence), while following the rules for solving limits?

Are there any instances of members of different Hogwarts houses coupling up and marrying each other?

Writing a worded mathematical expression

How to work with a technician hired with a grant who argues everything

How can I fix a framing mistake so I can drywall?

Renewed US passport, did not receive expired US passport

Is English tonal for some words, like "permit"?

Is the union of a chain of elementary embeddings elementary?

Exact Brexit date and consequences

How do you build a Dominant 7th chord?

Where does the expression "triple-A" comes from?

Contract Employer Keeps Asking for Small Things Without Pay

Why should I always enable compiler warnings?

How to read torque specs off this Nissan service diagram?

Will replacing a fake visa with a different fake visa cause me problems when applying for a legal study permit?

Is there a star over my head?

Using the pipe operator ("|") when executing system commands

Can I cast Sunbeam if both my hands are busy?

Why was "leaping into the river" a valid trial outcome to prove one's innocence?

A medieval fantasy adventurer lights a torch in a 100% pure oxygen room. What happens?

How to split a string by the third .(dot) delimiter

extract lines from bottom until regex match

Are Democrats more likely to believe Astrology is a science?

Writing a love interest for my hero



Converting multiple assignment statements to single comma separated assignment


Align a block of code on the basis of a single characterFigure out which plugin is responsible for a key bindingHow to test if Vim supports <C-g>U?How do I make key mappings for both OS X and Linux?ci" - change at the closest quotes (not on the same line)How to move cursor in expression insert mode mapping?Display human readable meta/alt-(key) mappingsMultiple <ESC> with a single <ESC>:put at the end of the current line?Mapping backspace to delete previous wordHow to expand selection to containing block?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















A vim newbie here.



Would like to know an efficient way of converting



self.mat = material
self.epsilon=epsilon
self.x = x


to



self.mat, self.epsilon, self.x = material, epsilon, x









share|improve this question







New contributor



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




























    1















    A vim newbie here.



    Would like to know an efficient way of converting



    self.mat = material
    self.epsilon=epsilon
    self.x = x


    to



    self.mat, self.epsilon, self.x = material, epsilon, x









    share|improve this question







    New contributor



    plasmon360 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


      2






      A vim newbie here.



      Would like to know an efficient way of converting



      self.mat = material
      self.epsilon=epsilon
      self.x = x


      to



      self.mat, self.epsilon, self.x = material, epsilon, x









      share|improve this question







      New contributor



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











      A vim newbie here.



      Would like to know an efficient way of converting



      self.mat = material
      self.epsilon=epsilon
      self.x = x


      to



      self.mat, self.epsilon, self.x = material, epsilon, x






      key-bindings






      share|improve this question







      New contributor



      plasmon360 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



      plasmon360 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



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








      asked 9 hours ago









      plasmon360plasmon360

      1062 bronze badges




      1062 bronze badges




      New contributor



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




      New contributor




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



























          2 Answers
          2






          active

          oldest

          votes


















          3
















          There might be plug-ins that do this particular transformation, or you might be able to use two fairly long :s/// commands to isolate each side.



          But personally I find that using Vim's Visual Block feature is the best way to handle column data, which is the main kind of transformation you have here.



          You can start by aligning the =s so you really have three columns (the variables to be assigned, the =s and the expressions.) I previously wrote an answer on how to do that using visual block mode.



          Once you have that, you can again use visual block mode to cut the right column and paste it into its own lines. Go to the start of material, then use CTRL-V to start visual block mode, then 2j to select the three lines and finally $ to select to the end. Use d or x to cut that text.



          Navigate to a blank line (for example, use o at the last line of the file to create a new bla m one and then <Esc> to go back to normal mode.) Then paste with P. You'll have your three expressions in different lines.



          Visually select the first two again: CTRL-V, j, $ and add commas. You can use A,<Esc> which will add commas to the end of every line in the visual block selection.



          Finally join them with 3J.



          With the variable names, first you'll need to clean up the whitespace at the end, together with the =, which you can do with 3:s/ *= *$//. At that point, you can use the same procedure to add commas to the end of the first few lines (except the last one) and join them together. (Also possible is 3:norm f D to jump to the first space and delete til the end of the line, or 3:norm f C, to add a comma already.)



          Here all you need to do is insert an = back to the line with the list of variables, and paste back the list of expressions to the end of that.



          All done!



          It's a bit of work, but it's a really flexible method that you can use in many similar situations, visual block mode is a tool in Vim I definitely recommend that everyone who uses Vim should really learn! It can be really useful for this kind of text transformation.






          share|improve this answer

































            2
















            I'm not sure this is what you want, but you could try these global commands:



            g/^self./,/^(self.)@!/-j
            g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=' = '.join(l, ', ')


            The first command should turn this block:



            self.mat = material
            self.epsilon=epsilon
            self.x = x


            into this line:



            self.mat = material self.epsilon=epsilon self.x = x


            The second command should turn the latter line into:



            self.mat, self.epsilon, self.x = material, epsilon, x




            Note that it doesn't work if the last line of the buffer starts with self..
            In that case, maybe you could add an empty line at the end, before running the commands.





            If this is a frequent task, and you don't want to remember these commands, you can wrap them inside a custom command.
            As an example:



            com! -bar -range=% AssignmentMulti2Single call s:assignment_multi2single(<line1>,<line2>)
            fu! s:assignment_multi2single(lnum1,lnum2) abort
            if getline('$') =~# '^self.' | $put='' | endif
            exe a:lnum1 . 'ka'
            exe a:lnum2 . 'kb'
            let range = "'a,'b"
            sil exe range . 'g/^self./,/^(self.)@!/-j'
            sil exe range . 'g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=" = ".join(l, ", ")'
            endfu


            You can use this AssignmentMulti2Single custom command on an arbitrary range, like :12,34 to target the lines from the address 12 to 34, or like '<,'> to target the last lines which were visually selected.





            I haven't tested the code a lot; only on this text file:



            self.mat = material
            self.epsilon=epsilon
            self.x = x

            some text

            self.one = abc
            self.two=def
            self.three = ghi

            some other text

            self.four = jkl
            self.five=mno
            self.six = pqr


            Which :AssignmentMulti2Single turns into:



            self.mat, self.epsilon, self.x = material, epsilon, x

            some text

            self.one, self.two, self.three = abc, def, ghi

            some other text

            self.four, self.five, self.six = jkl, mno, pqr


            enter image description here



            So there are probably special cases that it doesn't handle well.





            For more information, see:



            :h :g
            :h :j
            :h :s
            :h :s=
            :h :com
            :h :command-bar
            :h :command-range
            :h <line1>
            :h :put
            :h :k
            :h :silent
            :h :exe
            :h add()
            :h submatch()
            :h join()
            :h getline()
            :h =~#
            :h /^
            :h /$
            :h /(
            :h /@!
            :h s
            :h w





            share|improve this answer






























              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/4.0/"u003ecc by-sa 4.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
              });


              }
              });







              plasmon360 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%2f21169%2fconverting-multiple-assignment-statements-to-single-comma-separated-assignment%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
















              There might be plug-ins that do this particular transformation, or you might be able to use two fairly long :s/// commands to isolate each side.



              But personally I find that using Vim's Visual Block feature is the best way to handle column data, which is the main kind of transformation you have here.



              You can start by aligning the =s so you really have three columns (the variables to be assigned, the =s and the expressions.) I previously wrote an answer on how to do that using visual block mode.



              Once you have that, you can again use visual block mode to cut the right column and paste it into its own lines. Go to the start of material, then use CTRL-V to start visual block mode, then 2j to select the three lines and finally $ to select to the end. Use d or x to cut that text.



              Navigate to a blank line (for example, use o at the last line of the file to create a new bla m one and then <Esc> to go back to normal mode.) Then paste with P. You'll have your three expressions in different lines.



              Visually select the first two again: CTRL-V, j, $ and add commas. You can use A,<Esc> which will add commas to the end of every line in the visual block selection.



              Finally join them with 3J.



              With the variable names, first you'll need to clean up the whitespace at the end, together with the =, which you can do with 3:s/ *= *$//. At that point, you can use the same procedure to add commas to the end of the first few lines (except the last one) and join them together. (Also possible is 3:norm f D to jump to the first space and delete til the end of the line, or 3:norm f C, to add a comma already.)



              Here all you need to do is insert an = back to the line with the list of variables, and paste back the list of expressions to the end of that.



              All done!



              It's a bit of work, but it's a really flexible method that you can use in many similar situations, visual block mode is a tool in Vim I definitely recommend that everyone who uses Vim should really learn! It can be really useful for this kind of text transformation.






              share|improve this answer






























                3
















                There might be plug-ins that do this particular transformation, or you might be able to use two fairly long :s/// commands to isolate each side.



                But personally I find that using Vim's Visual Block feature is the best way to handle column data, which is the main kind of transformation you have here.



                You can start by aligning the =s so you really have three columns (the variables to be assigned, the =s and the expressions.) I previously wrote an answer on how to do that using visual block mode.



                Once you have that, you can again use visual block mode to cut the right column and paste it into its own lines. Go to the start of material, then use CTRL-V to start visual block mode, then 2j to select the three lines and finally $ to select to the end. Use d or x to cut that text.



                Navigate to a blank line (for example, use o at the last line of the file to create a new bla m one and then <Esc> to go back to normal mode.) Then paste with P. You'll have your three expressions in different lines.



                Visually select the first two again: CTRL-V, j, $ and add commas. You can use A,<Esc> which will add commas to the end of every line in the visual block selection.



                Finally join them with 3J.



                With the variable names, first you'll need to clean up the whitespace at the end, together with the =, which you can do with 3:s/ *= *$//. At that point, you can use the same procedure to add commas to the end of the first few lines (except the last one) and join them together. (Also possible is 3:norm f D to jump to the first space and delete til the end of the line, or 3:norm f C, to add a comma already.)



                Here all you need to do is insert an = back to the line with the list of variables, and paste back the list of expressions to the end of that.



                All done!



                It's a bit of work, but it's a really flexible method that you can use in many similar situations, visual block mode is a tool in Vim I definitely recommend that everyone who uses Vim should really learn! It can be really useful for this kind of text transformation.






                share|improve this answer




























                  3














                  3










                  3









                  There might be plug-ins that do this particular transformation, or you might be able to use two fairly long :s/// commands to isolate each side.



                  But personally I find that using Vim's Visual Block feature is the best way to handle column data, which is the main kind of transformation you have here.



                  You can start by aligning the =s so you really have three columns (the variables to be assigned, the =s and the expressions.) I previously wrote an answer on how to do that using visual block mode.



                  Once you have that, you can again use visual block mode to cut the right column and paste it into its own lines. Go to the start of material, then use CTRL-V to start visual block mode, then 2j to select the three lines and finally $ to select to the end. Use d or x to cut that text.



                  Navigate to a blank line (for example, use o at the last line of the file to create a new bla m one and then <Esc> to go back to normal mode.) Then paste with P. You'll have your three expressions in different lines.



                  Visually select the first two again: CTRL-V, j, $ and add commas. You can use A,<Esc> which will add commas to the end of every line in the visual block selection.



                  Finally join them with 3J.



                  With the variable names, first you'll need to clean up the whitespace at the end, together with the =, which you can do with 3:s/ *= *$//. At that point, you can use the same procedure to add commas to the end of the first few lines (except the last one) and join them together. (Also possible is 3:norm f D to jump to the first space and delete til the end of the line, or 3:norm f C, to add a comma already.)



                  Here all you need to do is insert an = back to the line with the list of variables, and paste back the list of expressions to the end of that.



                  All done!



                  It's a bit of work, but it's a really flexible method that you can use in many similar situations, visual block mode is a tool in Vim I definitely recommend that everyone who uses Vim should really learn! It can be really useful for this kind of text transformation.






                  share|improve this answer













                  There might be plug-ins that do this particular transformation, or you might be able to use two fairly long :s/// commands to isolate each side.



                  But personally I find that using Vim's Visual Block feature is the best way to handle column data, which is the main kind of transformation you have here.



                  You can start by aligning the =s so you really have three columns (the variables to be assigned, the =s and the expressions.) I previously wrote an answer on how to do that using visual block mode.



                  Once you have that, you can again use visual block mode to cut the right column and paste it into its own lines. Go to the start of material, then use CTRL-V to start visual block mode, then 2j to select the three lines and finally $ to select to the end. Use d or x to cut that text.



                  Navigate to a blank line (for example, use o at the last line of the file to create a new bla m one and then <Esc> to go back to normal mode.) Then paste with P. You'll have your three expressions in different lines.



                  Visually select the first two again: CTRL-V, j, $ and add commas. You can use A,<Esc> which will add commas to the end of every line in the visual block selection.



                  Finally join them with 3J.



                  With the variable names, first you'll need to clean up the whitespace at the end, together with the =, which you can do with 3:s/ *= *$//. At that point, you can use the same procedure to add commas to the end of the first few lines (except the last one) and join them together. (Also possible is 3:norm f D to jump to the first space and delete til the end of the line, or 3:norm f C, to add a comma already.)



                  Here all you need to do is insert an = back to the line with the list of variables, and paste back the list of expressions to the end of that.



                  All done!



                  It's a bit of work, but it's a really flexible method that you can use in many similar situations, visual block mode is a tool in Vim I definitely recommend that everyone who uses Vim should really learn! It can be really useful for this kind of text transformation.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 8 hours ago









                  filbrandenfilbranden

                  3,1175 silver badges19 bronze badges




                  3,1175 silver badges19 bronze badges




























                      2
















                      I'm not sure this is what you want, but you could try these global commands:



                      g/^self./,/^(self.)@!/-j
                      g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=' = '.join(l, ', ')


                      The first command should turn this block:



                      self.mat = material
                      self.epsilon=epsilon
                      self.x = x


                      into this line:



                      self.mat = material self.epsilon=epsilon self.x = x


                      The second command should turn the latter line into:



                      self.mat, self.epsilon, self.x = material, epsilon, x




                      Note that it doesn't work if the last line of the buffer starts with self..
                      In that case, maybe you could add an empty line at the end, before running the commands.





                      If this is a frequent task, and you don't want to remember these commands, you can wrap them inside a custom command.
                      As an example:



                      com! -bar -range=% AssignmentMulti2Single call s:assignment_multi2single(<line1>,<line2>)
                      fu! s:assignment_multi2single(lnum1,lnum2) abort
                      if getline('$') =~# '^self.' | $put='' | endif
                      exe a:lnum1 . 'ka'
                      exe a:lnum2 . 'kb'
                      let range = "'a,'b"
                      sil exe range . 'g/^self./,/^(self.)@!/-j'
                      sil exe range . 'g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=" = ".join(l, ", ")'
                      endfu


                      You can use this AssignmentMulti2Single custom command on an arbitrary range, like :12,34 to target the lines from the address 12 to 34, or like '<,'> to target the last lines which were visually selected.





                      I haven't tested the code a lot; only on this text file:



                      self.mat = material
                      self.epsilon=epsilon
                      self.x = x

                      some text

                      self.one = abc
                      self.two=def
                      self.three = ghi

                      some other text

                      self.four = jkl
                      self.five=mno
                      self.six = pqr


                      Which :AssignmentMulti2Single turns into:



                      self.mat, self.epsilon, self.x = material, epsilon, x

                      some text

                      self.one, self.two, self.three = abc, def, ghi

                      some other text

                      self.four, self.five, self.six = jkl, mno, pqr


                      enter image description here



                      So there are probably special cases that it doesn't handle well.





                      For more information, see:



                      :h :g
                      :h :j
                      :h :s
                      :h :s=
                      :h :com
                      :h :command-bar
                      :h :command-range
                      :h <line1>
                      :h :put
                      :h :k
                      :h :silent
                      :h :exe
                      :h add()
                      :h submatch()
                      :h join()
                      :h getline()
                      :h =~#
                      :h /^
                      :h /$
                      :h /(
                      :h /@!
                      :h s
                      :h w





                      share|improve this answer
































                        2
















                        I'm not sure this is what you want, but you could try these global commands:



                        g/^self./,/^(self.)@!/-j
                        g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=' = '.join(l, ', ')


                        The first command should turn this block:



                        self.mat = material
                        self.epsilon=epsilon
                        self.x = x


                        into this line:



                        self.mat = material self.epsilon=epsilon self.x = x


                        The second command should turn the latter line into:



                        self.mat, self.epsilon, self.x = material, epsilon, x




                        Note that it doesn't work if the last line of the buffer starts with self..
                        In that case, maybe you could add an empty line at the end, before running the commands.





                        If this is a frequent task, and you don't want to remember these commands, you can wrap them inside a custom command.
                        As an example:



                        com! -bar -range=% AssignmentMulti2Single call s:assignment_multi2single(<line1>,<line2>)
                        fu! s:assignment_multi2single(lnum1,lnum2) abort
                        if getline('$') =~# '^self.' | $put='' | endif
                        exe a:lnum1 . 'ka'
                        exe a:lnum2 . 'kb'
                        let range = "'a,'b"
                        sil exe range . 'g/^self./,/^(self.)@!/-j'
                        sil exe range . 'g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=" = ".join(l, ", ")'
                        endfu


                        You can use this AssignmentMulti2Single custom command on an arbitrary range, like :12,34 to target the lines from the address 12 to 34, or like '<,'> to target the last lines which were visually selected.





                        I haven't tested the code a lot; only on this text file:



                        self.mat = material
                        self.epsilon=epsilon
                        self.x = x

                        some text

                        self.one = abc
                        self.two=def
                        self.three = ghi

                        some other text

                        self.four = jkl
                        self.five=mno
                        self.six = pqr


                        Which :AssignmentMulti2Single turns into:



                        self.mat, self.epsilon, self.x = material, epsilon, x

                        some text

                        self.one, self.two, self.three = abc, def, ghi

                        some other text

                        self.four, self.five, self.six = jkl, mno, pqr


                        enter image description here



                        So there are probably special cases that it doesn't handle well.





                        For more information, see:



                        :h :g
                        :h :j
                        :h :s
                        :h :s=
                        :h :com
                        :h :command-bar
                        :h :command-range
                        :h <line1>
                        :h :put
                        :h :k
                        :h :silent
                        :h :exe
                        :h add()
                        :h submatch()
                        :h join()
                        :h getline()
                        :h =~#
                        :h /^
                        :h /$
                        :h /(
                        :h /@!
                        :h s
                        :h w





                        share|improve this answer






























                          2














                          2










                          2









                          I'm not sure this is what you want, but you could try these global commands:



                          g/^self./,/^(self.)@!/-j
                          g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=' = '.join(l, ', ')


                          The first command should turn this block:



                          self.mat = material
                          self.epsilon=epsilon
                          self.x = x


                          into this line:



                          self.mat = material self.epsilon=epsilon self.x = x


                          The second command should turn the latter line into:



                          self.mat, self.epsilon, self.x = material, epsilon, x




                          Note that it doesn't work if the last line of the buffer starts with self..
                          In that case, maybe you could add an empty line at the end, before running the commands.





                          If this is a frequent task, and you don't want to remember these commands, you can wrap them inside a custom command.
                          As an example:



                          com! -bar -range=% AssignmentMulti2Single call s:assignment_multi2single(<line1>,<line2>)
                          fu! s:assignment_multi2single(lnum1,lnum2) abort
                          if getline('$') =~# '^self.' | $put='' | endif
                          exe a:lnum1 . 'ka'
                          exe a:lnum2 . 'kb'
                          let range = "'a,'b"
                          sil exe range . 'g/^self./,/^(self.)@!/-j'
                          sil exe range . 'g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=" = ".join(l, ", ")'
                          endfu


                          You can use this AssignmentMulti2Single custom command on an arbitrary range, like :12,34 to target the lines from the address 12 to 34, or like '<,'> to target the last lines which were visually selected.





                          I haven't tested the code a lot; only on this text file:



                          self.mat = material
                          self.epsilon=epsilon
                          self.x = x

                          some text

                          self.one = abc
                          self.two=def
                          self.three = ghi

                          some other text

                          self.four = jkl
                          self.five=mno
                          self.six = pqr


                          Which :AssignmentMulti2Single turns into:



                          self.mat, self.epsilon, self.x = material, epsilon, x

                          some text

                          self.one, self.two, self.three = abc, def, ghi

                          some other text

                          self.four, self.five, self.six = jkl, mno, pqr


                          enter image description here



                          So there are probably special cases that it doesn't handle well.





                          For more information, see:



                          :h :g
                          :h :j
                          :h :s
                          :h :s=
                          :h :com
                          :h :command-bar
                          :h :command-range
                          :h <line1>
                          :h :put
                          :h :k
                          :h :silent
                          :h :exe
                          :h add()
                          :h submatch()
                          :h join()
                          :h getline()
                          :h =~#
                          :h /^
                          :h /$
                          :h /(
                          :h /@!
                          :h s
                          :h w





                          share|improve this answer















                          I'm not sure this is what you want, but you could try these global commands:



                          g/^self./,/^(self.)@!/-j
                          g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=' = '.join(l, ', ')


                          The first command should turn this block:



                          self.mat = material
                          self.epsilon=epsilon
                          self.x = x


                          into this line:



                          self.mat = material self.epsilon=epsilon self.x = x


                          The second command should turn the latter line into:



                          self.mat, self.epsilon, self.x = material, epsilon, x




                          Note that it doesn't work if the last line of the buffer starts with self..
                          In that case, maybe you could add an empty line at the end, before running the commands.





                          If this is a frequent task, and you don't want to remember these commands, you can wrap them inside a custom command.
                          As an example:



                          com! -bar -range=% AssignmentMulti2Single call s:assignment_multi2single(<line1>,<line2>)
                          fu! s:assignment_multi2single(lnum1,lnum2) abort
                          if getline('$') =~# '^self.' | $put='' | endif
                          exe a:lnum1 . 'ka'
                          exe a:lnum2 . 'kb'
                          let range = "'a,'b"
                          sil exe range . 'g/^self./,/^(self.)@!/-j'
                          sil exe range . 'g/^self./let l=[]|s/s*=s*(w*)/=add(l, submatch(1))[-1][-1]/g|s/s/, /g|s/$/=" = ".join(l, ", ")'
                          endfu


                          You can use this AssignmentMulti2Single custom command on an arbitrary range, like :12,34 to target the lines from the address 12 to 34, or like '<,'> to target the last lines which were visually selected.





                          I haven't tested the code a lot; only on this text file:



                          self.mat = material
                          self.epsilon=epsilon
                          self.x = x

                          some text

                          self.one = abc
                          self.two=def
                          self.three = ghi

                          some other text

                          self.four = jkl
                          self.five=mno
                          self.six = pqr


                          Which :AssignmentMulti2Single turns into:



                          self.mat, self.epsilon, self.x = material, epsilon, x

                          some text

                          self.one, self.two, self.three = abc, def, ghi

                          some other text

                          self.four, self.five, self.six = jkl, mno, pqr


                          enter image description here



                          So there are probably special cases that it doesn't handle well.





                          For more information, see:



                          :h :g
                          :h :j
                          :h :s
                          :h :s=
                          :h :com
                          :h :command-bar
                          :h :command-range
                          :h <line1>
                          :h :put
                          :h :k
                          :h :silent
                          :h :exe
                          :h add()
                          :h submatch()
                          :h join()
                          :h getline()
                          :h =~#
                          :h /^
                          :h /$
                          :h /(
                          :h /@!
                          :h s
                          :h w






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 7 hours ago

























                          answered 8 hours ago









                          user938271user938271

                          1,2621 gold badge3 silver badges9 bronze badges




                          1,2621 gold badge3 silver badges9 bronze badges


























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










                              draft saved

                              draft discarded

















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













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












                              plasmon360 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%2f21169%2fconverting-multiple-assignment-statements-to-single-comma-separated-assignment%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

                              Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                              Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                              Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...