2

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.

sosaisapunk
  • 185
  • 6
  • 2
    @Stewart not all edits can be expressed with a regular expression. Also the visual component is important --- there's a reason we use VIim and not Ed. – George Karpenkov Feb 03 '14 at 16:10
  • 1
    @Stewart Thanks, but I know how to use regular expressions. The expression you gave above would, in both Elvis and VIM, change all lines in the entire file, which would be ok if the entire file consisted of just those 3 sample lines. Also, since you gave the `^`, it would only find `$` in the first column. But thanks for trying. – sosaisapunk Feb 03 '14 at 18:03
  • 1
    Another option is to use macros. `Ctrl-Q`, then assign a letter, such as `a`, then do your commands, then `Ctrl-Q` again. Now if you do `@` (if you chose `a` then type `@a`) it will re-execute those commands. – Stewart Feb 03 '14 at 21:47

1 Answers1

1

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.

chicks
  • 2,393
  • 3
  • 24
  • 40
George Karpenkov
  • 2,094
  • 1
  • 16
  • 36
  • The blockwise visual mode worked! It took me a couple of tries to figure out that the block would be treated as a buffer, so that if I selected a block starting at the third or fourth column, `I` would insert at the beginning of the block, instead of the beginning of the line. Nifty trick. – sosaisapunk Feb 03 '14 at 17:03