It's quite often that while refactoring you'll run into the scenario of needing to inline a variable.
Obviously nothing ground-breaking here, so why not automate this? Let's attack it in a very straightforward manner:
- Record our actions into a register
- Dump the recorded commands into a function
- Create a leader to call that function
Here are the commands I recorded:
^*``2w"zDdd``cw^Rz^[
Hop to beginning of line, find next occurrence, hop back, move to value of variable, put it in register 'z', delete line, go back to variable usage, replace with register 'z', and ESC. If your commands are a little different, no need to worry, everyone has his or her own style. Now let's drop this in a function and create a leader for it.
function! InlineVariable()
normal ^*``2w"zDdd``cw^Rz^[
endfunction
nmap <Leader>i :call InlineVariable()<CR>
And we're all set! The important takeaway here is how simple this was, not necessarily about learning anything new in Vim. You should be automating any repetitive tasks you do. It's very easy to forget how much Vim wants you to type less while doing more. Happy automating!