94

Let's say I've got the following code

    <div class="footer">
      <div>Foo</div>
    </div>

How can I change .footer from a div element to a footer element?

That is, if I have the cursor in div I'm looking for a keyboard shortcut that selects the opening and closing tags of an element. I believe I've used emmet to do this before in Sublime, but I can't seem to find similar functionality in Code. (Ideally this would work in JSX files too...)

2
  • It's not a feature yet that I can find, and I haven't found an extension or editor feature to consistently emulate it (someone post if you find one). But there is a User Voice request we can up-vote.
    – Vimes
    Commented May 6, 2016 at 22:05
  • You can use Http snipet extension, installation and settings: stackoverflow.com/a/45279141/8187926
    – Rox
    Commented Jul 24, 2017 at 11:16

8 Answers 8

67

V1.41 is adding this functionality, see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_41.md#html-rename-tags

HTML rename tags

You can now use F2 to rename the opening/closing tag pairs in HTML.

F2 when the cursor is over one of the tags and you will get a little input box with the cursor to input the new tag name and the start/end tags will be replaced with whatever you type upon .

See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#html-mirror-cursor-off-by-default

demo of HTML rename tags

Also of interest might be the "mirror tags" functionality just added in v1.41 as well (https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_41.md#html-mirror-cursor):

mirror ccursors in tags demo

Clicking inside a tag will create another cursor in the matching start or end tag.

VS Code now adds a "mirror cursor" when you are editing HTML tags. This behavior is controlled by the setting html.mirrorCursorOnMatchingTag, which is on by default.

---------- v1.42 is changing the default status of the mirror cursor:

HTML Mirror Cursor off by default

We have made Mirror Cursor an opt-in feature. In the upcoming iteration, we'll continue to improve its implementation to make this feature more easily understandable and available to more languages. You can still use this feature by turning on html.mirrorCursorOnMatchingTag.


Thanks to JerryGoyal's answer below (I have upvoted it) - I have continued with this ongoing answer.

Because I have been tracking this for over half a year now, v1.44 has renamed this once again. From Synced Regions:

Synced Regions

We have improved the mirror cursor feature introduced last November with a new implementation called Synced Regions. Currently this feature is available for HTML and you can try it out by one of the following ways:

Running the command On Type Rename Symbol on an HTML tag (bound to Ctrl+Shift+F2 by default).

Turning on the editor.renameOnType setting and move the cursor to an HTML tag.

Synced Regions demo

The red regions are Synced Regions. As their name suggests, any change in one region will be synced to other regions. You can exit this mode by either moving your cursor out of the regions or pressing ESC. Additionally, typing or pasting any content leading with a whitespace in any region exits this mode.

We look forward to providing an API that could make this rename-on-type experience available to other languages such as JSX, XML, or even local variables in TypeScript.

As that last part notes, it works in html out of the box but other languages need to implement themselves. As of June 2020 by my testing it still does not work in jsx files on embedded html tags.

settings rename on type

You must enable this in your settings, it is off by default.

7
  • 1
    It looks that way - it doesn't work in .jsx files with embedded html tags.
    – Mark
    Commented Feb 13, 2020 at 19:59
  • @Luis The current work on modifying this functionality includes api links for other lsp's to implement synced regions/mirror cursors. github.com/microsoft/vscode/issues/88424#issuecomment-600840619
    – Mark
    Commented Mar 31, 2020 at 23:31
  • 1
    You can also access vscode's "rename symbol" from the command palette (and "rename symbol" can be mapped to any keyboard shortcut you want, of course)
    – clint
    Commented Jun 6, 2020 at 11:32
  • 1
    Unfortunately this doesn't seem to be working for me on Mac for my .vue files.
    – Ryan
    Commented Jan 4, 2021 at 15:11
  • F2 works fine, vscode 1.59.1, OS: kubuntu 21.04 , Linux Commented Aug 23, 2021 at 1:37
32

You can do this without an extension using Emmet Update Tag

  1. Place your cursor in the opening tag
  2. Press CTRL+SHIFT+P to open the command palette
  3. Search for "Emmet: Update Tag" by typing something such as "em up t", and/or find it in the list
  4. Press enter to select "Emmet: Update Tag"
  5. Enter the new tag
  6. Press enter

The opening and closing tag are updated to the new one.

3
  • @DrazenBjelovuk I've texted it in razor/cshtml and it may work in other file types if emmet is enabled for that type. See this answer. Commented Sep 20, 2018 at 23:02
  • Even if enabled, doesn't seem to work for all types: github.com/Microsoft/vscode/issues/41618 Commented Sep 20, 2018 at 23:06
  • 4
    Even if this works, this is very tedious... Should be more simple.
    – Shy Agam
    Commented Sep 22, 2019 at 11:34
25

Update Mar 2021:

No need for extension, this is now cooked into VSCode.

"editor.linkedEditing": true

Read more here: https://code.visualstudio.com/Docs/languages/html#_auto-update-tags

2
  • 2
    It's been deprecated, now it's called Linked Editing -> "editor.linkedEditing": true Commented Apr 11, 2021 at 5:00
  • 7
    for jsx, tsx files you need to use Auto Rename Tag ext.
    – GorvGoyl
    Commented Apr 11, 2021 at 16:45
22

CTRL + D on windows. As mentioned by @tataata, CMD + D on Mac OS. Not limited to tag renaming. Very useful.

4
  • 14
    That's definitely not a rename. That's just multiple selection. Which means that if you've got a div and inside it you've got a sentence for example "Some individuals are amazing" then you'll not rename the closing div but "div" from "individuals"... Also, if you've got multiple/nested elements which are the same, let say 3 nested div. You try to rename the first or the second one, it won't work.
    – maxime1992
    Commented Sep 6, 2018 at 14:28
  • @maxime1992 Actually, you can set it to match cap and/ or whole words. The whole words setting would more likely find the ending div as long as there is no "div" word in the sentence (ie "He used a div").
    – SILENT
    Commented Oct 11, 2018 at 13:49
  • 2
    This should be the answer.
    – pashute
    Commented Dec 6, 2018 at 9:25
  • 1
    CTRL-D is fine for basic use :)! Much appreciated.
    – MMacphail
    Commented Aug 3, 2019 at 15:13
