Samselvaprabu
Samselvaprabu

Reputation: 18147

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.

: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?

Upvotes: 1

Views: 73

Answers (2)

jeremysprofile
jeremysprofile

Reputation: 11435

Edit: @romainl's answer is strictly better and should be preferred for what OP is trying to do.

If all you're trying to do is copy the line containing "fatal:" and a couple of lines afterward, you can do:

/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.

Upvotes: 0

romainl
romainl

Reputation: 196556

Like all ex commands inherited from vi and ex, :help :t can take a :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,

which gives you this command:

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

Note that . is implied and + is a shortcut for +1, so it can be shortened to:

:g/fatal:/,+t$

Upvotes: 6

Related Questions