How to do tag wrapping in VS code? - html

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

Embedded Emmet could do the trick:
Select text (optional)
Open command palette (usually Ctrl+Shift+P)
Execute Emmet: Wrap with Abbreviation
Enter a tag div (or an abbreviation .wrapper>p)
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.
Launch VS Code Quick Open (Ctrl+P)
paste ext install htmltagwrap and enter
select HTML
press Alt + W (Option + W for Mac).

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

Open Keyboard Shortcuts by typing ⌘ Command+k ⌘ Command+s or Code > Preferences > Keyboard Shortcuts
Type emmet wrap
Click the plus sign to the left of "Emmet: Wrap with Abbreviation"
Type ⌥ Option+w
Press Enter

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.

With VSCode 1.47+ you can simply use OPT-w for this.
Utilizing built-in functionality to trigger emmet, this is the easiest way:
Select your text/html.
Shift+Option+w
In the emmet window opened in the command palette, type in the tag or wrapping code you need.
Enter
Voila

Many commands are already attached to simple ctrl+[key], you can also do chorded keybinding like 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.)
I have my Emmet: Wrap with Abbreviation bound to ((ctrl) (w+a)).
In windows: File > Preferences > Keyboard Shortcuts ((ctrl) (k+s))> search for Wrap with Abbreviation > double-click > add your combonation.

I just installed htmltagwrap from extension marketplace and used ALT-W to wrap the tags (Windows Version).

There is a fast typing of the solution.
Open the command palette (usually Ctrl+Shift+P)
Preferences: Open Keyboard Shortcuts (JSON)
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/}~~"
},
}
You select any text and press ctrl+`
result:
~~YourText~~

Related

visual studio code - how to comment multiple lines for html (each line instead whole block)

When I comment html files, vscode wraps my selection and puts a single
tag around the whole selection.
How can I change it that instead it comments every single line individually? (like e.g. in webstorm)
Thanks!
Keyboard shortcut to comment multiple in Windows is 'Shift+Alt+A'.
I made an extension that will do what you want: Toggle Comments.
Using this keybinding:
{
"key": "ctrl+alt+/", // whatevefr keybinding you wish
"command": "toggle-comments.toggleLineComments",
"when": "editorTextFocus"
}
Use the keyboard shortcut:
ctrl + shift + /

Enable spectacle keybindings with sublime text 2 on OSX Mavericks

I'm using spectacle on Mavericks (OSX 10.9). The default keybinding is super + alt + left pushes your current window to the left side of the screen and super + alt + right pushes your current window to the right side of the screen. This works great with most of my applications, except from sublime text 2. I tried to change the default sublime text keybindings by removing the following lines:
{ "keys": ["super+alt+left"], "command": "prev_view" },
{ "keys": ["super+alt+right"], "command": "next_view" },
but I'm still unable to use spectacle appropriately to move my sublime editor around my screen.
Any tips? Thanks
Same problem. According to the official website, the default key binding could be modified by overwrite default ones in your own (User) config file via "Preference -> Key Biding" menu, and I tried nullify the view switching behavior with
[
{ "keys": ["super+alt+left"],"command":"noop"},
{ "keys": ["super+alt+right"],"command": "noop"}
]
This doesn't work because it basically ignored the key-combo press, rather than passing over the registered key press to other Applications (Spectacle in this case).
One potential solution is to change the Spectacle key binding, but I don't want to.

custom keybinding on find and Reg replace in sublime text?

Am thinking whether it's achievable to make shortcut key-bindings on user
sublime key-map, so that one-key-triggered "find reg expression and replace-all"
if can assign custom keybinding will save a lot of work.
For example can be applied to make below answer into shortcut:
Sublime Text 2: How to delete blank/empty lines
Thanks to Joyas's answer, Below is the usage that could achieve the above task.
1.install RegReplace plugin for sublime 2/3
2.configure user reg replace function
{
"replacements": {
"remove_blank_lines": {
"find" : "(^\n)",
"replace": "",
"greedy_replace": true
}
}
}
3.configure key bindings
{
"keys": ["ctrl+shift+y"],
"command": "reg_replace",
"args": {"replacements": ["remove_blank_lines"]}
}
go to font-end press shortcut and it works so good!
There is already a post here about custom shortcut in sublime text.
In order to change the default shortcut for find and replace, you just have to edit de the line
{ "keys": ["super+shift+f"], "command": "show_panel", "args":
{"panel": "find_in_files"} }
in you preferences.
Furthermore, if you want to create a keybinding in order to "find and replace" a specific sequence of character, it seems that there is the plugin RegReplace to do so.

How do I insert a snippet of text using a keyboard shortcut in Sublime Text?

How do I set a shortcut for inserting console.log() and wrapping it around my current selection in Sublime Text without installing any plugin or extra third-party stuff?
So I found the answer. Thought I'd add it here for others:
Go to Sublime Text 2 > Preferences > Key Bindings - User and add this JSON to the file:
[
{ "keys": ["alt+d"],
"command": "insert_snippet",
"args": {
"contents": "console.log(${1:}$SELECTION);${0}"
}
}
]
Inserts a console.log() at the current cursor position on pressing alt+d.
Reference: https://gist.github.com/harthur/2951063

How do I disable Emmet's CSS Abbreviations in Sublime Text 2

I really like how Emmet generates HTML based on 'CSS-like' strings but I don't want to use their CSS Abbreviations. Cause when I write a piece of css as follows:
li a|
And I press 'TAB', I want to get a Tab
li a |
But with Emmet I get the following when I press Tab
li |-webkit-appearance: none;
-moz-appearance: none;
appearance: none
How can I disable this functionality of Emmet?
In Emmet.sublime-settings you have to update disable_tab_abbreviations_for_scopes property: add source.css to disable Tab trigger completely in CSS.
Also, if you’re on ST2, you may want to take a look at disable_tab_abbreviations_for_regexp preference due to some bugs in ST2 scope matcher.
If you installed with package control, search for Package Control: Remove Package in the command palette. Otherwise you can just remove the Emmet directory.
If you wish to use a custom caption to access commands, create Default.sublime-commands in your User folder. Then insert something similar to the following.
[
{
"caption": "Package Control: Uninstall Package",
"command": "remove_package"
}
]
Of course, you can customize the command and caption as you see fit.
Or try like this: CTRL + SHIFT + P >> Remove Package >>
Select package to remove then press enter
Instead of pressing Tab, you can use Shift+Tab to insert a tab without triggering the tab trigger.
It’s documented in the Sublime Text documentation.
Preferences.sublime-settings - toggle on/off is there.