10

You can use a key shortcut cmd + D (Mac OS) for adding to the selection the next matching element and then there is a possibility to edit open and closing tags simultaneously.

2
  • As mentioned above this post doesn't answers the question. With this the result would be <footer class="footer"><footer>Foo</footer></footer> or <footer class="footer"><footer>Foo</div></div>
    – n3ko
    Commented Jul 16, 2019 at 22:16
  • that doesnt answer the question
    – fdsafas
    Commented Apr 1, 2021 at 23:46
2

I'm using tag-rename. Press F2 on the tag and it renames the start and close tags.

https://marketplace.visualstudio.com/items?itemName=krizzdewizz.tag-rename

2

No need to write config codes, just enable it from settings page.

Enable link editing VS code

0

Quick and Simple Select tool works fine too but though it is not perfect. it has over 40k downloads https://marketplace.visualstudio.com/items?itemName=dbankier.vscode-quick-select

it allows you to select tools

  • Ctrl + K ' select everything between single quotes
  • Ctrl + K " select everything between double quotes
  • Ctrl + K ` select everything between backticks
  • Ctrl + K ( select everything inside the parenthesis
  • Ctrl + K ) select everything inside parenthesis and include them
  • Ctrl + K [ or ] select everything between square brackets and include them
  • Ctrl + K { or } select everything between curly braces and include them
  • Ctrl + K < or > select everything between angle brackets and include

Not the answer you're looking for? Browse other questions tagged or ask your own question.