简体   繁体   English

如何在 VS 代码中进行标签包装?

[英]How to do tag wrapping in VS code?

I would like to wrap my selected html within a tag in VS code.我想将我选择的 html 包装在 VS 代码的标签中。 How do I do that?我怎么做?

Embedded Emmet could do the trick:嵌入式Emmet可以解决问题:

  1. Select text (optional)选择文本(可选)
  2. Open command palette (usually Ctrl + Shift + P )打开命令面板(通常是Ctrl + Shift + P
  3. Execute Emmet: Wrap with Abbreviation执行Emmet: Wrap with Abbreviation
  4. Enter a tag div (or an abbreviation .wrapper>p )输入标签div (或缩写.wrapper>p
  5. Hit Enter按回车键

Command can be assigned to a keybinding.命令可以分配给键绑定。

在此处输入图片说明


This thing even supports passing arguments:这东西甚至支持传递参数:

{
    "key": "ctrl+shift+9",
    "command": "editor.emmet.action.wrapWithAbbreviation",
    "when": "editorHasSelection",
    "args": {
        "abbreviation": "span",
    },
},

Use it like this:像这样使用它:

  • span.myCssClass
  • span#myCssId
  • b
  • b.myCssClass

A quick search on the VSCode marketplace: https://marketplace.visualstudio.com/items/bradgashler.htmltagwrap .在 VSCode 市场上快速搜索: https ://marketplace.visualstudio.com/items/bradgashler.htmltagwrap。

  1. Launch VS Code Quick Open ( Ctrl + P )启动 VS Code 快速打开 ( Ctrl + P )

  2. paste ext install htmltagwrap and enter粘贴ext install htmltagwrap并输入

  3. select HTML选择 HTML

  4. press Alt + W ( Option + W for Mac).Alt + W (对于 Mac 是Option + W )。

As I can't comment, I'll expand on Alex's fantastic answer.由于我无法发表评论,我将扩展亚历克斯的精彩答案。

If you want the Sublime-like experience with wrapping, open up the Keyboard Shortcuts ( command ⌘ / Ctrl + shift + P > Preferences: Open Keyboard Shortcuts (JSON)) and add the following object:如果您想要类似 Sublime 的包装体验,请打开键盘快捷键(命令 ⌘ / Ctrl + shift + P > Preferences: Open Keyboard Shortcuts (JSON))并添加以下对象:

{
    "key": "alt+w",
    "command": "editor.emmet.action.wrapWithAbbreviation",
    "when": "editorHasSelection && editorTextFocus"
}

Which will bind the Emmet wrap command to option ⌥ / Alt + W when text is selected.选择文本时,将Emmet Wrap命令将Emmet Wrap命令绑定到选项⌥ / alt + w

You can also use the UI to do this, open the Keyboard Shortcuts menu and search for "emmet wrap with abbreviation" to add the shortcut.您也可以使用 UI 来执行此操作,打开键盘快捷键菜单并搜索“emmet wrap with abbreviation”以添加快捷方式。 在此处输入图片说明

  1. Open Keyboard Shortcuts by typing ⌘ Command + k ⌘ Command + s or Code > Preferences > Keyboard Shortcuts通过输入⌘ Command + k ⌘ Command + sCode > Preferences > Keyboard Shortcuts打开键盘快捷Code > Preferences > Keyboard Shortcuts
  2. Type emmet wrap类型emmet wrap
  3. Click the plus sign to the left of "Emmet: Wrap with Abbreviation"单击“Emmet:Wrap with Abbreviation”左侧的加号
  4. Type ⌥ Option + w输入 ⌥ 选项+ w
  5. Press EnterEnter

imo there's a better answer for this using Snippets imo 有一个更好的答案,使用 Snippets

Create a snippet with a definition like this:创建一个定义如下的片段:

"name_of_your_snippet": {
    "scope": "javascript,html",
    "prefix": "name_of_your_snippet",
    "body": "<${0:b}>$TM_SELECTED_TEXT</${0:b}>"
}

Then bind it to a key in keybindings.json Eg like this:然后将其绑定到 keybindings.json 中的一个键,例如:

{ 
    "key": "alt+w",
    "command": "editor.action.insertSnippet",
    "args": { "name": "name_of_your_snippet" }
}

I think this should give you exactly the same result as htmltagwrap but without having to install an extension.我认为这应该为您提供与 htmltagwrap 完全相同的结果,但无需安装扩展程序。

It will insert tags around selected text, defaults to <b> tag & selects the tag so typing lets you change it.它将在所选文本周围插入标签,默认为<b>标签并选择标签,因此键入可以更改它。

If you want to use a different default tag just change the b in the body property of the snippet.如果您想使用不同的默认标签,只需更改代码段的body属性中的b

With VSCode 1.47+ you can simply use OPT-w for this.使用 VSCode 1.47+,您可以简单地使用 OPT-w。

Utilizing built-in functionality to trigger emmet, this is the easiest way:利用内置功能触发emmet,这是最简单的方法:

  1. Select your text/html.选择您的文本/html。
  2. Shift + Option + w Shift + Option + w
  3. In the emmet window opened in the command palette, type in the tag or wrapping code you need.在命令面板中打开的emmet窗口中,输入您需要的标记或包装代码。
  4. Enter输入
  5. Voila

Many commands are already attached to simple ctrl + [key] , you can also do chorded keybinding like ctrl a + b .许多命令已经附加到简单的ctrl + [key] ,您也可以像ctrl a + b那样进行和弦键绑定。

(In case this is your first time reading about chorded keybindings: They work by not letting go of the ctrl key and pressing a second key after the first.) (如果这是您第一次阅读和弦键绑定:它们的工作原理是不松开ctrl键并在第一个键之后按下第二个键。)

I have my Emmet: Wrap with Abbreviation bound to (( ctrl ) ( w + a )).我有我的Emmet: Wrap with Abbreviation绑定到 (( ctrl ) ( w + a ) )。

In windows: File > Preferences > Keyboard Shortcuts (( ctrl ) ( k + s ))> search for Wrap with Abbreviation > double-click > add your combonation.在 Windows 中:文件 > 首选项 > 键盘快捷键(( ctrl )( k + s ))> 搜索 Wrap with Abbreviation > 双击 > 添加您的组合。

我刚刚从扩展市场安装了 htmltagwrap 并使用 ALT-W 来包装标签(Windows 版本)。

There is a fast typing of the solution.可以快速输入解决方案。

  1. Open the command palette (usually Ctrl+Shift+P)打开命令面板(通常是 Ctrl+Shift+P)

  2. Preferences: Open Keyboard Shortcuts (JSON)首选项:打开键盘快捷键 (JSON)

  3. Add this snap code添加此快照代码

    { "key": "ctrl+`", "command": "editor.action.insertSnippet", "when": "editorTextFocus", "args": { "snippet": "~~${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}${TM_SELECTED_TEXT/^[\\t]*(.*)$/$1/}${TM_SELECTED_TEXT/^([\\t]*).*$/$1/}~~" }, }
  4. You select any text and press ctrl+`你 select 任何文字,然后按 ctrl+`

result:结果:

~~YourText~~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM