How to change the pair of a changed HTML tag in Vim?
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
add a comment |
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
add a comment |
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
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
vim html
edited Jan 25 '17 at 11:45
kenorb
8,746372108
8,746372108
asked Nov 15 '14 at 21:53
hjkmlhjkml
453
453
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Here are few steps:
- Place your cursor on the first tag.
- Select outer html code by pressing: vat.
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
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
add a comment |
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.
add a comment |
The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div>
(c
hange s
urrounding t
ag 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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Here are few steps:
- Place your cursor on the first tag.
- Select outer html code by pressing: vat.
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
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
add a comment |
Here are few steps:
- Place your cursor on the first tag.
- Select outer html code by pressing: vat.
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
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
add a comment |
Here are few steps:
- Place your cursor on the first tag.
- Select outer html code by pressing: vat.
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
Here are few steps:
- Place your cursor on the first tag.
- Select outer html code by pressing: vat.
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
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 16 '14 at 20:02
Ingo KarkatIngo Karkat
8,73911932
8,73911932
add a comment |
add a comment |
The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div>
(c
hange s
urrounding t
ag 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.
add a comment |
The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div>
(c
hange s
urrounding t
ag 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.
add a comment |
The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div>
(c
hange s
urrounding t
ag 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.
The excellent surround.vim makes this simple — with the cursor anywhere within the tag you want to change, in normal mode, type cst<div>
(c
hange s
urrounding t
ag 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.
answered 14 mins ago
sj26sj26
1714
1714
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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