3

I have two lines in editor as you can see below

3  àáâ
4  aaa

Based on these two lines, I'd like to get the result below

'à' => 'a',
'á' => 'a',
'â' => 'a',

Any ideas?

5

If you have repetitive tasks to do, you can record a macro.

For example here,

qajxkphi'Escla' => 'Escla',Enter Escq

Explanations:

  • qa : start recording macro a
  • jxkp: go down one line, erase-copy one character, go up, print it
  • hi'Esc : go left, insert one ', go back to normal mode
  • la' => ',Esc : go right, append after the current character ' => ', go back to normal mode
  • la'EnterEsc : go right, append ', and a newline, and go back to normal mode.
  • q : stop recording

To use the macro (and confirm that it works), place yourself on the first character and press @a. The result is:

'à' => 'a',
áâ
aa

and you're in the second line. Press 2@a to execute the macro twice and get:

'à' => 'a',
'á' => 'a',
'â' => 'a',
2

Place the below 3 map-pings in your ~/.exrc file and then load the file in vi|vim|gvim you wanted edited. Bring the cursor anywhere on the line where you want the transformation, and just depress the Q key in ESC mode of vi and this recursive macro will do the rest. N.B.: the ^M are not to be literally and they are inserted into the file by pressing the following keycombo: <Ctrl><V> <Ctrl><M>

:map q :s/./&\r/^M
:map v :s/\(.\) \(.\)/'\1' => '\2',/^M
:map Q :/^^M-q+q-dd-P-Jv+Q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.