How to yank an entire block in Vim?

17,047

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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 :)

Share:
17,047

Related videos on Youtube

john2x
Author by

john2x

twitter, github, bitbucket account: john2x

Updated on October 13, 2020

Comments

  • john2x
    john2x about 2 years

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

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

    • Joachim Sauer
      Joachim Sauer about 13 years
      "Belongs on superuser.com", maybe? I'm not sure ...
    • Adam Bellaire
      Adam Bellaire about 13 years
      Don't agree, this is about the use of a software tool in a specific development process (coding Python), see meta.stackexchange.com/questions/12373/…
  • noomz
    noomz about 13 years
    You can use Ctrl-v and y} to see which lines will be yanked. You can press } as you want to cover your block.
  • John La Rooy
    John La Rooy about 13 years
    This is what I usually do too, I don't think it's the best way, but it's easy enough to do with one brain cell :)
  • John La Rooy
    John La Rooy about 13 years
    the visual selection is also very useful for indenting/dedenting blocks using > and <
  • jmhmccr
    jmhmccr almost 12 years
    Great for a C language. I think the question was specific to python though.
  • Robert Rüger
    Robert Rüger over 8 years
    There is also p to yank the current paragraph (that is anything separated by blank lines above an below). yip yanks the entire paragraph, and yap yanks the entire paragraph + the following blank line. This is useful if your cursor is already somewhere inside the paragraph you want to yank.
  • Andy
    Andy almost 2 years
    Where is this feature in the help document? I look for a long time did not find the corresponding URL.The modifier cannot be found using help: a.