I have this

subject { }

it 'delete user' do
  expect(BlaBla).to eq ''

And I want this

subject { MyModel.my_method }

it 'delete user' do
  expect(BlaBla).to eq ''

1. First try

If I do dd and then past it inside my bracket with i<CTRL>r"" I will get something like

subject {   MyModel.my_method

And I have to go in insert mode to past the text and I have to clean some extra space and joins line.

2. Second try

I go on the line wy$ then "_dd to remove the line without save it. Then go after the first { : ?{<ENTER>lpa<space>

What will be a better motion to cut only the content of the line store into a register remove the line jump below past inside brackets?

5 Answers 5


If your problem is to repeat the operation in an efficient way you could use a macro:

First put { in your search register with /{

Then record your macro: qq^DNp``ddq

  • qq record the macro in the q register
  • ma put a mark on the line you'll delete
  • ^ got to the first word of the line (ie. MyModel)
  • D delete the end of the line and put it in the unnamed register
  • N go to the previous occurence of the search register ({)
  • p put the deleted text between the brackets
  • 'a go to the line you marked before
  • dddelete the line
  • q stop the macro recording

Then you'll simply have to go to the next occurence of MyModel.my_method and use @q to repeate the operation.

  • Definitely love the macro version because this is something I do often. Thanks a lot.
    – Mio
    Feb 24, 2016 at 10:51
  • 1
    I'm glad you liked it. There just an important point with this macro version: as it relies on the content of the search register, you might have to check that the register still contains { and not something else if you've been doing other stuff between to uses of the macro.
    – statox
    Feb 24, 2016 at 10:55
  • Rather than jumping back and forth, you can also just delete the line into the black hole: D"_ddNp as the macro. (Although I understand you might want to be back where you removed the line when the macro is done.) Feb 24, 2016 at 17:28
  • @statox: to avoid the dependance on the search register, maybe have the macro contain : ?{ *} instead of: N Feb 24, 2016 at 19:13
  • @dash-tom-bang: You're right, I just assumed that it would be better to go back to the line where you started the macro but it all depends on you're workflow :)
    – statox
    Feb 24, 2016 at 19:50

You can delete the content of a line (without the line break) with ^d$. You can delete to any register (e.g. 'l': "l^d$.) If you use that often, I suggest mapping it to a new key:

nnoremap D ^d$


nnoremap D "l^d$

Note that ^ in difference to 0 does not contain leading whitespace.

Then, within the {}, paste it via p or "lp respectively.

Additional Note:

I personally often end up deleting the line after deleting its contents (or more general: deleting something afterwards that should not overwrite my unnamed register). Instead of using registers, for me this plugin feels more convenient. It effectively turns your unnamed register into a stack, so you can delete multiple things and when you paste, you can cycle through them.

  • Thanks for this version with the plugin. Not sure I will mapping D but like your version.
    – Mio
    Feb 24, 2016 at 10:50
  • 1
    Thanks! Of course, mappings are something personal, that was only an example to express the idea. ;-) Make yourself familiar with 'mapleader', if you haven't already! Feb 24, 2016 at 11:44

With the cursor on MyModel.my_method, I would do:


^          jump to first printable character
y$         yank the rest of the line
?{<CR>     jump to the opening bracket
p          put what I just yanked
``         jump back
dd         cut the line

You might consider doing something like this on the following text: 5G, ^v$hd,?{<cr>,p

1  subject { }
3  it 'delete user' do
4  expect(BlaBla).to eq ''
5  MyModel.my_method
6  end

5G gets you to line 5.

^v$hd selects the text an line 5 and places it in a register.

?{<cr> moves to the nearest { prior to line 5. p places the text in the register immediately after the { (the current position of the cursor).


For this situations, I have a smart line delete cmd:

noremap <silent> <leader>dd ^D"_dd

Then you want to paste inside { }. I'm using ?{<ENTER>vi{p:

subject {MyModel.my_method}

Your Answer

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

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