How to do tag wrapping in VS code?

后端 未结 7 1362
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 05:10

I would like to wrap my selected html within a tag in VS code. How do I do that?

相关标签:
7条回答
  • 2020-12-04 05:48
    1. Open Keyboard Shortcuts by typing ⌘ Command+k ⌘ Command+s or Code > Preferences > Keyboard Shortcuts
    2. Type emmet wrap
    3. Click the plus sign to the left of "Emmet: Wrap with Abbreviation"
    4. Type ⌥ Option+w
    5. Press Enter
    0 讨论(0)
  • 2020-12-04 05:49

    With VSCode 1.47+ you can simply use OPT-w for this.

    Utilizing built-in functionality to trigger emmet, this is the easiest way:

    1. Select your text/html.
    2. Option+w
    3. In the emmet window opened in the command palette, type in the tag or wrapping code you need.
    4. Enter
    5. Voila
    0 讨论(0)
  • 2020-12-04 05:53

    Embedded Emmet could do the trick:

    1. Select text (optional)
    2. Open command palette (usually Ctrl+Shift+P)
    3. Execute Emmet: Wrap with Abbreviation
    4. Enter a tag div (or an abbreviation .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
    0 讨论(0)
  • 2020-12-04 05:53

    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 Keymap Extensions (Preferences > Keymap Extensions [Cmd+K Cmd+M]) and add the following object:

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

    Which will bind the Emmet wrap command to Alt+W when text is selected

    (Sorry for OSX only instructions)

    0 讨论(0)
  • 2020-12-04 05:54

    A quick search on the VSCode marketplace: https://marketplace.visualstudio.com/items/bradgashler.htmltagwrap.

    1. Launch VS Code Quick Open (Ctrl+P)

    2. paste ext install htmltagwrap and enter

    3. select HTML

    4. press Alt + W (Option + W for Mac).

    0 讨论(0)
  • 2020-12-04 05:58

    imo there's a better answer for this using 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 E.g. like this:

    { 
        "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.

    It will insert tags around selected text, defaults to <b> tag & selects the tag so typing lets you change it.

    If you want to use a different default tag just change the b in the body property of the snippet.

    0 讨论(0)
提交回复
热议问题