tags:

views:

50

answers:

3

I'm looking for a command in VI/VIM to search for particular text in the file and grab the whole line and output in either on the screen or separate file. e.g.

This is some line with this _word_ and some other text.
This is some line with this some other text and some other text.

so this would output only the first line..

A: 

in vi, from command mode

search for _word_

 /_word_

yank the line

yy

paste the line

p
Zak
well, I have several thousands of lines, I wanted to do it in a more streamlined fashion. similar to DOS command: find /i "String" "c:\my src\myfile.txt"
ra170
then use grep as the original commenter suggested
Zak
Not sure how :P...
ra170
+3  A: 
:redir > output.txt
:g/_word_/p
:redir END

The line will be output to the screen and to output.txt. See :h :redir.

EDIT: I agree with others who suggest using plain old *nix grep if you can. Situations where you might not be able to use grep:

  • You're searching buffer text that doesn't exist in a file on the filesystem.
  • You're using Vim-specific regex extensions, like cursor location, column number, marks, etc.
  • You want do this in a cross-platform way, and grep might not exist on the system you're using.

redir can be useful in these situations.

Brian Carper
Btw. do you know how to redir to C:\somepath\ under cygwin?
ra170
Something like `/cygdrive/c/somepath`.
Brian Carper
+1  A: 

use the g (global) command:

:g/_word_/y

will yank all lines containing _word_


having mentioned the DOS find command, you probably want to use grep:

grep -h '_word_' * > results
knittl
i bow to your superior vi mastery
Zak