Question

I've been using Slackware Linux since very nearly the first version. I've gotten very used to the Elvis Editor over the years, but now am trying to switch to Ubuntu (for updates, ease of finding packages, etc.). One the thing that's taken the most effort to get used to is VIM. I love some of the features, but there is one feature in Elvis that I can't seem to duplicate in VIM.

Take, for instance, the following PHP code:

$A = 2;
$B = 3;
$C = 4;

Now, say that I'd like to put these vars in a class named MyVars. That would change the above block of code to:

$MyVars->A = 2;
$MyVars->B = 3;
$MyVars->C = 4;

In Elvis, I would insert MyVars-> before the A and hit ESC, then move down one line, left-arrow to the the B, hit . to repeat the command, and when I move the cursor down again, the cursor would already be ready to insert again directly in front of the C. This is a big time saver, especially when there is a list of 20 or 30 vars that need to have the same change done. In VIM, the cursor doesn't behave like this. The after the second down-cursor, the cursor ends up on the ; after the 4, which means that I need to hit left-arrow 5 times to get the cursor back to the needed position. This could literally be 20 or 30 times, depending on the line that needs to be edited.

I can't seem to find anything in the documentation for either editor that will change this.

Any help would be appreciated.

Was it helpful?

Solution

I'm not sure whether it is possible to achieve exactly what you want from the Vim cursor (without changing Vim too much, that is), however I do know how to achieve what you want.

Usually this is done using the blockwise visual mode. E.g. to perform the refactoring needed for your example you would:

  • Select all three lines using Ctrl-V to enter the visual mode.
  • Press Shift-I to enter the insert mode
  • Change the first entry
  • Leave the visual mode. The change will be applied to all rows.

I think this is more efficient then the solution offered by Elvis.


If you use plugins an even better approach is multiple-cursor mode. Add a cursor per each row, perform the same operation on each one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top