简体   繁体   English

如何在vim中将错误行和更多行复制到文件末尾?

[英]How to copy error line and few more lines to end of file in vim?

I find fatal errors in my ansible log and copy them to end of file to see all the fatal errors.我在 ansible 日志中发现致命错误并将它们复制到文件末尾以查看所有致命错误。

:g/fatal:/t$

But I would like to copy error and next line ,because next line some time would be 'ignoring' which need not to be considered.但我想复制错误和下一行,因为下一行有时会被“忽略”,不需要考虑。 I can find only real fatal errors which is required我只能找到真正需要的致命错误

How to move pattern and few more lines?如何移动图案和更多的线条? In grep we can find -A -B ,is there any way in g command to do it?在 grep 中我们可以找到 -A -B ,在 g 命令中有什么方法可以做到吗?

Like all ex commands inherited from vi and ex , :help :t can take a :help :range .像所有从viex继承的 ex 命令一样, :help :t可以采用:help :range In this case, you want to operate on the matching line and the line below, which translates to the following range:在这种情况下,您希望对匹配的行和下面的行进行操作,这将转换为以下范围:

.,+1

where:在哪里:

  • the left-hand side of the comma is the first line,逗号的左侧是第一行,
  • the right-hand side is the last line,右边是最后一行,
  • . represents the current line,代表当前行,
  • +1 represents the line below the current line, +1代表当前行下方的行,

which gives you this command:这给了你这个命令:

:g/fatal:/.,+1t$

Note that .请注意. is implied and + is a shortcut for +1 , so it can be shortened to:是隐含的, ++1的快捷方式,因此可以缩短为:

:g/fatal:/,+t$

Edit: @romainl's answer is strictly better and should be preferred for what OP is trying to do.编辑:@romainl 的答案绝对更好,应该是 OP 尝试做的首选。

If all you're trying to do is copy the line containing "fatal:" and a couple of lines afterward, you can do:如果您要做的只是复制包含“fatal:”的行,然后再复制几行,您可以执行以下操作:

/fatal:<CR>y<motion> , where <CR> is Enter and <motion> can be, for example, 2j for "this line and the next line" or G for "from this line to the end of the file", etc. /fatal:<CR>y<motion> ,其中<CR>Enter<motion>可以是,例如, 2j表示“这一行和下一行”或G表示“从这一行到文件末尾” “, 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM