Emmet How To Wrap Usig Multiple Tags - visual-studio-code

I'm trying to wrap bunch of data with following tags.
For an example:
link1
link2
link3
link4
link5
I want each one of them to be wrapped with following tags.
<url>
<loc>link1</loc>
<lastmod>2020-01-16T22:59:45+00:00</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>link2</loc>
<lastmod>2020-01-16T22:59:45+00:00</lastmod>
<priority>0.80</priority>
</url>
....
I want to know if this is possible to do using Emmet code. Any help would be appreciated.

There are two things you should use from Emmet syntax:
Implicit repeater: mark element with * (without number) to Emmet to repeat element as many as lines you’re wrapping. For example, ul>li*
Output placeholder: tell Emmet where to put content you are wrapping with $#. You can use it in text (li{Put here: $#}) and/or in attributes (li[title=$#]).
So, in the end your wrapping abbreviation will look like this:
url*>loc{$#}+lastmod{2020-01-16T22:59:45+00:00}+priority{0.8}
Note that, for some reason, in VSCode you should use Emmet: Wrap Individual Lines with Abbreviation command to wrap multiple lines while in other editors the default Wrap With Abbreviation should work.
Read more about abbreviation syntax: https://docs.emmet.io/abbreviations/syntax/

In PHPStorm, I'd suggest defining a live template for that:
<url>
<loc>$SELECTION$</loc>
<lastmod>$date$</lastmod>
<priority>0.80</priority>
</url>
where $date$ has date("yyyy-MM-dd'T'HH:mm:ss.SSSZ") used as Expression:
Now enable column selection mode (Edit | Column Selection Mode), select the lines you'd like to surround with tags, choose Code > Surround With Live Template...

Another alternative is to use regular snippets. This is for vscode:
"link snippet": {
"prefix": "link",
"body": [
"<url>"
"<loc>$TM_SELECTED_TEXT</loc>",
"<lastmod>2020-01-16T22:59:45+00:00</lastmod>", // if date is fixed ahead of time
// use below if date is dynamic at creation time
"<lastmod>${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}T${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}+00:00</lastmod>"
"<priority>0.80</priority>",
"</url>",
""
],
"description": "Wrap link with url, etc."
},
Then, because you will need to chain 3 commands together to make this easy, use a macro extension like multi-command. Pu this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.expandLink",
"sequence": [
"editor.action.insertCursorAtEndOfEachLineSelected",
"cursorHomeSelect",
{
"command": "editor.action.insertSnippet",
"args": {
"name": "link snippet",
}
},
]
}
]
That will trigger the snippet after it selects each of your lines separately. To trigger the macro itself you need a keybinding (in keybindings.json):
{
"key": "shift+alt+l",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.expandLink" },
},
A fair amount of setup, but then it is just the one keybinding to trigger it all. Demo:

Related

Setting character $ to wrap text as parentheses in vscode

