Programming in Vim: What specific mechanism is used * to use copy-paste variables?

Note

This has been noted as a potentially subjective issue, but it is not subjective. They asked me to study the various specific ways that people use Vim so that a person who comes from a text editor with a mouse and keyboard can learn more about editing Vim.

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

This is a specific question about mechanical steps. that you could get to get the result in the Vim editor using alternative editors as the baseline for cross-referencing.

PROBLEM

Suppose you have the following code in your Vim, and you want to switch from it before it looks like this:

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

... and after that it looks like this ...

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

HOW INSUFFICIENT EDITORS WILL DO IT

Using the non-vim editor, programmer A simply copies the first line for alice, pastes it several times into the editor, and then re-edits the values ​​to replace alice and alpha with the corresponding values ​​by editing one line at a time.

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

What about VIM?

Given that Vim is a significantly different approach from the mouse and keyboard editors of the day, this is a request to understand the specific steps that are taken when editing with Vim. Obviously, you can simply type each line individually, but it is assumed that there is a time-saving method in Vim that compares with what programmer A and programmer B did above.

1) How will the vim programmer begin this editing operation using a time-saving method similar to the one above?

2) If someone had to search the Internet for more examples of specific β€œstep-by-step” comparisons of Vim editing sessions and mouse and keyboard style editing, what would you look for?

+4
source share
3 answers

I use the same, first copy the string. then pasting it at any time that I need.

Then you can create a macro to edit the keys. When the cursor is on the first line, where do I need to work. (sticker with frist)

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

You can then play your macro at any time by @q. (I have a map Q = @q to quickly run a macro on Shift + q) Similarly, you can use for values:

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

Macro to search for a block of values ​​and switch to insert mode.

And the answer for comparison, I will save time to move my hand from the keyboard to the mouse time = the number of lines of editing. Select a block to change. Vim is more productive, no doubt.

+6
source

If I know in advance what the different values ​​will be, I will approach a circular approach. I will start with this:

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

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

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

+3
source

I like the solution of AlexRus (I love Vim macros).

But I think a more realistic situation would be to insert key / value pairs from another application / document:

 betty bravo cindy charlie deana delta 

and perform a sequence of conversions on each line.

SOLUTION 1

We could select all three lines using <Sv>jj or some other way and apply a series of search / replace in the selection:

 :'<,'>s/^/$mynames[' gv to reselect :'<,'>s/ /'] = ' gv to reselect :'<,'>s/$/'; 

The whole editing sequence looks like this:

 <Sv>jj:s/^/$mynames['<CR>gv:s/ /'] = '<CR>gv:s/$/';<CR> 

SOLUTION 2

We could apply one search / replace

 :'<,'>s/^\(.*\) \(.*\)$/$myname['\1'] = '\2'; 

where the search part isolates the beginning of the line ( ^ ), the space between words ( ) and the end of the line ( $ ), actually matching the text between them and the replacement part, replaces the entire line with $myname[' + first match ( \1 ) + '] = ' + second match ( \2 ) + '; .

I'm not good at regex, so I had to check my notes to put them together, but I have no doubt that many Vim users can type this command at a time. I will ever be.

The whole editing sequence looks like this:

 <Sv>jj:s/^\(.*\) \(.*\)$/$myname['\1'] = '\2';<CR> 

SOLUTION 3

With the same setup, we could enter VISUAL-BLOCK mode at the beginning of the first line using <Cv> , skip as far as possible and type I$myaccess['<Esc> to get:

 $mynames['betty bravo $mynames['cindy charlie $mynames['deana delta 

move the cursor to the space between words with f<Space> , press <Cv> again, expand the selection below and type c'] = '<Esc> to get:

 $mynames['betty'] = 'bravo $mynames['cindy'] = 'charlie $mynames['deana'] = 'delta 

then move to the end of the line with $ , press <Cv> again, select what you want again, and enter A';<Esc> for the last touch.

The whole editing sequence looks like this:

 <Cv>jjI$myaccess['<Esc>f <Cv>jjc'] = '<Esc>$<Cv>jjA';<Esc> 
+1
source

Source: https://habr.com/ru/post/1380442/


All Articles