3

I have a template.php file where I want to change every occurence of apple with grape throughout the file. At the moment,I am having to painstakingly edit the entire file to find where to change. Sometimes the name is like this in the file, apple_button and sometimes it is just apple.

I am using vi to edit.

  • That's very basic vi / Vim functionality, covered by most tutorials on the web. Also, learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor. – Ingo Karkat Mar 28 '14 at 14:49
  • Thanks, yes I couldn't actually live without it. Unless I stopped doing computer stuff :) – cea Mar 29 '14 at 4:02
2

Open the file template.php using vi editor. After opening it, press escape button and type below.

:%s/apple/grape/g
  • Brilliant :! so this is just a vi editor command? – cea Mar 27 '14 at 4:00
  • It is just the vi editor that you use in Linux environments. – Ramesh Mar 27 '14 at 4:01
  • Glad that I could help you again :) – Ramesh Mar 27 '14 at 4:02
1

You should use sed:

sed -i.bak 's/apple/grape/g' template.php

If you use vi, you can do in command mode:

:%s/apple/grape/g
1

try use the command sed to change.

sed -e 's/apple/grape/g'  filethatyouwantchange

this will just show the output , if you want to change the file use the parameter -i:

sed -i -e 's/apple/grape/g'  filethatyouwantchange

if you are using vi or vim you can do the same thing with the file open

:%s/apple/grape/g
  • thanks but Ramesh beat everyone to it. – cea Mar 27 '14 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.