简体   繁体   English

Visual Studio - 如何在 HTML 标签中快速换行?

[英]Visual Studio - how to quickly wrap text in HTML tags?

The new HTML editor in VS 2013 removed WYSIWYG mode and the Formatting toolbar is disabled. VS 2013 中的新 HTML 编辑器删除了 WYSIWYG 模式并且禁用了格式工具栏。 This is for the good, generally, but it also made simple edits like making some text bold or italics harder.一般来说,这是好的,但它也进行了简单的编辑,例如使某些文本加粗或斜体更难。

What I currently do to make some text bold is:我目前所做的使一些文本加粗的是:

  1. Type <b>输入<b>
  2. VS autocompletes </b> VS 自动完成</b>
  3. I select the end tag and press Ctrl+X我选择结束标签并按 Ctrl+X
  4. I go to the end of the text that should be bold and press Ctrl+V我转到应该加粗的文本的末尾,然后按 Ctrl+V

This is much less convenient than just pressing Ctrl+B or something similar.这比按 Ctrl+B 或类似的东西方便得多。 I know there is the "Surround with" command in Visual Studio, there are customizable keyboard shortcuts etc. so my hope is that the Ctrl+B functionality can somehow be achieved in Visual Studio.我知道 Visual Studio 中有“环绕”命令,有可自定义的键盘快捷键等,所以我希望 Ctrl+B 功能可以在 Visual Studio 中以某种方式实现。 Is there some clever trick or am I possibly missing something other, rather simple?是否有一些聪明的技巧,或者我可能错过了其他一些相当简单的东西?

Web Essentials 2013 has a Surround with tag... feature (Alt+Shift+W) that is more fluid than the built-in Surround with . Web Essentials 2013具有带有标签环绕声...功能 (Alt+Shift+W),比带有. I'm probably just going to stick with it as I am using Web Essentials anyway.我可能会坚持使用它,因为无论如何我都在使用 Web Essentials。

A good suggestion from How to quickly surround text with HTML markup in VS 2008?来自如何在 VS 2008 中使用 HTML 标记快速环绕文本的好建议 is this:这是:

  1. Select your text选择您的文字
  2. Press Ctrl + X to cut it to clipboardCtrl + X将其剪切到剪贴板
  3. Type your <strong> tag, which Visual Studio will automatically close with your cursor remaining in the middle of the start and end tag键入您的<strong>标记,Visual Studio 将自动关闭该标记,光标停留在开始和结束标记的中间
  4. Press Ctrl + V to re-insert your text between the tagsCtrl + V在标签之间重新插入文本

You could also add a custom snippet (I don't know why Visual Studio didn't provide a snippet for the strong tag by default).您还可以添加自定义代码段(我不知道为什么 Visual Studio 默认没有为强标记提供代码段)。

To create the snippet:要创建代码段:

1) Create a text file in C:\\Users\\USERNAME\\Documents\\Visual Studio 2013\\Code Snippets\\Visual Web Developer\\My HTML Snippets named strong.snippet 1) 在 C:\\Users\\USERNAME\\Documents\\Visual Studio 2013\\Code Snippets\\Visual Web Developer\\My HTML Snippets 中创建一个名为 strong.snippet 的文本文件

2) Place the following text inside the text file: 2) 将以下文本放入文本文件中:

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>strong</Title>
    <Author>YOUR NAME</Author>
    <Shortcut>strong</Shortcut>
    <Description>Markup snippet for a strong text</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Code Language="html"><![CDATA[<strong>$selected$$end$</strong>]]></Code>
  </Snippet>
</CodeSnippet>

To use the snippet:要使用代码段:

1) Select your text 1)选择你的文字

2) Right-click text and choose "Surround With...", or press Ctrl-K,Ctrl-S 2) 右键单击​​文本并选择“环绕...”,或按 Ctrl-K、Ctrl-S

3) Open "My HTML Snippets" folder in the popup menu and choose your strong snippet 3) 在弹出菜单中打开“My HTML Snippets”文件夹并选择您的强代码段

Create an HTML Custom Code-Snippet in Visual Studio在 Visual Studio 中创建 HTML 自定义代码片段

1. Create a new .xml file 1.新建一个.xml文件

2. Use the following snippet template: 2.使用以下片段模板:

 <CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <!-- HTML Anchor Snippet -->
  <Header>
    <Title>P</Title>
    <Author>Microsoft Corporation</Author>
    <Shortcut>P</Shortcut>
    <Description>Markup snippet for a Paragraph</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Paragraph</ID>
        <ToolTip>Paragraph</ToolTip>
        <Default>p</Default>
      </Literal>
      <Literal>
        <ID>selected</ID>
        <ToolTip>content</ToolTip>
        <Default>content</Default>
      </Literal>
    </Declarations>
    <Code Language="html"><![CDATA[<p>$selected$</p>$end$]]></Code>

  </Snippet>
</CodeSnippet>

