python 如何在 Vim 中拉出整个块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1531551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to yank an entire block in Vim?
提问by john2x
Is it possible to yank an entire block of Python code in Vim?
是否可以在 Vim 中提取整个 Python 代码块?
Be it a def
, for
, if
, etc. block...
无论是def
,for
,if
等...块
回答by Vincent
You can yank a paragraph with y}
. This will not yank all the methods if you have a blank line though.
你可以用 猛拉一个段落y}
。但是,如果您有一个空行,这不会删除所有方法。
回答by Nathan Fellman
If you want to yank everything except the {
use yi{
(or yi}
). If you to include the curly braces use ya{
(or ya}
).
如果你想拉除{
使用yi{
(或yi}
)之外的所有东西。如果要包含花括号,请使用ya{
(或ya}
)。
The i
and a
modifiers mean inand all.
在i
与a
修饰符意味着在与所有。
To yank a word no matter where in the word you are: yiw
无论您在单词中的哪个位置,都要猛拉一个单词: yiw
To yank the contents of parentheses: yi)
; if you want to include them, use ya(
拉出括号的内容:yi)
; 如果要包含它们,请使用ya(
You can do the same for "
or '
with yi"
, ya"
or yi'
and ya'
.
您可以做同样的"
或'
与yi"
,ya"
或yi'
和ya'
。
Of course, you're not limited to yanking. You can delete a word with diw
or change it with ciw
, etc... etc...
当然,您不仅限于猛拉。您可以使用 删除diw
或更改单词ciw
,等等...等等...
回答by David Cain
The excellent add-on suite Python-modeincludes some key commandsto navigate classes, methods, and function blocks.
优秀的附加套件Python 模式包括一些用于导航类、方法和功能块的关键命令。
To yank a method: yaM(inner method: yiM)
To yank a class: yaC
要拷贝的方法:yaM(内法:yiM)
猛拉一个班级: yaC
There are other handy motions, like moving from function-to-function (]]). See the complete list of keysfor more.
还有其他方便的动作,例如从功能移动到功能 ( ]])。有关更多信息,请参阅完整的密钥列表。
回答by Adam Bellaire
There's a vim add-on script python_fn.vimwhich 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.
有一个 vim 附加脚本python_fn.vim,它的功能之一是一个键绑定,可以使用]v
. 然后你可以y
像往常一样猛拉它。
回答by JimB
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 :)
我通常只使用视觉块模式。Shift-V,移动并“y”ank 突出显示的块。我一次只能记住这么多快捷方式:)
回答by user186024
- Enter visual line selection by pressing 'V'
- When finished selecting the block pres 'y'
- Paste it somewhere with 'p' or 'P'
- 按“V”进入视线选择
- 完成选择块后按“y”
- 用“p”或“P”将其粘贴到某处
回答by John La Rooy
You can combine a search with yank, so if your function ends with return retval
you can type y/return retval
您可以将搜索与 yank 结合使用,因此如果您的功能以return retval
您可以键入y/return retval
回答by glts
I made a plugin named spaceboxwhich does a Visual selection of all lines with the same or more indentation as the current line.
我制作了一个名为spacebox的插件,它对与当前行具有相同或更多缩进的所有行进行可视化选择。
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".
由于 Python 空白就是这样,您可以将光标放在def
或下方的行上if
,然后发出命令:SpaceBox
来选择您的“块”。
回答by user2151627
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.
只需使用 za 折叠类,然后使用视觉模式 ( V ) 选择折叠的类。这样你就不必滚动太多。然后用 y 猛拉。完成后,再次使用 za 展开类。
回答by Joann V
- In .py file, press
Esc
- Press
shift V
to enter visual line mode - Highlight with up and down arrow keys
- Press
d
to delete the selected rows - Go to the row you would like to place the lines and press
p
to paste lines after a row or pressshift P
to paste before a row
- 在 .py 文件中,按
Esc
- 按下
shift V
进入视线模式 - 使用向上和向下箭头键突出显示
- 按下
d
可删除选定的行 - 转到您想要放置线条的行,然后按
p
在一行之后粘贴行或按shift P
在一行之前粘贴
Hope that helps.
希望有帮助。