Pages

Wednesday, 15 May 2013

Vi Tip: Search and Replace

 Search and Replace Text in Specific Range

There are times when you would like to change text within a specific range:
First show the line numbers using;

:set nu

Then do the search and replace task like so:

:start_line,end_lines/text_to_be_searched_for_and_replaced/replacing_text/g

Example:


100 
101     def _set_attr(j_patient):
102         self.id = j_patient.pop("patient.id", None)
103         self.voided = j_patient.pop("patient.voided", False)
104         self.created_by = j_patient.pop("patient.created_by", None)
105         self.created_on = j_patient.pop("patient.created_on", None)
106         self.modified_by = j_patient.pop("patient.modified_by", None)
107         self.modified_on = j_patient.pop("patient.modified_on", None)
108 
109     def _set_patient(self, patient):
110         patient.id = self.id
111         patient.voided = self.voided
112         patient.created_by = self.created_by
113         patient.created_on = self.created_on
114         patient.modified_by = self.modified_by
115         patient.modified_on = self.modified_on
116     


So, to change the word patient to person only under the _set_patient function, do this:

:109,115s/patient/person/g

Friday, 29 March 2013

Modifying multiple files in one stroke



The other day, I was working on code and felt that I had to change the date creation function and make use of SQLAlchemy's server_default = text("sysdate") from a rather bizare default = datetime.now()

so I run this;

sed -e 's/default = datetime.now()/server_default = text("sysdate")/g' -i models/*.py

and all python files were updated

Note: the letter g at the after the replacement text ensures that all instances of the
text to be replaced are actually replaced and not only the first instance if the g (global) were left out