3. Give a name to your snippet, and save-it-as using( YourFileName.snippet ) extention 3.为您的代码段命名,并将其另存为using( YourFileName.snippet ) 扩展名

Add snippet to Visual Studio将代码片段添加到 Visual Studio

1. Use Code Snippets manager: Go to: Tools/Code Snippets Manager/ 1.使用代码片段管理器:转到:工具/代码片段管理器/

2. Choose HTML from a DropDown list, and open HTML folder by DoubleClick on it. 2.下拉列表中选择 HTML,然后双击打开HTML文件夹。

4. Click Import button, and browse to your newly .snippet file 4.单击导入按钮,然后浏览到您新的.snippet文件


Use Your Custom Snippet使用您的自定义代码段

1. Select some text in the Text-Editor . 1.文本编辑器中选择一些文本。

2. use combination of CTRL+K CTRL+X to open Insert Snippet dialog window 2.使用CTRL+K CTRL+X组合打开Insert Snippet对话窗口

3. Choose HTML from a DropDown list where you previously saved your .snipped file 3.从之前保存.snipped 文件的下拉列表中选择HTML

4. Choose your custom snippet from a list. 4.从列表中选择您的自定义片段。 It should now wrap your text with <p></p> tag for example它现在应该用<p></p>标签包裹你的文本,例如


★ Follow the above procedure again for creating a new snippents. ★ 再次按照上述步骤创建新的片段。

Read More about custom snippets 阅读更多关于自定义片段的信息



☕ HINT (just like in Sublime text editor build in feature) ☕ 提示(就像在 Sublime 文本编辑器中内置功能一样)
You could create a Lorem Ipsum custom snippet just like this in a seconds:您可以在几秒钟内创建一个 Lorem Ipsum 自定义片段,如下所示:

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <!-- HTML Anchor Snippet -->
  <Header>
    <Title>LoremIpsum</Title>
    <Author>Microsoft Corporation</Author>
    <Shortcut>Lorem</Shortcut>
    <Description>Markup snippet for a Lorem Ipsum</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
      <SnippetType>SurroundsWith</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>Lorem</ID>
        <ToolTip>LoremIpsum</ToolTip>
        <Default>Lorem</Default>
      </Literal>
      <Literal>
        <ID>selected</ID>
        <ToolTip>content</ToolTip>
        <Default>content</Default>
      </Literal>
    </Declarations>
    <Code Language="html"><![CDATA[$selected$Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus blandit nunc eget consectetur aliquet. Vivamus feugiat ipsum ex, eget egestas erat condimentum eget. Suspendisse luctus velit ac purus venenatis imperdiet sed vel mauris. Ut tempus risus elit, eget molestie purus hendrerit a. Curabitur ac urna posuere, lacinia neque quis, facilisis libero. Cras viverra tellus eu lacus hendrerit, in auctor neque sodales. Pellentesque et finibus lectus. Vestibulum pulvinar, lorem in dignissim fermentum, nunc ipsum luctus arcu, non malesuada nisi sem nec libero. Integer eget arcu interdum, elementum lacus et, ornare arcu. Phasellus quis nulla non enim porta ornare non eget ex. Mauris at purus sed ipsum congue facilisis. Donec tempor, eros ac posuere tempus, velit sapien faucibus augue, vitae convallis augue nulla ut felis. Nunc sapien libero, dictum non finibus nec, rutrum at odio. Nulla facilisi. $end$]]></Code>
  </Snippet>
</CodeSnippet>



☕ HINT ☕ 提示
You Can create almost any code snippet you want by providing a language="" property, and by setting up right description in<code> section您可以通过提供language=""属性并在 <code> 部分设置正确的描述来创建几乎任何您想要的代码片段
The following is the example of a JavaScript Alert() function code snippet以下是 JavaScript Alert() 函数代码片段的示例

<Code Language="JavaScript"><![CDATA[alert($selected$);$end$]]></Code>

单击要包装的标签,然后按 Ctrl + Alt + W

In visual studio code you can use the extension by name htmltagwrap在 Visual Studio 代码中,您可以使用扩展名htmltagwrap

Link关联

https://marketplace.visualstudio.com/items?itemName=bradgashler.htmltagwrap https://marketplace.visualstudio.com/items?itemName=bradgashler.htmltagwrap

Installation Options安装选项

Option 1. Search for htmltagwrap in VS Code extensions tab and install it.选项 1.在 VS Code 扩展选项卡中搜索 htmltagwrap 并安装它。

Option 2. Go to the marketplace link mentioned above and click on install, it will prompt you to open it with VS Code and once you do that it'll be installed.选项 2.转到上面提到的市场链接并单击安装,它会提示您使用 VS Code 打开它,一旦您这样做,它将被安装。

Usage用法

  • Select one or more blocks of text or strings of text.选择一个或多个文本块或文本字符串。
  • Press Alt + W or Option + W for Mac.对于 Mac,请按 Alt + W 或 Option + W。
  • Type the tag name you want.键入所需的标记名称。

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

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