开发者

How to yank an entire block in Vim?

开发者 https://www.devze.com 2022-12-08 11:22 出处:网络
Is it possible to ya开发者_如何转开发nk an entire block of Python code in Vim? Be it a def, for, if, etc. block...You can yank a paragraph with y}. This will not yank all the methods if you have a bl

Is it possible to ya开发者_如何转开发nk an entire block of Python code in Vim?

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


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


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


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.


There's a vim add-on script python_fn.vim which 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.


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


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


  1. Enter visual line selection by pressing 'V'
  2. When finished selecting the block pres 'y'
  3. Paste it somewhere with 'p' or 'P'


I made a plugin named spacebox which does a Visual selection of all lines with the same or more indentation as the current line.

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


vim-indent-object works pretty well. Worth a shot.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消