How to quickly change several lines at once

2007-01-24 01:05:48 UTC

It’s 130.87 K, and just over four seconds, and will play under any version of QuickTime.

It shows me using vim to replace an old variable name (“fullScreenOverlayWindow”) with a new one (“panel”). The sequence of events:

  1. I move into position.
  2. I enter Visual Block mode by pressing ctrl-v.
  3. I jump to the end of the current word with e.
  4. I jump 7 lines down with 7j. This gives me a total of 8 lines selected (including the one I started on).
  5. I enter Change mode by pressing c.
  6. I type my shiny new variable name. (Notice that it only displays on the line where my cursor is; all the others stay blank for the duration of my Change.)
  7. I exit Change mode by pressing escape.
  8. I wait a beat.
  9. Voila!

Cocoa text editors like TextEdit and Xcode have rectangular selection, but not rectangular editing; you will obliterate all the lines, but only insert your new text into the first line. This is not useful.

4 Responses to “How to quickly change several lines at once”

  1. Sören Nils 'chucker' Kuklau Says:

    Cocoa text editors like TextEdit and Xcode have rectangular selection, but not rectangular editing; you will obliterate all the lines, but only insert your new text into the first line. This is not useful.

    Indeed, Cocoa doesn’t normally offer this. The two popular Cocoa developer’s text editors, SubEthaEdit and TextMate, both do, however.

  2. Jesper Says:

    Sören: That’s correct, except that there’s three popular Cocoa developer’s text editors, and the biggest one of them is Xcode. ;)

  3. David Paul Robinson Says:

    Great video, thanks!

  4. Nicholas Riley Says:

    Languages like Smalltalk let you send multiple messages to the same object without having to type the name of the object repeatedly. You’d think Objective-C would have learned something after all this years :-)

    But yeah, rectangle editing is useful. I only learned about the rectangle commands in Emacs about a year ago, and they’re great, even if they don’t provide the same highlighting and feedback as vim does. You’d do the above with:

    1. C-SPC, to set the mark.
    2. M-f (or C-right), to go to the end of the word.
    3. M-7 down (or M-7 C-n for those people who hate arrow keys :-), to go down 7 lines.
    4. C-x r t for “string rectangle”, i.e. “replace rectangular selection”
    5. Type “panel”, hit return.

Leave a Reply

Do not delete the second sentence.