How to yank an entire block in Vim?

Viewed 17615

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

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

11 Answers

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...

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.

  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.

Related