如何在VIM中视觉选择中删除单词

发布于 2025-02-07 22:46:48 字数 380 浏览 2 评论 0原文

假设我有以下输入,如下所示。我想做的是视觉上选择第2行至4(shift + v),然后删除狗一词。

我该怎么做?我知道我可以在选择中使用:s/dog //之类的东西,但是我想知道是否有更直接的方式。

1 The quick brown dog
2 dog jumps
3 over the
4 lazy dog,
5 but it should be just a fox.

最终输出应为(仅受第2行至4上的视觉选择的影响):

1 The quick brown dog
2  jumps
3 over the
4 lazy ,
5 but it should be just a fox.

Suppose I have the following input as shown below. What I would like to do is to visually select lines 2 through 4 (shift + v) and then delete the word dog.

How can I do that? I know I can use something like :s/dog// on my selection, but I was wondering if there's a more straightforward way.

1 The quick brown dog
2 dog jumps
3 over the
4 lazy dog,
5 but it should be just a fox.

The final output should be (affected only by the visual selection on lines 2 through 4):

1 The quick brown dog
2  jumps
3 over the
4 lazy ,
5 but it should be just a fox.

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

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

发布评论

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

评论(4

风蛊 2025-02-14 22:46:48

当您处于视觉模式时,按自动将视觉范围插入命令行中,以便您在此之后使用的任何EX命令都可以在视觉上选择的行上使用:

  1. :变为:'<,'>,其中'<,'>是从视觉选择的第一行开始,并在视觉选择的最后一行。
  2. 之后,您可以执行s/dog< cr>(无需//)来代替第一个dog noth选定的行。这实际上等同于做:2,4S/dog< cr>

从语义的角度来看,:s/dog< cr>在当前视觉选择中的内置功能中所能获得的近距离“因此,除非制作一个假设的自定义映射以节省几个击键,否则您不太可能找到一种更加“直接”的方式。

When you are in visual mode, pressing : automatically inserts the visual range in the command-line so whatever Ex command you use after that is going to work on the visually selected lines:

  1. : becomes :'<,'>, in which '<,'> is a range beginning on the first line of the visual selection and ending on the last line of the visual selection.
  2. After that, you can do s/dog<CR> (no need for the //) to substitute the first dog with nothing on every selected line. This is effectively equivalent to doing :2,4s/dog<CR>.

From a semantic point of view, :s/dog<CR> is as close as you can get with the built-in features to "remove dog in the current visual selection" so, barring making an hypothetical custom mapping that would only save a couple of keystrokes, you are unlikely to find a more "straightforward" way.

停滞 2025-02-14 22:46:48

那是不可能的。

为什么?


tl; dr

我认为,您正在设想在视觉模式下像正常模式一样的东西(以便在视觉模式处于视觉模式时,您可以“移动和删除”)。这样的事情不存在。


考虑一下:如果您可视选择一些行,那么 ,别无其他,是您下一步采取的任何操作的对象( d s < /kbd>, c 或其他)。

但是,您想在视觉上选择的行,而是对其中的单词上采取行动。但是,您如何告诉VIM对 dog 的单词采取行动,而不是换句话说?您可以通过运动来做到这一点,但是只要您处于视觉模式,这是不可能的,因为任何动作都会改变视觉选择,并且不允许您以某种方式移动它。

这意味着您需要放弃视觉选择,以便可以移动到这些单词并将其用作动作的文本对象。

如果您真的想从视觉选择开始,那么提供您要对单词 dog 采取行动的信息的唯一方法是在视觉模式时输入它们。这正是:s方法所做的。

That's impossible.

Why?


tl;dr

I think, you are envisioning something like a normal mode within visual mode (so that you can "move and delete like in Vim" while you are in visual mode). Such a thing doesn't exist.


Think about it: if you visual select some lines, then those lines, and nothing else, are the object of whatever action you do next (d, s, c, or whatever).

But you want to take an action not on those visually selected lines, but on words within them. But how can you tell Vim to take action on the words dog and not on other words? You can do that with movements, but as long as you are in visual mode, that's not possible, because any movement will just change the visual selection, and not allow you to somehow move within it.

This means that you need to abandon the visual selection so that you can move to those words and use them as the textual object for the action.

If you really want to start from the visual selection, then the only way to give the info that you want to take action on the words dog, is to type them out while you are in visual mode. And that's precisely what the :s approach does.

↘人皮目录ツ 2025-02-14 22:46:48

您可以利用标记'&lt;'&gt;:它们存储视觉选择的开始/结束位置,并在您退出Visual后保持其价值模式。

在命令行模式下, ctrl-r ctrl-w 插入光标下的单词。

通过结合使用,您可以创建这样的映射:

noremap <c-d> :'<,'>s/<c-r><c-w>//<cr>

然后使用它:

  • 首先选择所需的区域,其中v;
  • 点击 ESC 到退出视觉模式;
  • 在要删除的单词下移动光标;
  • 然后触发映射,在此示例 ctrl-d 中。

You can take advantage of the marks '< and '>: they store the start/end position of the visual selection and they keep their value after you exit the visual mode.

In command line mode, Ctrl-RCtrl-W inserts the word under the cursor.

By combining this, you can create a mapping like that:

noremap <c-d> :'<,'>s/<c-r><c-w>//<cr>

Then to use it:

  • first select the wanted zone with V;
  • hit Esc to exit visual mode;
  • move your cursor under the word you want to delete;
  • then trigger the mapping, in this example Ctrl-D.
影子是时光的心 2025-02-14 22:46:48

我认为,没有办法将特定行中的特定单词替换为视觉选择。您也可以为此使用sed(查看#5)。

无论如何:
这是在文件中删除dog一词的四种方法,也是一种使用sed进行操作的方法:

1(带有视觉模式):

  1. 键入v输入视觉字符模式
  2. 突出显示dog
  3. 按D用于删除

2(替代和确认):

:%s/dog//gc

g代表全局

c代表确认,

您将要求每个条目,该怎么办。

3(替代):

:2,4s/dog//

4(使用搜索模式):

/dog

类型:n下一个匹配

类型:d用于删除匹配项

以获取更多信息:

5(使用sed):

sed 2,4\s/dog// filename

I think there is no way in way to just replace a specific word in specific lines with visual selection. You can also use sed for that (look at #5).

Anyways:
Here are 4 way to delete the word dog in a file and one way to do it with sed:

1 (with visual mode):

  1. Type v to enter visual character mode
  2. Highlight dog
  3. Press d for deleting

2 (with substitute and confirmation):

:%s/dog//gc

g stands for global

c stand for confirmation

You will be ask for every entry, what to do with.

3 (with substitute):

:2,4s/dog//

4 (with search mode):

/dog

Type: n for next match

Type: d for deleting the match

For further information: Search and Replace

5 (with sed):

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