31 October, 2009

Filter file content in Vim

The following is a simple method to do a 'grep' in the Vim buffer, without do it in the shell.

Consider the following file content:

  public class SomeThing {
    // some constant
    final static public String CONSTANT_A = "a";
    // another constant
    final static public String CONSTANT_B = "b";
    // yet another constant
    final static public String CONSTANT_C = "c";
  }

To filter out the constant definition lines ('final static public ...'), you can use a 'g' command in Vim to filter lines by regex and append to a register 'x'.

:g/final static public/normal "Xyy

Then, paste the register in somewhere you need it. For example, to put it in new buffer.

No comments: