Pages

Showing posts with label vi. Show all posts
Showing posts with label vi. Show all posts

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

Sunday 20 November 2011

Vi Tip: Prepending Text to multiple lines


There are cases when you want (for many reasons), to add same text to the beginning of multiple lines. This may happen when you are coding (say python) and then you realise that you need to enclose your function within a class. So you add the class definition at the start of the module file. You then need to indent the function, as python requires. To indent you need to add four spaces (or your preferred number). In vi, you do this like so;

  1. Go to the starting line that you intend to prepend your desired character(s).
  2. Press Ctrl + V
  3. Press the down arrow or Enter/Return key to the last line that you intend to prepend your desired character(s)
  4. Press Shift + i (lowercase i )
  5. Enter the character(s). In our python example, this would be the desired number of spaces (I usually use four)
  6. Then press the Esc key one.
  7. Your entered characters should appear in a few seconds.