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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30043891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:26:57  来源:igfitidea点击:

Auto closing HTML tags in Sublime Text 3

htmlautocompletesublimetext3

提问by P-L

Sublime 2, how to auto close HTML tags and place cursor inside the tag

I'm trying to figure out how to get Sublime 2 to create the following behavior:

Type

<strong

then, upon typing >

Sublime will then immediately print

<strong></strong>

And then your cursor will be placed inside of the tag.

Sublime 2,如何自动关闭 HTML 标签并将光标置于标签内

我试图弄清楚如何让 Sublime 2 创建以下行为:

类型

<strong

然后,在输入 >

Sublime 将立即打印

<strong></strong>

然后您的光标将被放置在标签内。

I'm trying to do what's quoted above in Sublime Text 3. A similar question was also asked herebut an answer was lacking. Given the time past, I'm asking again. Basically I want to emulate how tags are completed on Codecademy, automatically, i.e. without shortcuts (NO TAB).

我正在尝试做上面在 Sublime Text 3 中引用的内容。这里也提出类似的问题,但缺少答案。考虑到过去的时间,我再问一次。基本上我想模拟如何在 Codecademy 上自动完成标签,即没有快捷方式 (NO TAB)

Codeacademy example.gif

Codeacademy 示例.gif

回答by Federico

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" }
    ]
}

回答by Bwata

Not really the answer you want but if you type </ST3 will auto complete the closest unclosed tag. Otherwise the auto complete answer by Nick is your best bet.

不是你想要的答案,但如果你输入</ST3 将自动完成最近的未闭合标签。否则,尼克的自动完成答案是您最好的选择。

回答by Jishnu Venugopal

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

我在 Windows 中遇到了同样的问题,可能有几个论坛的人建议我删除并重新安装 Sublime 3。但问题没有解决。我不知道问题的原因。安装的软件包或配置的设置似乎出现了一些问题。我通过删除已安装的软件包和设置以这种方式解决了这个问题。

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.

所以请务必记下并重新安装所有需要的插件。

回答by systemaddict

Ok, the best work around for Sublime 3 that I have found is to go ahead and install the SublimeCodeIntelpackage. 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.

好的,我发现的 Sublime 3 的最佳解决方法是继续安装SublimeCodeIntel包。我不确定这是否是生成弹出窗口的问题,因为包中给出的键绑定不起作用,但肯定有效的是输入后多语言自动建议<_(其中 _ 是空格)以及何时使用自动-suggest 自动生成的结束标记。

回答by djstone

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 标签。

回答by NickOS

Heres the official documentation
https://www.sublimetext.com/docs/3/auto_complete.html
Its the same in most IDE's. Aslong as you have the settings enabled (as shown in the document) it is then:
CTRL+Space - to show suggestions
Enter to apply suggestion. (you can change that in the settings)

这是官方文档
https://www.sublimetext.com/docs/3/auto_complete.html
在大多数 IDE 中都是一样的。只要您启用了设置(如文档中所示),它就是:
CTRL+Space - 显示建议
Enter 应用建议。(您可以在设置中更改)

回答by bugsbunny5112

On Sublime Text 3 save blank new file as .html file.

在 Sublime Text 3 上将空白的新文件保存为 .html 文件。

Then typing something like:

然后输入如下内容:

<strong>

<strong>

followed by TAB key automatically expand to:

后跟TAB键自动展开为:

<strong> </strong>

<strong> </strong>

with active cursor inside the tag

在标签内带有活动光标