How to yank an entire block in Vim?
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 :)
Related videos on Youtube

Comments
-
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 about 13 years"Belongs on superuser.com", maybe? I'm not sure ...
-
Adam Bellaire about 13 yearsDon'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 about 13 yearsYou 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 about 13 yearsThis 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 about 13 yearsthe visual selection is also very useful for indenting/dedenting blocks using > and <
-
jmhmccr almost 12 yearsGreat for a C language. I think the question was specific to python though.
-
Robert Rüger over 8 yearsThere is also
p
to yank the current paragraph (that is anything separated by blank lines above an below).yip
yanks the entire paragraph, andyap
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 almost 2 yearsWhere 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
.