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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:30:51  来源:igfitidea点击:

How to yank an entire block in Vim?

pythonvim

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

无论是defforif等...块

回答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 iand amodifiers mean inand all.

ia修饰符意味着所有

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 diwor 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 yas 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

  1. Enter visual line selection by pressing 'V'
  2. When finished selecting the block pres 'y'
  3. Paste it somewhere with 'p' or 'P'
  1. 按“V”进入视线选择
  2. 完成选择块后按“y”
  3. 用“p”或“P”将其粘贴到某处

回答by John La Rooy

You can combine a search with yank, so if your function ends with return retvalyou 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 defor if, and issue the command :SpaceBoxto 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

  1. In .py file, press Esc
  2. Press shift Vto enter visual line mode
  3. Highlight with up and down arrow keys
  4. Press dto delete the selected rows
  5. Go to the row you would like to place the lines and press pto paste lines after a row or press shift Pto paste before a row
  1. 在 .py 文件中,按 Esc
  2. 按下shift V进入视线模式
  3. 使用向上和向下箭头键突出显示
  4. 按下d可删除选定的行
  5. 转到您想要放置线条的行,然后按p在一行之后粘贴行或按shift P在一行之前粘贴

Hope that helps.

希望有帮助。