This morning I finally got tired of opening a file from ftp in ultraedit just to replace a few words on our AIX systems. I knew there was an easy way to do this directly in the vi editor, but just had not taken the time to research. I finally did, and it is pretty easy, although similar to many unix commands it is not very user friendly in it's context. Here are the basics for replacing a string of text with another string of text in the vi editor:
:s is the command for substitute and you follow that with the string you want to replace and the new string separated by "/". For example, if I wanted to replace the word "left" with "right" I would enter the following command in vi:
:s/left/right/
Now, I don't really find that command very useful because it only searches the current line and only replaces the first instance of the word "left". IF you want to search the entire file, the "%s" command takes care of that:
:%s/left/right/
While that searches the entire file, it still only replaces the first instance of the word "left" that it finds. What I really want to do most often is replace all instances of a word in the file. Going back to the example above, the command to replace all instances of the word "left" would be the "g" option at the end of the command:
:%s/left/right/g
There are plenty of more options that make the substitute command even more powerful, but those are the basics and what I find myself using the most.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment