john2x
john2x

Reputation: 23634

How to yank an entire block in Vim?

Is it possible to yank an entire block of Python code in Vim?

Be it a def, for, if, etc. block...

Upvotes: 27

Views: 21648

Answers (11)

Dunk
Dunk

Reputation: 1406

vim-indent-object works pretty well. Worth a shot.

Upvotes: 2

Joann V
Joann V

Reputation: 1

  1. In .py file, press Esc
  2. Press shift V to enter visual line mode
  3. Highlight with up and down arrow keys
  4. Press d to delete the selected rows
  5. Go to the row you would like to place the lines and press p to paste lines after a row or press shift P to paste before a row

Hope that helps.

Upvotes: 0

user2151627
user2151627

Reputation: 21

Just fold the class using za and then use visual mode ( V ) to select the collapsed class. This way you don't have to scroll too much. Then just yank with y. When you are done yanking unfold the class with za again.

Upvotes: 0

David Cain
David Cain

Reputation: 17333

The excellent add-on suite Python-mode includes some key commands to navigate classes, methods, and function blocks.

  • To yank a method: yaM (inner method: yiM)

  • To yank a class: yaC

There are other handy motions, like moving from function-to-function (]]). See the complete list of keys for more.

Upvotes: 9

glts
glts

Reputation: 22684

I made a plugin named spacebox which does a Visual selection of all lines with the same or more indentation as the current line.

With Python whitespace being the way it is, you could place your cursor on the line below def or if, and issue the command :SpaceBox to select your "block".

Upvotes: 1

Vincent
Vincent

Reputation: 4933

You can yank a paragraph with y}. This will not yank all the methods if you have a blank line though.

Upvotes: 34

Nathan Fellman
Nathan Fellman

Reputation: 127428

If you want to yank everything except the { use yi{ (or yi}). If you to include the curly braces use ya{ (or ya}).

The i and a modifiers mean in and all.

To yank a word no matter where in the word you are: yiw

To yank the contents of parentheses: yi); if you want to include them, use ya(

You can do the same for " or ' with yi", ya" or yi' and ya'.

Of course, you're not limited to yanking. You can delete a word with diw or change it with ciw, etc... etc...

Upvotes: 17

John La Rooy
John La Rooy

Reputation: 304137

You can combine a search with yank, so if your function ends with return retval you can type y/return retval

Upvotes: 4

user186024
user186024

Reputation: 29

  1. Enter visual line selection by pressing 'V'
  2. When finished selecting the block pres 'y'
  3. Paste it somewhere with 'p' or 'P'

Upvotes: 2

JimB
JimB

Reputation: 109331

I usually just use visual block mode. Shift-V, move, and 'y'ank the highlighted block. There's only so many shortcuts I can keep in memory at once :)

Upvotes: 4

Adam Bellaire
Adam Bellaire

Reputation: 110429

There's a vim add-on script python_fn.vim which has, as one of its functions, a key binding to visually select a block of Python code using ]v. You could then yank it with y as normal.

Upvotes: 4

Related Questions