0

I have several instances of variables in a file and I need to modify the variable and append "new" to each of the names.

var Modelfrfr = ....

var Modeldede = ...

var Modelenen = ...

change it to

var ModelfrfrNew = ....

var ModeldedeNew = ...

var ModelenenNew = ...

What is the best way to do it?

2 Answers 2

2

Kind of this

%s/Model\a\+/&New/g

See :h :s, :h /\a, :h sub-replace-special and so on.

2
  • Maybe change Model to var to be more generic?
    – Vee
    Nov 30, 2020 at 21:30
  • @Vee That depends on use case.
    – Matt
    Dec 1, 2020 at 6:21
0

With the cursor on the first line at the first column, the command t=<C-v>2jINew<Esc> where:

  1. t= moves the cursor to immediately before the first =.
  2. <C-v> for visual block mode.
  3. 2j to select the two lines beneath. Replace the 2 with any number or press j until the cursor has selected the desired number of lines.
  4. I for insert mode.
  5. New for the desired text to append to all of the variables.
  6. <Esc> to exit insert mode and apply the text to all lines in the selection.

Edit

In retrospect, that will only work for a solid block of variables. If they're spaced throughout a file, maybe a macro? qq/var<CR>eeaNew<Esc>q

  1. qq to start the macro in register q
  2. /var<CR> to search for var.
  3. ee to move to the end of the variable name.
  4. aNew to append "New".
  5. <Esc>q to exit insert mode and stop recording the macro.

Repeat with @q as needed.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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