Vim Surround:创建新标签但不缩进/新行

发布于 2024-11-09 08:13:40 字数 270 浏览 4 评论 0原文

我想模仿 Textmates CTRL+ALT+w,它在同一行上创建一对新的开始和结束 HTML 标签。

在 VIM Surround 中,我在编辑模式下使用 CTRL+st 来实现此目的,但它总是在设置标签后缩进并创建一个新行,因此它看起来像这样(* = 光标位置):

<p>
  *
</p>

有没有办法实现此目的? :

<p>*</p>

I would like to mimic Textmates CTRL+ALT+w, which creates a new pair of opening and closing HTML tags on the same line.

In VIM Surround I'm using CTRL+st in Edit mode for this, but it always indents and creates a new line after setting the tag, so that it looks like this (* = cursor position):

<p>
  *
</p>

Is there a way to achieve this? :

<p>*</p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嗫嚅 2024-11-16 08:13:40

我猜你的问题是所选区域是“逐行”的。例如,如果您使用 V 选择一些生命并用标签将其包围,则标签将放置在所选行的上方一行和下方一行。

您可能想要创建一个“字符明智”的选择,在包围它之前使用 v

无论如何,请发布您创建的地图,以便我们帮助调试。


更新

在评论中进行一些澄清后,我会告诉您环绕插件不是最好的选择。正如其名称所描述的,它是为了处理周围的内容而创建的。所以你可能需要围绕内容。

对于您的情况,我建议您查看 HTML AutoCloseTag。一旦您输入 >,该插件就会关闭 html 标签。它当然更合适,并且比环绕声使用的击键更少。

<p         <--- Now when you type ">", if becomes:
<p>|</p>   <--- Where "|" is the cursor.

显然,您将在每个标签中获得这种行为。但如果你喜欢的话,这可能会很方便。

I guess your problem is that the selected area is "line wise". For example, if you select a few lives with V and surround it with tags, the tags will be placed one line above and one bellow the selected lines.

You probably want to create a "character wise" selection, with v before surrounding it.

Anyway, please post the map you created, so we can help debugging this.


Update

After some clarification in the comments, I would tell you that the surround plugin is not the best option. As its name describes, it was created to deal with surrounded content. So you may need content to surround.

In your case, I recommend taking a look in HTML AutoCloseTag. This plugin closes the html tag once you type the >. It is certainly more appropriated, and uses less keystrokes than surround.

<p         <--- Now when you type ">", if becomes:
<p>|</p>   <--- Where "|" is the cursor.

Obviously, you will get this behavior to every tag. But that may be handy if you like it.

原野 2024-11-16 08:13:40

在正常模式下,输入 vstp> 进入可视模式,并在当前光标位置的同一行上输出开始和结束

标记。使用大写 S 来保持当前的缩进级别。

这不会将光标放在您所描述的标签之间,但 Textmate 的 CtrlW 快捷方式也不会(我认为您的意思是 CTRL+Shift+w,而不是 CTRL+ ALT+w,因为后者仅输出菱形符号。)

From normal mode, type vstp> to enter visual mode and output an opening and closing <p> tag on the same line at the current cursor position. Use a capital S to maintain the current indent level.

This doesn't place the cursor in between the tags as you describe, but neither does Textmate's CtrlW shortcut (I think you meant CTRL+Shift+w, not CTRL+ALT+w, as the latter just outputs a diamond sign.)

无妨# 2024-11-16 08:13:40

我的回答可能会晚一些,但我会尽力提供帮助。
我在使用 Vimsurround 插件时遇到了类似的问题。每次我使用 ctrl+V 选择句子(一行)并尝试用一些东西包围它时,我得到的是:

{
var myVar
}

而不是这个:

{ var myVar } // what I wanted

我找到了简单的解决方案:从正常模式中,我选择带有 vis 的行命令,然后我输入大写 C (我的 vim 环绕映射)并选择要包围的括号。然后我得到了一行很好的包围。

My answer is probably coming to late, but I'll try to help.
I had similar problem with Vimsurround plugin. Every time I select sentence (one line) using ctrl+V and try to surround it with something I get this:

