Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this

subject { }

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

And I want this

subject { MyModel.my_method }

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

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
    
Definitely love the macro version because this is something I do often. Thanks a lot. – Beni Mio Feb 24 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 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.) – dash-tom-bang Feb 24 at 17:28
    
@statox: to avoid the dependance on the search register, maybe have the macro contain : ?{ *} instead of: N – Olivier Dulac Feb 24 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 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$

or

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.

share|improve this answer
    
Thanks for this version with the plugin. Not sure I will mapping D but like your version. – Beni Mio Feb 24 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! – Philipp Moers Feb 24 at 11:44

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

^y$?{<CR>p``dd

^          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
share|improve this answer
    
Thanks for your version Romain. Love it – Beni Mio Feb 24 at 10:50

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

1  subject { }
2
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).

share|improve this answer

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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