简体   繁体   English

Sublime Text 3 中的自动关闭 HTML 标签

[英]Auto closing HTML tags in Sublime Text 3

Sublime 2, how to auto close HTML tags and place cursor inside the tag Sublime 2,如何自动关闭 HTML 标签并将光标置于标签内

I'm trying to figure out how to get Sublime 2 to create the following behavior:我试图弄清楚如何让 Sublime 2 创建以下行为:

Type类型

<strong

then, upon typing >然后,在输入 >

Sublime will then immediately print Sublime 将立即打印

<strong></strong>

And then your cursor will be placed inside of the tag.然后您的光标将被放置在标签内。

I'm trying to do what's quoted above in Sublime Text 3. A similar question was also asked here but an answer was lacking.我正在尝试做上面在 Sublime Text 3 中引用的内容。 这里也提出类似的问题,但缺少答案。 Given the time past, I'm asking again.考虑到过去的时间,我再问一次。 Basically I want to emulate how tags are completed on Codecademy, automatically, ie without shortcuts (NO TAB) .基本上我想模拟如何在 Codecademy 上自动完成标签,即没有快捷方式 (NO TAB)

Codeacademy example.gif Codeacademy 示例.gif

There's a plugin that I use that will auto-close tags like described above named Auto Close . 我使用了一个插件,该插件将自动关闭如上所述的名为Auto Close的标签。 It works for both Sublime 2 and 3. 它适用于Sublime 2和3。

You can install it via Package Control by searching for "Auto Close". 您可以通过程序包控制搜索“自动关闭”来安装它。

This works for me.这对我有用。 It closes on a backslash, ie your close tag.它以反斜杠关闭,即您的关闭标签。

For example, you have <div>my div< and upon typing / , you will get <div>my div</div> .例如,您有<div>my div<并且在键入/ ,您将获得<div>my div</div>

To enable, add the following object to the array in the key binding file (Sublime Text > Preferences > Key Bindings).要启用,请将以下对象添加到键绑定文件中的数组(Sublime Text > Preferences > Key Bindings)。

 {
    "keys": ["/"],
    "command": "close_tag",
    "args": { "insert_slash": true },
    "context": [
        {
            "key": "selector",
            "operator": "equal",
            "operand": "(text.html, text.xml, meta.jsx.js) - string - comment",
            "match_all": true
        },
        {
            "key": "preceding_text",
            "operator": "regex_match",
            "operand": ".*<$",
            "match_all": true
        },
        { "key": "setting.auto_close_tags" }
    ]
}

Not really the answer you want but if you type </ ST3 will auto complete the closest unclosed tag.不是您真正想要的答案,但如果您输入</ ST3 将自动完成最近的未闭合标签。 Otherwise the auto complete answer by Nick is your best bet.否则,尼克的自动完成答案是您最好的选择。

I had the same issue in windows and may people in several forums suggested me to remove and reinstall Sublime 3. But issue not resolved.我在 Windows 中遇到了同样的问题,可能有几个论坛的人建议我删除并重新安装 Sublime 3。但问题没有解决。 I don't know the cause of issue.我不知道问题的原因。 it seems some problem occurred with the packages installed or settings configured.安装的软件包或配置的设置似乎出现了一些问题。 I got resolved the issue in this way by removing installed packages and settings.我通过删除已安装的软件包和设置以这种方式解决了这个问题。

In Windows 10在 Windows 10 中

go to C:\\Users\\ YOUR_USER_NAME \\AppData\\Roaming\\Sublime Text 3转到 C:\\Users\\ YOUR_USER_NAME \\AppData\\Roaming\\Sublime Text 3

Remove folders and contents from that folder it will resolve this issue.从该文件夹中删除文件夹和内容将解决此问题。

In Linux在 Linux 中

Please reverse the process described in https://packagecontrol.io/installation请颠倒https://packagecontrol.io/installation中描述的过程

There is some issue coursing with this is that all the installed packages will get removed.与此有关的一些问题是所有已安装的软件包都将被删除。 You need to reinstall needful packages again.您需要再次重新安装需要的软件包。

So please make sure to note down and reinstall all the plugins needed.所以请务必记下并重新安装所有需要的插件。

Ok, the best work around for Sublime 3 that I have found is to go ahead and install the SublimeCodeIntel package.好的,我发现最好的 Sublime 3 解决方法是继续安装SublimeCodeIntel包。 I'm not sure if it's an issue with generating popups because the keybindings as given within the package are not working, but what is definitely working is multi lang auto suggest after typing <_ (where _ is whitespace) as well as when you use the auto-suggest the closing tag auto generates.我不确定这是否是生成弹出窗口的问题,因为包中给出的键绑定不起作用,但肯定有效的是在键入<_ (其中 _ 是空格)后以及何时使用多语言自动建议自动建议自动生成的结束标记。

There's a plugin that will auto-close tags like you describe called Auto Close .有一个插件可以像您描述的那样自动关闭标签,称为Auto Close It works for both Sublime 2 and 3.它适用于 Sublime 2 和 3。

Just be aware that it doesn't work for JSX, and the package doesn't appear to be maintained, but it does the job for me so hopefully it might help.请注意,它不适用于 JSX,并且该软件包似乎没有得到维护,但它对我有用,所以希望它可能会有所帮助。

You can install it via Package Control by searching for "Auto Close".您可以通过包控制通过搜索“自动关闭”来安装它。

On Sublime Text 3 save any file as .html file.在 Sublime Text 3 上,将任何文件保存为 .html 文件。

Then typing something like:然后输入如下内容:

followed by TAB key automatically expand to:后跟TAB键自动展开为:

with complete tags of title , body and head.带有完整的 title 、 body 和 head 标签。

There is another plugin called SublimeCodeIntel which not only gives auto close functionality but also the code suggestion. 还有另一个名为SublimeCodeIntel插件,它不仅提供自动关闭功能,而且还提供代码建议。

Here is the complete documentation. 是完整的文档。

The plugin provides the following features: 该插件提供以下功能:

  • Jump to Symbol Definition - Jump to the file and line of the definition of a symbol. 跳转到符号定义-跳转到符号定义的文件和行。
  • Imports autocomplete - Shows autocomplete with the available modules/symbols in real time. 导入自动完成功能-实时显示带有可用模块/符号的自动完成功能。
  • Function Call tooltips - Displays information in the status bar about the working function. 功能调用工具提示-在状态栏中显示有关工作功能的信息。

You can install it via Package Control by searching for "SublimeCodeIntel". 您可以通过程序包控制搜索“ SublimeCodeIntel”来安装它。

Heres the official documentation这是官方文档
https://www.sublimetext.com/docs/3/auto_complete.html https://www.sublimetext.com/docs/3/auto_complete.html
Its the same in most IDE's.它在大多数 IDE 中都是一样的。 Aslong as you have the settings enabled (as shown in the document) it is then:只要您启用了设置(如文档中所示),它就会:
CTRL+Space - to show suggestions CTRL+Space - 显示建议
Enter to apply suggestion.输入以应用建议。 (you can change that in the settings) (您可以在设置中更改)

On Sublime Text 3 save blank new file as .html file.在 Sublime Text 3 上将空白的新文件保存为 .html 文件。

Then typing something like:然后输入如下内容:

<strong>

followed by TAB key automatically expand to:后跟TAB键自动展开为:

<strong> </strong>

with active cursor inside the tag在标签内带有活动光标

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

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