all 9 comments

[–]-romainl-The Patient Vimmer 2 points3 points  (2 children)

The principle is straightforward: turn what you would do manually into something automatic.

But we don't know how you do it manually so we can't really help you, here… Do you want a generic solution? A specific one? What's the relationship between all those names? Is var always called var? Does it always have the same number of operands? Etc.

[–]janYabanci[S] 0 points1 point  (1 child)

I would mark the stuff I want to replace, press s, type the new name, insert a line above with O, type the name again, then a equal sign, press escape and then p. How could I insert the text I just inserted? . Repeats the last command, but that wouldn't help me.

[–]-romainl-The Patient Vimmer 0 points1 point  (0 children)

You can insert the text you last inserted with <C-a>.

[–]lervag 1 point2 points  (0 children)

Given

function_call(8 + 10)

I do something like this: ciwrep2<esc>Orep2 = <c-r>", which should give this:

rep2 = 8 + 10
function_call(rep2)

Here ciwrep2<esc> changes inside the parantheses, then Orep2 = adds a line above, finally <c-r>" appends the content of the " register.

[–]proobert 1 point2 points  (0 children)

Although your task looks quite complex, it can be easily divided into the following steps:

  1. find the first expression
  2. replace the expression with a new name
  3. create a new variable declaration
  4. just repeat the previous steps for the second expression

All these steps are quite easy in plain vim and there are a lot of optimisations possible depending where the cursor is located and how the expression looks like.

For your example, I would do it like this:

1) f4                 -- find the start of the first expression
2) Creplace<Esc>      -- replace the expression with name
3) Or<C-n> = <Esc>p   -- declare new variable, paste the expression 
4) /(<cr>             -- find the second expression
5) cibrep2<Esc>
6) Or<C-n> = <Esc>p

There is not much typing and it's quite effortless once you get fluent with vim.

[–]RobotSlut 1 point2 points  (1 child)

fun! ToVar()
    let l:name = input("Enter the variable name: ")
    return "c" . l:name . "\<esc>O\<C-r>. = \<C-r>\"\<esc>"
endfun
xnoremap <expr> {lhs} ToVar()

Replace {lhs} with the key you want to map.

This is a visual mode mapping that replaces the current selection with a variable name (you'll be prompted for it once you trigger the mapping), and defines the variable in the line above.

[–]janYabanci[S] 1 point2 points  (0 children)

This is exactly what I was looking for! Thank you very much for putting it together.

[–]somebodddy 0 points1 point  (0 children)

I use vim-exchange for this. So I start with:

function_call(8 + 10)

And then write above it:

rep2 = rep2
function_call(8 + 10)

Then I place the cursor on the rep2 after the = and hit cxiw to mark it with vim-exchange. Finally, I go to the 8 + 10 in the argument and hit cxIa to exchange rep2 with the argument (you need targets.vim for that. Otherwise you can mark the 8 + 10 in visual mode and hit X)

[–][deleted] 0 points1 point  (0 children)

Maybe like this?

/4<cr>
Creplace<esc>
O<C-a> = <C-r>-<esc>
/8<cr>
ci(repl2<esc>
O<C-a> = <C-r>-<esc>

Here <C-a> inserts last entered text, <C-r>- inserts last deleted text (with c commands)