Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming in Vim: What specific mechanism do *you* use to 'copy-paste' variables?

NOTE

This was flagged as a potentially subjective question, but it is not subjective. It is requested to learn the various specific ways that people use Vim so that a person coming from a "mouse-and-keyboard" oriented text editor might learn more of the Vim way of editing.

This is not a subjective question about personal preferences or which editor or editing style is best.

This is a specific question about the mechanical steps one would take to obtain an outcome in the Vim editor, using alternative editors as a baseline for cross-reference.

PROBLEM

Suppose you have the following code in your Vim and you want to get from before to after where before looks like this:

// Before //

$mynames    = Array();
$mynames['alice'] = 'alpha';

... and after looks like this ...

// After //

$mynames    = Array();
$mynames['alice'] = 'alpha';
$mynames['betty'] = 'bravo';
$mynames['cindy'] = 'charlie';
$mynames['deana'] = 'delta';

HOW NON-VIM EDITORS WOULD DO IT

Using a non-vim editor, programmer A would simply copy the first line for alice, paste it multiple times into the editor and then re-edit the values so that alice and alpha are replaced with the appropriate values, editing one line at a time.

Using a non-vim editor, programmer B would create a rectangular selection that spans four lines, and just start typing the common text $mynames[''] = ''; and then go back and fill in the appropriate values, editing one line at a time.

HOW ABOUT VIM?

Given that Vim is a significantly different approach from "mouse-and-keyboard" style editors of the day, this is a request for insight on the specific steps one takes in editing with Vim. Obviously, it is possible to just type each line individually, but it is assumed that there is a time-saving way to do this in Vim in a way that compares to what programmer A and programmer B did above.

1) How would a vim programmer go about doing this edit operation, using a time-saving method like those above?

2) If someone were to search the Internet for more examples of specific 'step-by-step' comparisons of Vim editing sessions vs "mouse-and-keyboard" style editing, what would one search for?

like image 664
dreftymac Avatar asked Dec 28 '22 11:12

dreftymac


2 Answers

I use the same, first i copy a line. then pasting it any times what i need.

Then you can create a macro to edit a keys. When cursor is on first line where i need to work. (a frist pasted line)

    qq f[ci'<C>-<o>q  "recordes a macro to find a [block] and 
change inner quotes ' and stays in insert mode

Then you can play your macro any time by @q . (I have a map Q = @q for fast macro start by Shift+q) The same way you can use for values:

 qq f=f'ci'<C>-<o>q

Macro for find a value block and go to insert mode.

And the answer for comparsion i will save time to move my hand from keyboard to mouse times = number of edit lines. Selecting a block for changing. Vim is more productive no doubt.

like image 84
RusAlex Avatar answered May 14 '23 20:05

RusAlex


If I know ahead of time what the different values are going to be, I'll to the roundabout approach. I'll start with this:

$mynames    = Array();
alice alpha
betty bravo 
cindy charlie
deana delta

Then I will place my cursor in front of alice, hit Ctrl+V, move down to deana, then hit Shift+I to go into insert mode and type $mynames[' followed by Esc. This inserts the text in all selected lines. Then I repeat that for '] = ', followed finally by ';'

Not the most efficient way, but usually the first that comes to mind.

like image 32
rossipedia Avatar answered May 14 '23 20:05

rossipedia