您对VIM的问题是您没有Grok vi

发布于 2025-02-04 07:17:33 字数 471 浏览 3 评论 0原文

我们可以使用M移动线路,而J加入线路。例如,如果您有一个列表,并且想在不删除它们的情况下将所有与之匹配的内容分开(或相反不匹配某些图案),那么您可以使用类似的内容,例如::%g/foo/m $ ...以及所有的所有内容” foo”行将移至文件末尾。 (请注意有关将文件末尾用作刮擦空间的另一个提示)。这将保留所有“ foo”线的相对顺序,同时将其从列表中的其余部分中提取。 (这将等同于做以下操作:1g!ggmap!ggrep foo< enter> 1g:1,'ag/foo'/d(将文件复制到自己的尾巴上,通过过滤尾巴,通过grep,并从头部删除所有东西)。

grep,从头部 t我围绕这个顺序:

1G!ggmap!ggrep foo< enter> 1g:1,'ag/foo'/d

有人帮助详细说明,为什么有什么ggmap? GGMAP?

We can use m to move lines around, and j to join lines. For example if you have a list and you want to separate all the stuff matching (or conversely NOT matching some pattern) without deleting them, then you can use something like: :% g/foo/m$ ... and all the "foo" lines will have been moved to the end of the file. (Note the other tip about using the end of your file as a scratch space). This will have preserved the relative order of all the "foo" lines while having extracted them from the rest of the list. (This would be equivalent to doing something like: 1G!GGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d (copy the file to its own tail, filter the tail through grep, and delete all the stuff from the head).

Going through the this legendary answer by Jim Dennis but I still can't get my head around this sequence:

1G!GGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d

Someone help elaborate, what is GGmap? Why's there a bang between 1G! GGmap? Does Ggrep come from vim-fugitive?

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

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

发布评论

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

评论(1

梦亿 2025-02-11 07:17:33

!gg位外,该命令中的一切都很有意义。让我们解构整个过程:

1G!GGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d
  1. 1G在第1行上移动光标,请参见:help g
  2. !gg 没有任何意义,
  3. ma创建标记a,请参见:help m ,
  4. p将未命名寄存器的内容放在之后光标,请参见:help p
  5. !ggrep foo&lt; enter&gt;用外部命令 grep foo ,请参阅:help!
  6. 1G在第1行上移动光标,
  7. :1,'ag/foo'/foo'/d剪切每行匹配foo'从第1行到标记a,请参见:help:range ,:help:help:g:帮助D

我相信步骤2是错字。 !gg字面意思是“用外部命令g”从当前行进行过滤文本,这是没有意义的,因为A)g不是一个通用的Unix命令(甚至存在),b)不在执行EX命令所必需的&lt; enter&gt;之后。

该步骤应该在答案的正文中描述:复制缓冲区的内容。可以通过多种方式完成:

yG

从这里到最后一行,随后是:

G

将光标放在最后一行中,在这种情况下似乎更合适。因此,整个序列实际上应该是:

1GyGGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d<ENTER>

哪个在VIM和VI中起作用。

---编辑---

请注意,仍然有一些改进的空间。以下序列具有相同的结果,但没有不必要的光标运动:

:$ka|r!grep foo %<ENTER>:1,'ag/foo/d<ENTER>

它也可以在ex ;-)中起作用。

另一个优化的版本,该版本与答案试图演示的相反(作为刮擦区域而不是文件底部)的相反,但具有完全相同的结果:

:0r!grep -v foo %<ENTER>:+,$v/foo/d<ENTER>
  • 没有正常模式光标运动,
  • 没有标记。

但是,老实说,这个问题并不需要简单的解决方案,可以用简单的解决方案来完成:

:g/foo/m$

在Ex,VI,每个VI克隆,甚至在ED中都起作用。

Everything makes sense in that command except the !GG bit. Let's deconstruct the whole thing:

1G!GGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d
  1. 1G moves the cursor on line 1, see :help G,
  2. !GG makes no sense,
  3. ma creates mark a, see :help m,
  4. p puts the content of the unnamed register after the cursor, see :help p,
  5. !Ggrep foo<ENTER> filters the lines from the current line to the last line with the external command grep foo, see :help !,
  6. 1G moves the cursor on line 1,
  7. :1,'a g/foo'/d cuts every line matching foo' from line 1 to mark a, see :help :range, :help :g, and :help d.

Step 2 is, I believe, a typo. !GG literally means "filter the text from the current line to the last line with external command G", which makes no sense because a) G is not a common Unix command (if it even exists), and b) it is not followed by <ENTER>, which is required for executing an Ex command.

What that step is supposed to do is described in the answer's body: copy the content of the buffer. It can be done in many ways but:

yG

to yank from here to the last line, followed by:

G

to put the cursor on the last line, seems more fitting in this context. So the whole sequence should actually be:

1GyGGmap!Ggrep foo<ENTER>1G:1,'a g/foo'/d<ENTER>

which works in Vim and in vi.

--- EDIT ---

Note that there is still some room for improvement. The following sequence has the same outcome but without unnecessary cursor movements:

:$ka|r!grep foo %<ENTER>:1,'ag/foo/d<ENTER>

and it also works in ex ;-).

Another optimised version that kind of does the opposite of what the answer tries to demonstrate (top of file as scratch area instead of bottom of file) but with the exact same outcome:

:0r!grep -v foo %<ENTER>:+,$v/foo/d<ENTER>
  • no normal mode cursor motion,
  • no marks.

But, to be honest, the problem doesn't really require such a convoluted solution as it can be done with a simple:

:g/foo/m$

which works in ex, vi, every vi clone, and even in ed.

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