Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Function copy

Tags:

vim

Is there a command to copy a complete function? (Not by selecting through mouse)

ex: 10yy --> will copy 10 lines from the cursor position.
I do not know the no of lines of a function, but want to copy the entire function.

Is there any command to copy the entire function?

like image 225
Tamin Avatar asked May 17 '12 09:05

Tamin


People also ask

How do I copy a whole function in Vim?

V]m : start visually selecting (line-wise) and then move your cursor to the opening bracket of the function body. %y : move your cursor to the closing bracket of the function body, then yank.

How do you copy in Vim?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.

How do I copy a block of text in Vim?

You can copy a block of text by pressing Ctrl-v (or Ctrl-q if you use Ctrl-v for paste), then moving the cursor to select, and pressing y to yank. Now you can move elsewhere and press p to paste the text after the cursor (or P to paste before).

How do I copy a section of code in Vim?

For copy: Place cursor on starting of block and press md and then goto end of block and press y'd. This will select the block to paste it press p. For cut: Place cursor on starting of block and press ma and then goto end of block and press d'a. This will select the block to paste it press p.


2 Answers

This is what I do in JavaScript and PHP:

va{Vy
  1. Place the cursor somewhere in my method,
  2. va{ to visually select the the code block,
  3. V to turn the selection from character-wise to line-wise,
  4. y to yank

If you are too deep and va{ only selects a portion of your function you can do more a{ until you select what you want.

like image 77
romainl Avatar answered Nov 16 '22 01:11

romainl


If you are using VIM, and function body in the programming language you happen to be enclosed with braces, you can just place the cursor on one of the braces and type y%.

like image 31
satoru Avatar answered Nov 16 '22 01:11

satoru