{
var myVar
}

instead of this:

{ var myVar } // what I wanted

I found easy solution: From a normal mode I choose a line with vis command and then I type capital C (my vim surround mapping ) and choose brackets to surround.Then I get one line nicely surrounded.

萌能量女王 2024-11-16 08:13:40

从技术上讲,问题标题根据作者实际寻找的内容进行了错误标记,但由于我实际上是在寻找标题中提出的问题的答案,因此我认为我也应该提供答案。

要在元素周围创建一个新标签,而不使用 Vim Surround 在使用块明智选择时使用的自动缩进(即:VysS),您可以这样做:

^ys$

此命令会将光标移动到该行的第一个非空白字符,发出您想要使用 You Surround 的命令,然后移动到该行的末尾。然后,只需开始输入您的标签即可。

结果是这样的:

<input type="email" name="email">

可能会变成这样:

<li><input type="email" name="email"></li>

该命令对于 . 以及所有其他正常的 Vim 优点都是可重复的。

The question title is technically mislabeled based on what the author was actually looking for, but since I was actually looking for the answer to the question asked in the title, I figure I should provide an answer to it as well.

To create a new tag surrounding an element without the automatic indentation Vim Surround uses when using a block wise selection (ie: VysS), you can instead do something like:

^ys$

This command will move your cursor to the first non-blank character of the line, issue the command that you want to utilize You Surround, and move to the end of the line. Then, simply start entering your tag.

The result is this:

<input type="email" name="email">

Could become something like this:

<li><input type="email" name="email"></li>

The command is repeatable as well with . and all the normal other Vim goodness.

蹲在坟头点根烟 2024-11-16 08:13:40

偶然发现这个问题是因为我也想知道这个问题 - 我相信最简单的方法就是:

yss<p>

(yss 用一些不缩进的内容包围一行 - 请参阅此处: http://www.catonmat.net/blog/vim-plugins-surround-vim/)

Stumbled upon this question because I was wondering this as well - I believe the simplest way to do this is just:

yss<p>

(yss surrounds a line with something without indenting - see here: http://www.catonmat.net/blog/vim-plugins-surround-vim/)

廻憶裏菂餘溫 2024-11-16 08:13:40

您可以通过选择相关文本对象来完成此操作: :h text-objects

...并将其包围而不是包围视觉线选择。

我发现自己遇到的最常见的例子是尝试用另一个标签包围一个标签时。在这种情况下,itat 文本对象非常有用:

                        *v_at* *at*
at          "a tag block", select [count] tag blocks, from the
            [count]'th unmatched "<aaa>" backwards to the matching
            "</aaa>", including the "<aaa>" and "</aaa>".
            See |tag-blocks| about the details.
            When used in Visual mode it is made characterwise.

                        *v_it* *it*
it          "inner tag block", select [count] tag blocks, from the
            [count]'th unmatched "<aaa>" backwards to the matching
            "</aaa>", excluding the "<aaa>" and "</aaa>".
            See |tag-blocks| about the details.
            When used in Visual mode it is made characterwise.

例如,如果您将光标放在一个段落中,并且希望在同一行,ysat

就可以实现这一点。

You can accomplish this by selecting the relevant text object: :h text-objects

...and surrounding that instead of surrounding a Visual Line selection.

The most common example I found myself running into was when trying to surround one tag with another. In that situation, the it and at text objects are quite useful:

                        *v_at* *at*
at          "a tag block", select [count] tag blocks, from the
            [count]'th unmatched "<aaa>" backwards to the matching
            "</aaa>", including the "<aaa>" and "</aaa>".
            See |tag-blocks| about the details.
            When used in Visual mode it is made characterwise.

                        *v_it* *it*
it          "inner tag block", select [count] tag blocks, from the
            [count]'th unmatched "<aaa>" backwards to the matching
            "</aaa>", excluding the "<aaa>" and "</aaa>".
            See |tag-blocks| about the details.
            When used in Visual mode it is made characterwise.

For example, if you had your cursor in a paragraph and you wanted to surround it with a div on the same line, ysat<div> would accomplish that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文