How to change the pair of a changed HTML tag in Vim?












5















How can I configure Vim so that when I change one member of a matching pair of HTML tags, the other is automatically changed to match?



For example: given <span>content</span>, I want to change the first <span> to <div>, and then the second <span> automatically changes to </div>. Optimally this would happen upon returning from insert mode to normal mode, but it would be OK if I had to hit a special keybinding to make this happen.










share|improve this question





























    5















    How can I configure Vim so that when I change one member of a matching pair of HTML tags, the other is automatically changed to match?



    For example: given <span>content</span>, I want to change the first <span> to <div>, and then the second <span> automatically changes to </div>. Optimally this would happen upon returning from insert mode to normal mode, but it would be OK if I had to hit a special keybinding to make this happen.










    share|improve this question



























      5












      5








      5


      1






      How can I configure Vim so that when I change one member of a matching pair of HTML tags, the other is automatically changed to match?



      For example: given <span>content</span>, I want to change the first <span> to <div>, and then the second <span> automatically changes to </div>. Optimally this would happen upon returning from insert mode to normal mode, but it would be OK if I had to hit a special keybinding to make this happen.










      share|improve this question
















      How can I configure Vim so that when I change one member of a matching pair of HTML tags, the other is automatically changed to match?



      For example: given <span>content</span>, I want to change the first <span> to <div>, and then the second <span> automatically changes to </div>. Optimally this would happen upon returning from insert mode to normal mode, but it would be OK if I had to hit a special keybinding to make this happen.







      vim html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 '17 at 11:45









      kenorb

      8,746372108




      8,746372108










      asked Nov 15 '14 at 21:53









      hjkmlhjkml

      453




      453






















          3 Answers
          3






          active

          oldest

          votes


















          2














          Here are few steps:




          1. Place your cursor on the first tag.

          2. Select outer html code by pressing: vat.


          3. Press Esc to exit visual mode and type:



            :'<,'<s/span/div/
            :'>,'>s/span/div/


            Note that :'>,'>s/span/div/ won't work correctly if there are two occurrences of the same closing tag in the same line, as it'll always change the first one. To fix it, use: :'>,'>s/.*zsspan/div/ instead. See: How to change last occurrence of the string in the line?




          Related:





          • How to jump between matching HTML/XML tags? at Vim SE






          share|improve this answer


























          • Does entering visual mode do anything, once you've exited it?

            – Pureferret
            Jan 25 '17 at 11:43











          • I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

            – kenorb
            Jan 25 '17 at 11:46











          • When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

            – Pureferret
            Jan 25 '17 at 13:10











          • @Pureferret The ranges from the 3. step should use the last selected region.

            – kenorb
            Jan 25 '17 at 13:29











          • This is a terrible thing! It must change a closing tag on the fly during editting matching tag

            – Alex Shwarc
            Sep 15 '17 at 17:55



















          0














          With the SwapIt - Extensible keyword swapper, you can define tag groups that can then be toggled via <C-a> / <C-x>. For example, in ~/.vim/ftplugin/html_swapit.vim:



          SwapList layout p div span




          If you have both start and end tag on the same line (and there's only one of that type), you can edit both via my ChangeGlobally plugin.





          There are also several "multicursor" plugins, which allow you to mark certain words and edit them all at once.






          share|improve this answer































            0














            The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div> (change surrounding tag to <div>). The tag name will be changed to "div" in both the opening and closing tag, and you'll be back in normal mode.






            share|improve this answer























              Your Answer








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

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

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


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f168169%2fhow-to-change-the-pair-of-a-changed-html-tag-in-vim%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Here are few steps:




              1. Place your cursor on the first tag.

              2. Select outer html code by pressing: vat.


              3. Press Esc to exit visual mode and type:



                :'<,'<s/span/div/
                :'>,'>s/span/div/


                Note that :'>,'>s/span/div/ won't work correctly if there are two occurrences of the same closing tag in the same line, as it'll always change the first one. To fix it, use: :'>,'>s/.*zsspan/div/ instead. See: How to change last occurrence of the string in the line?




              Related:





              • How to jump between matching HTML/XML tags? at Vim SE






              share|improve this answer


























              • Does entering visual mode do anything, once you've exited it?

                – Pureferret
                Jan 25 '17 at 11:43











              • I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

                – kenorb
                Jan 25 '17 at 11:46











              • When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

                – Pureferret
                Jan 25 '17 at 13:10











              • @Pureferret The ranges from the 3. step should use the last selected region.

                – kenorb
                Jan 25 '17 at 13:29











              • This is a terrible thing! It must change a closing tag on the fly during editting matching tag

                – Alex Shwarc
                Sep 15 '17 at 17:55
















              2














              Here are few steps:




              1. Place your cursor on the first tag.

              2. Select outer html code by pressing: vat.


              3. Press Esc to exit visual mode and type:



                :'<,'<s/span/div/
                :'>,'>s/span/div/


                Note that :'>,'>s/span/div/ won't work correctly if there are two occurrences of the same closing tag in the same line, as it'll always change the first one. To fix it, use: :'>,'>s/.*zsspan/div/ instead. See: How to change last occurrence of the string in the line?




              Related:





              • How to jump between matching HTML/XML tags? at Vim SE






              share|improve this answer


























              • Does entering visual mode do anything, once you've exited it?

                – Pureferret
                Jan 25 '17 at 11:43











              • I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

                – kenorb
                Jan 25 '17 at 11:46











              • When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

                – Pureferret
                Jan 25 '17 at 13:10











              • @Pureferret The ranges from the 3. step should use the last selected region.

                – kenorb
                Jan 25 '17 at 13:29











              • This is a terrible thing! It must change a closing tag on the fly during editting matching tag

                – Alex Shwarc
                Sep 15 '17 at 17:55














              2












              2








              2







              Here are few steps:




              1. Place your cursor on the first tag.

              2. Select outer html code by pressing: vat.


              3. Press Esc to exit visual mode and type:



                :'<,'<s/span/div/
                :'>,'>s/span/div/


                Note that :'>,'>s/span/div/ won't work correctly if there are two occurrences of the same closing tag in the same line, as it'll always change the first one. To fix it, use: :'>,'>s/.*zsspan/div/ instead. See: How to change last occurrence of the string in the line?




              Related:





              • How to jump between matching HTML/XML tags? at Vim SE






              share|improve this answer















              Here are few steps:




              1. Place your cursor on the first tag.

              2. Select outer html code by pressing: vat.


              3. Press Esc to exit visual mode and type:



                :'<,'<s/span/div/
                :'>,'>s/span/div/


                Note that :'>,'>s/span/div/ won't work correctly if there are two occurrences of the same closing tag in the same line, as it'll always change the first one. To fix it, use: :'>,'>s/.*zsspan/div/ instead. See: How to change last occurrence of the string in the line?




              Related:





              • How to jump between matching HTML/XML tags? at Vim SE







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 13 '17 at 12:51









              Community

              1




              1










              answered Feb 22 '15 at 18:15









              kenorbkenorb

              8,746372108




              8,746372108













              • Does entering visual mode do anything, once you've exited it?

                – Pureferret
                Jan 25 '17 at 11:43











              • I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

                – kenorb
                Jan 25 '17 at 11:46











              • When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

                – Pureferret
                Jan 25 '17 at 13:10











              • @Pureferret The ranges from the 3. step should use the last selected region.

                – kenorb
                Jan 25 '17 at 13:29











              • This is a terrible thing! It must change a closing tag on the fly during editting matching tag

                – Alex Shwarc
                Sep 15 '17 at 17:55



















              • Does entering visual mode do anything, once you've exited it?

                – Pureferret
                Jan 25 '17 at 11:43











              • I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

                – kenorb
                Jan 25 '17 at 11:46











              • When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

                – Pureferret
                Jan 25 '17 at 13:10











              • @Pureferret The ranges from the 3. step should use the last selected region.

                – kenorb
                Jan 25 '17 at 13:29











              • This is a terrible thing! It must change a closing tag on the fly during editting matching tag

                – Alex Shwarc
                Sep 15 '17 at 17:55

















              Does entering visual mode do anything, once you've exited it?

              – Pureferret
              Jan 25 '17 at 11:43





              Does entering visual mode do anything, once you've exited it?

              – Pureferret
              Jan 25 '17 at 11:43













              I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

              – kenorb
              Jan 25 '17 at 11:46





              I'm not sure if I get the question. In the visual mode you're selecting the outer html for the given HTML tags, so substitution will only affect the selected region.

              – kenorb
              Jan 25 '17 at 11:46













              When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

              – Pureferret
              Jan 25 '17 at 13:10





              When I tried this, it looked like pressing <kbd>Esc</kbd> exited visual mode entirely, so that the substitution would have effected any other region; nothing remained highlighted after pressing <kbd>Esc</kbd> .

              – Pureferret
              Jan 25 '17 at 13:10













              @Pureferret The ranges from the 3. step should use the last selected region.

              – kenorb
              Jan 25 '17 at 13:29





              @Pureferret The ranges from the 3. step should use the last selected region.

              – kenorb
              Jan 25 '17 at 13:29













              This is a terrible thing! It must change a closing tag on the fly during editting matching tag

              – Alex Shwarc
              Sep 15 '17 at 17:55





              This is a terrible thing! It must change a closing tag on the fly during editting matching tag

              – Alex Shwarc
              Sep 15 '17 at 17:55













              0














              With the SwapIt - Extensible keyword swapper, you can define tag groups that can then be toggled via <C-a> / <C-x>. For example, in ~/.vim/ftplugin/html_swapit.vim:



              SwapList layout p div span




              If you have both start and end tag on the same line (and there's only one of that type), you can edit both via my ChangeGlobally plugin.





              There are also several "multicursor" plugins, which allow you to mark certain words and edit them all at once.






              share|improve this answer




























                0














                With the SwapIt - Extensible keyword swapper, you can define tag groups that can then be toggled via <C-a> / <C-x>. For example, in ~/.vim/ftplugin/html_swapit.vim:



                SwapList layout p div span




                If you have both start and end tag on the same line (and there's only one of that type), you can edit both via my ChangeGlobally plugin.





                There are also several "multicursor" plugins, which allow you to mark certain words and edit them all at once.






                share|improve this answer


























                  0












                  0








                  0







                  With the SwapIt - Extensible keyword swapper, you can define tag groups that can then be toggled via <C-a> / <C-x>. For example, in ~/.vim/ftplugin/html_swapit.vim:



                  SwapList layout p div span




                  If you have both start and end tag on the same line (and there's only one of that type), you can edit both via my ChangeGlobally plugin.





                  There are also several "multicursor" plugins, which allow you to mark certain words and edit them all at once.






                  share|improve this answer













                  With the SwapIt - Extensible keyword swapper, you can define tag groups that can then be toggled via <C-a> / <C-x>. For example, in ~/.vim/ftplugin/html_swapit.vim:



                  SwapList layout p div span




                  If you have both start and end tag on the same line (and there's only one of that type), you can edit both via my ChangeGlobally plugin.





                  There are also several "multicursor" plugins, which allow you to mark certain words and edit them all at once.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '14 at 20:02









                  Ingo KarkatIngo Karkat

                  8,73911932




                  8,73911932























                      0














                      The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div> (change surrounding tag to <div>). The tag name will be changed to "div" in both the opening and closing tag, and you'll be back in normal mode.






                      share|improve this answer




























                        0














                        The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div> (change surrounding tag to <div>). The tag name will be changed to "div" in both the opening and closing tag, and you'll be back in normal mode.






                        share|improve this answer


























                          0












                          0








                          0







                          The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div> (change surrounding tag to <div>). The tag name will be changed to "div" in both the opening and closing tag, and you'll be back in normal mode.






                          share|improve this answer













                          The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div> (change surrounding tag to <div>). The tag name will be changed to "div" in both the opening and closing tag, and you'll be back in normal mode.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 14 mins ago









                          sj26sj26

                          1714




                          1714






























                              draft saved

                              draft discarded




















































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


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

                              But avoid



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

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


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




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f168169%2fhow-to-change-the-pair-of-a-changed-html-tag-in-vim%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              濃尾地震

                              How to rewrite equation of hyperbola in standard form

                              No ethernet ip address in my vocore2