I have a script that I am editing with vi. Is there a simple way to change all the occurences of a word to another word? For example if the word "dog" appears 15 times, how can I change all 15 occurences to cat?
:s/RE/string/opt Search-and-replace; string is the replacement. Use opt to specify options c (confirm), g (globally on each line), and p (print after making change).
so if you wanted just to change lines 1 thru 30 you could do
:1,30s/dog/cat/g
Mike
"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
As a general rule the vi editing commands are the sed editing commands - their both derived from ed. One difference I have found is that braces need to be escaped. i.e.
Code:
:%s/ \{4,20\}//g
will delete any block of 4 - 20 spaces anywhere in the file. Note that the pattern is greedy. i.e. 22 spaces will be seen as 20 spaces followed by 2 spaces, not 4 spaces followed by 18 spaces.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.