3

Example: I have some text, like this php code

if(empty($condition)) {
    // work here...
}

And my default register contains the string "$NewCondition".

I want to place the cursor inside the first set of parens (...) and perform a command that will result in this:

if($NewCondition) {
    // work here...
}

So my question: Is there some way to replace the text inside the parens using the default register?

Most suggestions I've found, such as this one, fail when there's nested parens.

I also know I can name registers, such as "ayi( to yank all text in parens into register a, but I don't want to have to type two extra characters every time I'm yanking. That's why I'm asking about the default register.

In an ideal solution the default register would still contain "$NewCondition" when the replace is complete.

Community
  • 1
  • 1
Jonathan Beebe
  • 5,241
  • 3
  • 36
  • 42

3 Answers3

4
vi)p

Unfortunately this will also place the old text in the default register.

cdleonard
  • 6,570
  • 2
  • 20
  • 20
  • Overwriting the default register can be fixed: http://stackoverflow.com/questions/290465/vim-how-to-paste-over-without-overwriting-register – Luc Hermitte Oct 13 '11 at 07:50
2
vi("_dP
  • vi( to select empty($condition)
  • "_d to delete it into the blackhole register
  • P to put the content of the default register before the cursor which is now on the last )

I use this a lot so I have this mapping to make the whole thing shorter:

vnoremap <Leader>p "_dP

so (with , as the mapleader) I do vi(,p.

As a side note, if your cursor is in the second set of parenthesis you can do vi(i( to go one level up and so on.

romainl
  • 186,200
  • 21
  • 280
  • 313
1

Try this: "_ci)<c-r>"<esc>

Some detailed explanations here: "_ means to select the "black-hole" register for the following command. So the text that gets deleted in your next operation will simply be discarded and will not be put into any registers. ci) means "change inner parentheses", it will delete all the text within current level of parenthesis pair, then bring you into insert mode. <c-r> in insert mode means to insert the text in a specified register into the current cursor position, and " selects the "default register". At last, we use <esc> to bring us back to normal mode.

Indeed a lot of key strokes! If you use such operation very often, you can consider creating a keymap for it. See :help map for more details.

Lifu Tang
  • 1,271
  • 9
  • 11