Converting multiple assignment statements to single comma separated assignmentAlign 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?

Why did they ever make smaller than full-frame sensors?

How seriously should I take a CBP interview where I was told I have a red flag and could only stay for 30 days?

Does the wording of the Wrathful Smite spell imply that there are other living beings that aren't considered "creatures"?

Writing a worded mathematical expression

What is a realistic time needed to get a properly trained army?

Do Milankovitch Cycles fully explain climate change?

Defining a function which returns a function pointer which also returns a function pointer without typedefs

Can I use ratchet straps to lift a dolly into a truck bed?

Kerning feedback on logo

Is it appropriate for a professor to require students to sign a non-disclosure agreement before being taught?

How to help my 2.5-year-old daughter take her medicine when she refuses to?

How can a resurrection system prevent the cheapening of death?

Georgian capital letter “Ⴒ” (“tar”) in pdfLaTeX

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

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

Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?

Do all humans have an identical nucleotide sequence for certain proteins, e.g haemoglobin?

What is the purpose of libraries like Pyomo and Google OR tools?

Where can I find vomiting people?

Job offer without any details but asking me to withdraw other applications - is it normal?

Creating a Master Image to roll out to 30 new Machines Licensing Issues

How could a imperial dynasty keep a loose collection of pirates, raiders, etc unified?

How to stabilise the bicycle seatpost and saddle when it is all the way up?

How do you build a Dominant 7th chord?



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

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單