如何在 Vim 中快速关闭 HTML 标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/130734/
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
How can one close HTML tags in Vim quickly?
提问by wds
It's been a while since I've had to do any HTML-like code in Vim
, but recently I came across this again. Say I'm writing some simple HTML
:
我已经有一段时间没有在 中编写任何类似 HTML 的代码了Vim
,但最近我再次遇到了这个问题。说我正在写一些简单的HTML
:
<html><head><title>This is a title</title></head></html>
How do I write those closing tags for title, head and html down quickly? I feel like I'm missing some really simple way here that does not involve me going through writing them all down one by one.
我如何快速写下标题、标题和 html 的结束标签?我觉得我在这里错过了一些非常简单的方法,不需要我将它们一一写下来。
Of course I can use CtrlPto autocomplete the individual tag names but what gets me on my laptop keyboard is actually getting the brackets and slash right.
当然,我可以使用CtrlP自动完成各个标签名称,但让我在笔记本电脑键盘上使用的实际上是正确使用括号和斜线。
采纳答案by Ian P
Check this out..
看一下这个..
closetag.vim
closetag.vim
Functions and mappings to close open HTML/XML tags
https://www.vim.org/scripts/script.php?script_id=13
https://www.vim.org/scripts/script.php?script_id=13
I use something similar.
我使用类似的东西。
回答by hakamadare
I find using the xmleditplugin pretty useful. it adds two pieces of functionality:
我发现使用xmledit插件非常有用。它增加了两个功能:
- When you open a tag (e.g.type
<p>
), it expands the tag as soon as you type the closing>
into<p></p>
and places the cursor inside the tag in insert mode. If you then immediately type another
>
(e.g.you type<p>>
), it expands that into<p>
</p>
- 当你打开一个标签(例如类
<p>
),它扩展了标签,只要你键入右>
进<p></p>
和插入模式将光标置于标签内。 如果您立即输入另一个
>
(例如您输入<p>>
),它会将其扩展为<p>
</p>
and places the cursor inside the tag, indented once, in insert mode.
并将光标置于标签内,在插入模式下缩进一次。
The xmlvim plugin adds code folding and nested tag matching to these features.
的XMLVIM插件添加代码折叠和嵌套代码匹配到这些特征。
Of course, you don't have to worry about closing tags at all if you write your HTML content in Markdownand use %!
to filter your Vim buffer through the Markdown processor of your choice :)
当然,如果您在Markdown 中编写 HTML 内容并使用%!
您选择的 Markdown 处理器过滤 Vim 缓冲区,则完全不必担心关闭标签:)
回答by sjh
I like minimal things,
我喜欢极简的事物,
imap ,/ </<C-X><C-O>
回答by Krzysiek Goj
I find it more convinient to make vim write both opening and closing tag for me, instead of just the closing one. You can use excellent ragtag pluginby Tim Pope. Usage looks like this (let | mark cursor position) you type:
我发现让 vim 为我编写开始和结束标记更方便,而不仅仅是结束标记。您可以使用Tim Pope 的优秀ragtag 插件。您键入的用法如下(让 | 标记光标位置):
span|
press CTRL+xSPACE
按CTRL+xSPACE
and you get
你得到
<span>|</span>
You can also use CTRL+xENTERinstead of CTRL+xSPACE, and you get
你也可以使用CTRL+xENTER而不是CTRL+ xSPACE,你会得到
<span> | </span>
Ragtag can do more than just it (eg. insert <%= stuff around this %> or DOCTYPE). You probably want to check out other plugins by author of ragtag, especially surround.
Ragtag 可以做的不仅仅是它(例如,在这个 %> 或 DOCTYPE 周围插入 <%= 东西)。你可能想检查出其他插件乌合之众的作家,尤其是环绕。
回答by thanthese
If you're doing anything elaborate, sparkupis very good.
如果你正在做任何复杂的事情,sparkup是非常好的。
An example from their site:
他们网站上的一个例子:
ul > li.item-$*3
expands to:
ul > li.item-$*3
扩展为:
<ul>
<li class="item-1"></li>
<li class="item-2"></li>
<li class="item-3"></li>
</ul>
with a <C-e>
.
带有<C-e>
.
To do the example given in the question,
为了做问题中给出的例子,
html > head > title{This is a title}
yields
产量
<html>
<head>
<title>This is a title</title>
</head>
</html>
回答by Preet Kukreti
There is also a zencoding vim plugin: https://github.com/mattn/zencoding-vim
还有一个 zencoding vim 插件:https: //github.com/mattn/zencoding-vim
tutorial: https://github.com/mattn/zencoding-vim/blob/master/TUTORIAL
教程:https: //github.com/mattn/zencoding-vim/blob/master/TUTORIAL
Update:this now called Emmet: http://emmet.io/
更新:现在称为Emmet:http: //emmet.io/
An excerpt from the tutorial:
教程摘录:
1. Expand Abbreviation
Type abbreviation as 'div>p#foo$*3>a' and type '<c-y>,'.
---------------------
<div>
<p id="foo1">
<a href=""></a>
</p>
<p id="foo2">
<a href=""></a>
</p>
<p id="foo3">
<a href=""></a>
</p>
</div>
---------------------
2. Wrap with Abbreviation
Write as below.
---------------------
test1
test2
test3
---------------------
Then do visual select(line wize) and type '<c-y>,'.
If you request 'Tag:', then type 'ul>li*'.
---------------------
<ul>
<li>test1</li>
<li>test2</li>
<li>test3</li>
</ul>
---------------------
...
12. Make anchor from URL
Move cursor to URL
---------------------
http://www.google.com/
---------------------
Type '<c-y>a'
---------------------
<a href="http://www.google.com/">Google</a>
---------------------
回答by Keith Pinson
Mapping
映射
I like to have my block tags (as opposed to inline) closed immediately and with as simple a shortcut as possible (I like to avoid special keys like CTRLwhere possible, though I do use closetag.vim
to close my inline tags.) I like to use this shortcut when starting blocks of tags (thanks to @kimilhee; this is a take-off of his answer):
我喜欢立即关闭我的块标签(而不是内联标签)并使用尽可能简单的快捷方式(我喜欢尽可能避免使用特殊键CTRL,尽管我确实closetag.vim
用来关闭我的内联标签。)我喜欢使用这个开始标签块时的快捷方式(感谢@kimilhee;这是他的回答的一个起飞):
inoremap ><Tab> ><Esc>F<lyt>o</<C-r>"><Esc>O<Space>
Sample usage
示例用法
Type—
类型-
<p>[Tab]
Result—
结果-
<p>
|
</p>
where |
indicates cursor position.
其中|
表示光标位置。
Explanation
解释
inoremap
meanscreate the mapping in insert mode><Tab>
meansa closing angle brackets and a tab character; this is what is matched><Esc>
meansend the first tag and escape from insert into normal modeF<
meansfind the last opening angle bracketl
meansmove the cursor right one (don't copy the opening angle bracket)yt>
meansyank from cursor position to up until before the next closing angle bracket (i.e. copy tags contents)o</
meansstart new line in insert mode and add an opening angle bracket and slash<C-r>"
meanspaste in insert mode from the default register ("
)><Esc>
meansclose the closing tag and escape from insert modeO<Space>
meansstart a new line in insert mode above the cursor and insert a space
inoremap
意味着在插入模式下创建映射><Tab>
表示右尖括号和制表符;这是匹配的><Esc>
意味着结束第一个标签并从插入到正常模式转义F<
意味着找到最后一个左尖括号l
表示将光标向右移动一个(不要复制左尖括号)yt>
表示从光标位置向上拉到下一个右尖括号之前(即复制标签内容)o</
意味着在插入模式下开始新行并添加一个左尖括号和斜线<C-r>"
表示从默认寄存器 ("
)以插入模式粘贴><Esc>
意味着关闭结束标记并退出插入模式O<Space>
表示在光标上方以插入模式开始一个新行并插入一个空格
回答by Sheharyar
Check out vim-closetag
查看 vim-closetag
It's a really simple script (also available as a vundle
plugin) that closes (X)HTML tags for you. From it's README
:
这是一个非常简单的脚本(也可作为vundle
插件使用),可为您关闭 (X)HTML 标签。从它是README
:
If this is the current content:
<table|
Now you press >, the content will be:
<table>|</table>
And now if you press >again, the content will be:
<table> | </table>
如果这是当前内容:
<table|
现在按>,内容将是:
<table>|</table>
现在如果你再按>一次,内容将是:
<table> | </table>
Note: |
is the cursor here
注意:|
这里是光标
回答by kimilhee
allml (now Ragtag ) and Omni-completion ( <C-X><C-O> ) doesn't work in a file like .py or .java.
allml(现在是 Ragtag )和 Omni-completion( <CX><CO> )在 .py 或 .java 之类的文件中不起作用。
if you want to close tag automatically in those file, you can map like this.
如果你想在这些文件中自动关闭标签,你可以像这样映射。
imap <C-j> <ESC>F<lyt>$a</^R">
( ^R is Contrl+R : you can type like this Control+v and then Control+r )
( ^R 是 Control+R :你可以像这样输入 Control+v 然后 Control+r )
(| is cursor position ) now if you type..
(| 是光标位置) 现在如果你输入..
<p>abcde|
<p>ABCDE|
and type ^j
然后输入^j
then it close the tag like this..
然后它像这样关闭标签..
<p>abcde</p>|
<p>abcde</p>|
回答by mloskot
Here is yet another simple solution based on easily foundable Web writing:
这是另一个基于易于找到的 Web 写作的简单解决方案:
:iabbrev </ </<C-X><C-O>
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
:iabbrev </ </<C-X><C-O>
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags