next up previous contents
Next: Editing Multiple Files Up: Guide to VI and Previous: Using Markers

Global/Limited Search/Replace

To perform text search or replace we must use ex commands by typing:
:<command><ret>
The command appears at the bottom of the screen.
To execute more ex commands in sequence, it is usefull to switch from vi to ex mode, to obtain the ex : prompt.


Q switch from vi to ex mode
vi switch from ex to vi mode


When ex mode is entered from vi Q command, it is possible to execute ex commands on one or more lines. In general single ex commands can be entered with the : colon prefix. Commands that require entering \ can be entered only when in ex mode (Q command) and are platform dependent.


Advanced Pattern Matching Syntax
Q Commands
:%s/[space tab][space tab]*/\ split whole file in one word lines
:%s/$/\ double spacing text
:g/pattern/p search and print pattern in whole file
:g!/pattern/p print all lines NOT including
  pattern in whole file
:init,end g/pattern/p search and print from init
  to end line in file
:/pattern1/,/pattern2/p search and print from pattern1
  to pattern2


In ex commands g means global, p means print. In the Advanced matching table are shown the following features.


:[space tab][space tab]* regular expression that means:
  any <space> or <tab> followed by zero or more
  <space>s and/or <tab>s
% operate on whole file; synonim of 1,$
/$/ end of line


All matching lines are printed on screen and the following message appears on the bottom line:
[Hit return to continue]
Cursor moves to last matching pattern found.

A complete overview of pattern searching with or without text substitution is given in the Special and Common pattern handling tables.


Special Pattern Searching Syntax
:g/pattern/s/old/new/g search pattern in whole file and change old to new
  globally on that line
:%s/[space tab]*$// remove blanks and/or tabs at EOL in whole file
/^pattern search pattern at BOL (beginning of line)
/pattern$ search pattern at EOL (end-of-line)



Common Pattern Substitution Syntax
:s/old/new change only first occurrence of old to new on
  current line
:s/old/~ repeat previous change on another line
:& repeat previous substitute command
:s/old/\unew change only first occurrence of old to New on
  current line
:s/old/\lNEW change only first occurrence of old to nEW on
  current line
:s/old/\Unew change only first occurrence of old to NEW on
  current line
:s/old/\LNEW change only first occurrence of old to new on
  current line
:s/old/new/g change every occurrence of old to new on current line
:50,100 s/old/new/g change every occurrence of old to new from line
  50 to line 100
:% s/old/new/g change every occurrence of old to new in whole file
:% s/old/new/gc as previous command but with confirm. It displays
  entire line where string has been located, string will
  be marked by a series of ^^^^. answer y to make
  replacement, <ret> for no replacement
:s repeats last substitution


Recognized colon command for init end/or end :


:$ last line in file
:. current line
:% abbreviation for 1,$ (whole file)
:g whole file
:n nth line in file
:.-n nth line before current line
:.+n nth line after current line
:n,m from line n to line m
:.-n,.+m nth preceding line to mth following line


To visualize non-printing control characters hidden in a file:


:l $ indicates EOL, ^I indicates tab.


To visualize tabs and EOL for whole file:


:set list  
:set nolist toggle back to normal mode



next up previous contents
Next: Editing Multiple Files Up: Guide to VI and Previous: Using Markers
Marisa Luvisetto
2000-12-20