Suppose following code exists.
sample text
When user double click text, then press { or (, it just wraps the text while keeping it.
sample {text}
sample (text)
But I don't know how to apply this rule to $ in VS Code Settings.
What I expect is
sample $text$
Which setting in VS Code is related to this feature?
Edit> Auto Surround
is the setting in vscode. But it only applies to quotes and brackets like (), {}, <> and [] (and possibly some other language-defined cases). You cannot change that setting to include another character like $ unfortunately.
Here is a keybinding you might try (in keybindings.json):
{
"key": "alt+4", // or whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
// "snippet": "\\$$TM_SELECTED_TEXT\\$"
// to have the text still selected after the $'s are inserted, use this
"snippet": "\\$${1:$TM_SELECTED_TEXT}\\$"
},
"when": "editorTextFocus && editorHasSelection"
},
So that any selected text will be wrapped by a $ when you select it and alt+4 (where the $ is on an English keyboard). If you do that operation a lot it might be worth it.
If you use this line instead in the snippet above:
"snippet": "$1$TM_SELECTED_TEXT$1" // or
"snippet": "$1${2:$TM_SELECTED_TEXT}$1"
then more generically select text to surround, trigger that keybinding and type whichever and how many characters you want to wrap the selection.

Regex in VSCode: case conversion in replace, modifiers \l, \L, \U, \u not working

I'm trying to replace a structure like this:
testVars.bread.componentFlour
testVars.bread.componentWater
to something like this:
testVars.dough.flour
testVars.dough.water
this happens with multiple variable names; I want to remove the component and have the first letter converted to lowercase to match CamelCase.
What I tried doing was matching testVars.bread.component(.) replacing it with testVars.dough.\l$1.
According to regex documentation, that should convert my match to lowercase. However, VSCode wants to insert \l as text.
How do I get VSCode to convert my match to lowercase?
EDIT: To clarify, this is strictly for VSCode's implementation, not a regex question itself. Matching this group in notepad++ and replacing with testVars.dough.\l\1 does exactly what I want it to do.
NEW ANSWER: !! Those case modifiers like \l and \u are being added to the find/replace functionality in vscode (both in find within one file and search across multiple files [v1.49] - see https://github.com/microsoft/vscode/pull/105101) (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_47.md#case-changing-in-regex-replace) - will be in v1.47 (find) and v1.49 (search across files).
So that your original thought to
Find : testVars.bread.component(.)
Replace : testVars.dough.\l$1
is now working.
For more on how the case modifiers work in vscode, see https://stackoverflow.com/a/62270300/836330
OLD ANSWER:
While you cannot replace with a lowercase identifier in vscode, you still have some of options.
Use a regex like (?<=testVars\.).*\.component(.*) so that testVars. is not captured - because you do not want to change its case.
Ctrl+Shift+L to select all your matches (same aseditor.action.selectHighlights).
Ctrl+Shift+P, type lowerand trigger that command (or make a keybinding for this unbound command).
Trigger your replaceAll
To speed that up you have two options. (1) Make a macro that would run steps 2, 3 and 4. Or (2) make a snippet that will transform your selections - effectively running steps 3 and 4 at once.
Snippet in your keybindings.json (not in a snippets file):
{
"key": "alt+m", // choose some keybinding
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
},
},
and then Ctrl+Shift+L to select all, and alt+m to trigger the above insertSnippet.
Macro approach:
Using some macro extension, here multi-command, put this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.findReplaceLowercase",
"sequence": [
"editor.action.selectHighlights", {
"command": "editor.action.insertSnippet",
"args": {
"name": "replace and lowercase",
}
},
]
}
]
and a snippet, in one of your snippets files:
"replace and lowercase": { // this "label" is used in the macro
"prefix": "for",
"body": [
"${TM_SELECTED_TEXT/.*\\.component(.*)/dough.${1:/downcase}/}"
// "${TM_SELECTED_TEXT/(.*)/${1:/downcase}/}" // to downcase all selected text
],
"description": "replace selected text"
},
and a keybinding to trigger the macro:
{
"key": "alt+m", // choose some keybinding
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.findReplaceLowercase" },
}
In the demo I copy the find regex into the find widget (or just write it there, it doesn't matter how it gets there or where focus is) and then hit alt+m (the macro keybinding) and that is it.
Obviously, this looks like a lot of work but you could keep reusing the macro and snippet, transforming to the replace result you would like the next time. And there you can use /downcase, /upcase, /capitalize and /pascalcase all you want which you can't use in the replace field of the find/search widgets.

Keyboard shortcut in VSCode for Markdown links?

from other text editors I'm used to adding Markdown links by
selecting the word I want to be linked,
pressing cmd-K on my Mac's / iPad Pro's keyboard, which puts square brackets around the marked word, appends a pair of normal parenthesis () and places the cursor right in beetween those two parenthesis so that I can
just paste the URL I have in my clipboard into the right place by pressing cmd-V.
So, select -> cmd-K -> cmd-V is a nice and short sequence for adding links in a Markdown document and cmd-K has become some kind of pseudo standard for adding links in several writing apps.
However, in VSCode that's not possible. But I'd love to make it possible. Any ideas? cmd-K is (hard-wired?) bound to listen for a next key press.
But it doesn't have to be cmd-K. I can learn another keystroke. But I need to be able to put additional text (square brackets and parenthesis) into the text and move the cursor to the right position. How's that done?
Thanks so much!
This extension Markdown All In One looks like it does what you want in one step.
Paste link on selected text
Just select your link and hit Ctrl+V and it creates the link and inserts the clipboard link.
If for some reason you don't want to use this extension, it would be pretty easy to create a snippet to do what you want.
Adding another answer that doesn't use the extension Markdown All In One I mentioned in the other answer and because a couple of commenters requested a different way. #MarcoLackovic
First Method: keybinding in keybindings.json, then manually paste
{
"key": "alt+w", // use whatever keybinding you wish
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
},
"when": "editorHasSelection && editorLangId == markdown "
}
Select the link text and, trigger your keybinding - the cursor will be placed where you want it and paste.
Second Method: use a macro to insert the snippet and paste in one step
You will need a macro extension like multi-command to run multiple commands in series. Then this keybinding:
{
"key": "alt+w",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"snippet": "[${TM_SELECTED_TEXT}]($0)"
}
},
"editor.action.clipboardPasteAction"
]
},
"when": "editorHasSelection && editorLangId == markdown "
}
Demo of second method:

