Pages

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.

No comments: