Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeatedly add text on both sides of a word in vim?

Tags:

vim

vi

editor

I have a bunch of local variable references in a Python script that I want to pull from a dictionary instead. So, I need to essentially change foo, bar, and others into env['foo'], env['bar'] and so on. Do I need to write a regular expression and match each variable name to transform, or is there a more direct approach that I could just repeat with the . command?

like image 497
Judge Maygarden Avatar asked May 31 '11 19:05

Judge Maygarden


People also ask

How do I put multiple lines of text in Vim?

vim Inserting text Insert text into multiple lines at oncePress Ctrl + v to enter into visual block mode. Use ↑ / ↓ / j / k to select multiple lines. Press Shift + i and start typing what you want. After you press Esc , the text will be inserted into all the lines you selected.

How do you add a word at the end of each line in Vim?

In a visual block, you can insert text in each line before the selection with I , and you can append text in each line after the selection with A . If you use $ to convert the visual selection to select to the end of line, then A will append text to the end of each line in the visual block.

How do I add text to the end of a line in vi?

Type A to add text to the end of a line. To see how this command works, position the cursor anywhere on a text line and type A . The cursor moves to the end of the line, where you can type your additions. Press Esc when you are finished.


1 Answers

You can use a macro: type these commands in one go (with spacing just to insert comments)

             " first move to start of the relevant word (ie via search)
qa           " record macro into the a register.
ienv['<esc>  " insert relevant piece
ea']         " move to end of word and insert relevant piece
q            " stop recording

then, when you're on the next word, just hit @a to replay the macro (or even @@ to repeat the last replay after that).

like image 68
Peter Avatar answered Nov 02 '22 23:11

Peter