2

I would like to add to my vimrc a custom command (:customFilter {pattern}) for filtering text and placing result in a new tab.

I tried using :global or :vimgrep /{pattern}/ % but the problem is that I would like to redirect this result of global or content of Quickfix list (without name of file) to a new tab.

Please help.

Example

my file:

class TemperatureNode {
    + uint8_t getTemperatureValue()
    + void setTemperatureValue(uint8_t newVal)
    + uint8_t getHumidityValue()
    + void setHumidityValue(uint8_t newVal)
}

and then I call :customFilter void and get:

+ void setTemperatureValue(uint8_t newVal)
+ void setHumidityValue(uint8_t newVal)

Having this with line numbers from the filtered file would be even better.

  • What do you want to do with that new buffer? – romainl Aug 21 '15 at 7:39
  • This would be mainly for searching through the logs, so having few buffer/tab with results of filtering would allow me to debug more efficiently (huge app, syslog debugging). But if you're asking more literally- I want to save this buffer/tab to a file. – lewiatan Aug 21 '15 at 8:05
4

You can create a new empty buffer in a new tab and then populate it with the result of a grep search on the previous file:

:tabnew|0r!grep -n pattern #

In a mapping:

nnoremap <key> :tabnew\|0r!grep -n  #<Left><Left>

Which works like this:

enter image description here

--- edit ---

Here is an alternative command that works with the current buffer instead of the current file:

:%y z|tabnew|0put=@z|%!grep -n pattern

Explanation:

:%y z                 " yank the whole buffer in register z
:tabnew               " edit a new buffer in a new tab
:0put=@z              " put the yanked text in that new buffer
:%!grep -n pattern    " do your filtering

As a command:

:command! -nargs=1 -range=% Filter <line1>,<line2>y z|tabnew|0put=@z|%!grep -n '<q-args>'

Usage:

:Filter pattern        " works on the whole buffer by default
:5,20Filter pattern    " works on the supplied range
  • This is great but has a flaw- it only works with existing files and doesn't work with new buffer/tab. Do you have any idea how to make this work for unsaved files? And one more question what does #<Left><Left> and the # itself do? EDIT: I get this <Left><Left> - it's just moving cursor for it to be in the right place to insert "pattern" – lewiatan Aug 21 '15 at 18:17
  • # represents "the alternate file" (usually it's the previous buffer). It is expanded by Vim before sending the command to the shell. – romainl Aug 21 '15 at 19:19
  • Ok. Thanks. I tested it in gVim but I get "shell returned 1" and an empty buffer, but similar :command! -nargs=1 Filter %y z|tabnew|0put=@z|%!grep -n <args> works perfect (a little bit slow though). That's what I needed! Thanks again. – lewiatan Aug 22 '15 at 10:47
5

First clear an register: qaq second, append all matching lines using the :g command:

:g/pattern/y A

Third, put all matches on a new page

:tabnew +$put\ A

If you want to be able to make changes on those filtered mached, have a look at my NrrwRgn Plugin. The advantage is, once you save the narrowed buffer, the changes will be taken back into the original buffer.

  • ok. this works. but I don't get what qaq does? it simply starts recording macro and then leave it. what's the purpose here? – lewiatan Aug 21 '15 at 8:07
  • As I said, clear a register – Christian Brabandt Aug 21 '15 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.