domlao
domlao

Reputation: 16029

Search a string in vim and output like grep

How can I search for a particular strings in a text file using VIM then it will output all the lines with the instance of the search string?

Please advise.

Thanks!

Upvotes: 2

Views: 3829

Answers (4)

Juan Lanus
Juan Lanus

Reputation: 2344

After searching for, say, "qqq", I see only the lines containing said string with the command:

:v//d

"v" matches the lines not containing the previous search ("//") and "d" deletes them. It's a negative "g".
I get a clean view of only the lines I want. If I'm interested in a particular one I mark it with, say, "mm".
When I'm done viewing them a use "u" to undo the delete and get my file back (need not forget).
After, I move among those lines with "n" or go to the marked one with "'m".

Upvotes: 0

romainl
romainl

Reputation: 196886

In addition to the :g[lobal] command in Mike's answer, here are a couple of alternatives:

  • :il[ist] foo lists all lines containing foo that don't look like comments in the current buffer (use :il[ist]! if you want to match comments as well).

    When the list is displayed you can hit <CR> to make it disappear or hit : to issue Ex commands: :5<CR> jumps to line 5, :5t3 copies line 5 after line 3 and so on.

    Note that the list is transient: you must re-issue the command if you want to see that list again.

    Usage:

    :il foo<CR>
    :23<CR>
    
  • :vim[grep] bar % | cw[indow] populates the quickfix window with all the lines containing bar in the current buffer.

    Read :h quickfix to know how to use it. Quickly, hit <CR> to jump to a result, :cn to jump to next result, :cp to jump to the previous result…

    Usage:

    :vim bar % | cw<CR>
    (navigate to the result you want in the quickfix window 
     with arrows, `jk`, line numbers, search…)
    <CR> 
    

    You can also use :grep bar % | cw[indow] which uses your system's grep instead of Vim's internal grep-like methods.

    Note that the content of the quickfix window won't change until the next :vim or :grep or :make or whatever command that manipulates the error list. It means that you can hide the quickfix window and open it back again with the same list.

Upvotes: 3

Kent
Kent

Reputation: 195259

There is another possibility to do it with :g

say you want to get all line in your current file with word foo :

you could type:

qzq:g/foo/y Z Enter

after executing the above line, those lines are stored in your register z. you could "zp to paste somewhere, or do later processing.

what does what shortly:

qzq : clear the z register

:g/foo/y Z : yank all matched lines to register z

Upvotes: 4

user849425
user849425

Reputation:

You can use Vim's built-in global tool. It will output the lines for all matches at the bottom of the window. An example of this is: :g/searchterm/Enter

You might find it helpful to turn on line numbering if it isn't already on to get a perspective on where in the file your matches are. The command to do this is: :set numberEnter

Additional info can be found by running the command: :help globalEnter

EDIT:

If you want to redirect the results into its own window, vim.wikia.com recommends using the command sequence

:redir @a         redirect output to register a
:g//              repeat last global command
:redir END        end redirection
:new              create new window
:put! a           paste register a into new window

You can script this quite easily as a vimscript function. I highly recommend you check out the Steve Losh - Learn Vimscript the Hard Way tutorial if you have never seen it to learn how to properly script Vim functions.

Upvotes: 5

Related Questions