83

In visual studio code when changing the start tag type (e.g., from <div> to <span>) I want the editor to automatically change the matching closing tag. How can I do that?

Théophile
  • 1,032
  • 10
  • 16
omer727
  • 7,117
  • 6
  • 26
  • 39
  • There is vscode extension that will solve your problem. – Abu Machi Aug 11 '18 at 20:55
  • 1
    Most probably this will help you https://stackoverflow.com/a/44982436/12069171 – Lint Mar 25 '20 at 22:36
  • 1
    For anyone stumbling across this thread from the first result of Google SERP, see Neil's answer - https://stackoverflow.com/a/70353463/8604951 since VSCode supports the functionality by default right now. No need to install another plugin & bloat your system lol. – Jarmos Jul 07 '22 at 10:32
  • The feature request _Mirror Cursor for JSX_ is [here](https://github.com/microsoft/TypeScript/issues/51832). – cachius Mar 15 '23 at 15:51

6 Answers6

91

Start VS Code, go to Plugins and search for Auto Rename Tag. Install, reload, done. This plugin: https://marketplace.visualstudio.com/items?itemName=formulahendry.auto-rename-tag

Vladislav Ladicky
  • 2,261
  • 1
  • 11
  • 13
64

Since VSCode 1.44, you can enable automatic updating of matching tags like this:

  1. Go to File menu, Preferences and Settings
  2. Search for editor.linkedEditing
  3. Enable "Linked Editing"

Screenshot of Linked Editing setting

Here is an example of how it works:

Example of linked editing

Anders Carstensen
  • 2,949
  • 23
  • 23
46

You can use the Rename Symbol command to do this.

  1. Select (or click to put the caret anywhere inside) the opening tag
  2. Hit F2
  3. Enter the new tag name and press Enter

Opening and closing tags will both be renamed.

Alternatively, you can press Shift + Enter at step 3 to preview changes and then Shift + Enter again to apply them.

clemlatz
  • 7,543
  • 4
  • 37
  • 51
  • 6
    This will rename the import statement of the component with `OldName as NewName`... :( Ctrl+D is probably the best option. – TwistedSim Jul 02 '21 at 18:09
  • One disadvantage of F2 is you cannot use it to set an empty tag. If you delete the name and press Enter, then it will cancel the operation. – VIBrunazo Nov 26 '21 at 12:39
  • 1
    In JSX, F2 just shows "Cannot be renamed" for me. The `Emmet: Update Tag` code action works though – phiresky Sep 22 '22 at 14:56
12

As mentioned by @TwistedSim, you can achieve this using Ctrl + D

Amy Doxy
  • 559
  • 6
  • 10
5

F2 works in (js|jsx).

If you happen to have IntelliJ Keybindings installed you can achieve the same with SHIFT + F6.

CNTRL+D didn't work for me at all. I'm on a Mac at the moment.

Neil Gaetano Lindberg
  • 2,488
  • 26
  • 23
2

From Anders Carstensen's answer

It works on .html files but does not work on React .jsx files.
The Auto Rename Tag extension from the accepted answer does work on both

Actually, VSCode 1.78 (Apr. 2023) will support tag rename for .jsx/.tsx files.
See issue 159534 and PR 179806, available in VSCode Insiders.
It requires TS 5.1+.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250