3

I'm using vim a lot and I'm trying to optimize few movements I do with too much key pressed.

For exemple I have this :

module SanitizeIdsParams
  def self.my_method ids
    ...code
  end
end

And I want this

module SanitizeIdsParams
  def self.my_method ids
    ...code
  end

  def self.my_method ids
    ...code
  end
end

For the moment I'm doing something like j<CR>v3jy<ESC>4Go<ESC>p I'm not a huge fan of :2,4t.4Go<ESC>, any other ideas without specifying exact line number ?

I would like to know also if when I yank I can stay where I yank and not going at the beginning of my visual selection.

Open to classic vim motions and plugins.

2

Once you have your visual selection you can do:

:'<,'>t'>

(it's actually :t'> because Vim adds the visual range automatically)

to copy it after the visual selection and do:

'[O<Esc>

to add an empty line.

If you don't want to use visual selection, something like this should work:

:,/end/t//

You will still need to add the empty line manually.

3

For most languages you probably would be able to jump between the start and the end of a block of code with % (check :help %), because the matchit plugin comes installed by default with vim. So on your example you could only navigate to 'begin' or 'end' and use y% to copy the entire block.

If you don't need to duplicate all the code, but just want to use as a template, you should check some kind of snippet plugin, such as Ultisnips and snipmate.

  • +1 for snipmate even despite your typo! – artfulrobot Oct 6 '15 at 20:39
0

If you have to copy a known number of lines you can do:

3yy

yanks 3 lines from the cursor position

And use p to paste where do you want them.


It may come as help to use:

set relativenumber

it will show line numbers relative to cursor position. Show you can see how many lines you need to yank(as far as the display permits).

To turn line numbering off do this:

set norelativenumber
  • thanks. I'm already using relative line number a lot. Thanks for tips. – Mio Oct 8 '15 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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