How to Create Custom Key Binded Snippets in VS Code

I am a huge Sublime Text user, and learned ways to improve my productivity using customizations in Sublime text. But as VScode is becoming popular day by day, wanted to check if there is any way which I can bind the shortcut keys to the custom actions.
For example, I select a word ABC in any file in VSCode and hit CTRL+B, and it places my own defined values around it like it should become
<b>ABC</b>
I had created the following snippet in Sublime Text, which when I wrote in Visual Studio Code - keybindings.json nothing worked.
{
"keys": [
"ctrl+b"
],
"command": "insert_snippet",
"args": {
"contents": "<b>${0:$SELECTION}</b>"
}
}
This will work in your keybindings.json:
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"when": "resourceExtname == .html", // this is optional
"args": {
"snippet": "<b>${TM_SELECTED_TEXT}</b>"
}
},
The optional when clause is if you want to limit the snippet's operation to .html files.
More general though is to use the emmet command which is built-in: Emmet: Wrap with Abbreviation in the command palette. Select your text, open the command palette, find that command and trigger it - type b or whatever your element is and it will wrap the selected text with the opening and closing elements.
[Note that there is a command workbench.action.toggleSidebarVisibility already bound to Ctrl-B, but the snippet above version seems to take precedence - meaning you lose the toggleSidebarVisibility keybinding functionality - that may be acceptable to you?]

Vscode snippet variable

"snippet with class binding":{
"prefix": "row.${variable}",
"body":[
"<table class=\"row ${same_variable_here}\">",
"\t<tr>",
"\t\t<td>",
"\t\t\t$0",
"\t\t</td>",
"\t</tr>",
"</table>"
]
}
Is it possible(and how if so) to create variables like some_entity.classname
expanding into something like this(in html for example):
<div class="classname"></div>
It looks like you have two questions there. Yes, emmet expansion will automatically turn
div.myClass into <div class="myClass"></div>. See emmet in vscode.
Your other question is about a emmet snippet for a full table expansion. See custom emmet snippets. In your settings.json you will need:
"emmet.extensionsPath": "C:\\Users\\Mark\\.vscode\\extensions"
That should point to a folder that contains a new file that you will create called snippets.json. In that file put:
{
"html": {
"snippets": {
"tableR":
"table.row.$1>tr>td"
}
}
}
Use whatever prefix you want besides "tableR". Then you must reload vscode. Then type your prefix and tab to expand (assuming you have the emmet tab expansion setting in your settings.]
[EDIT]: Based on your comment below, perhaps you are looking for something as simple as a snippet with a keybinding:
{
"key": "ctrl+alt+n",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "${TM_SELECTED_TEXT/(.*)\\.(.*)/<$1 class=\"$2\"><\\/$1>/}"
}
},
So if you select anyTag.someClass becomes <anyTag class="someClass"></anyTag>
when you use whatever keybinding you have chosen. Emmet is not involved here, this is just a simple keybinding in your keybindings.json (you can limit it to certain languages if you wish). Emmet expansion does not allow you to transform its prefix (the regexp above) the way a plain snippet can grab the selection or current word and transform it.