Unix Power ToolsUnix Power ToolsSearch this book

17.15. Counting Occurrences; Stopping Search Wraps

Want to see how many times you used the word very in a file? There are a couple of easy ways.

First, tell vi to stop searching when you get to the end of the file. Type the command :set nowrapscan or put it in your .exrc file (Section 17.30).

  1. Move to the top of the file with the 1G command. Search for the first very with the command /very (HINT: using the word-limiting regular expression /\<very\> (Section 32.12) instead will keep you from matching words like every). To find the next very, type the n (next) command.

    When vi says Address search hit BOTTOM without matching pattern, you've found all of the words.

  2. Use the command:

    :g/very/p

    The matching lines will scroll down your screen.

To find the line numbers, too, type :set number before your searches.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.