northtree
northtree

Reputation: 9275

what's the fastest way to select a function of Python via VIM?

Is it possible without any plugins? Or what's the best plugin for edit python file?

Upvotes: 20

Views: 9584

Answers (9)

ajkaanbal
ajkaanbal

Reputation: 87

With this plugin https://github.com/bps/vim-textobj-python you can select, delete, change, etc:

  • af: a function
  • if: inner function
  • ac: a class
  • ic: inner class

Upvotes: 5

Felipe Perry
Felipe Perry

Reputation: 1

simple: vaf (visual around function) vac (visual around class) zero plugins

Upvotes: -3

Thanh DK
Thanh DK

Reputation: 4397

I assume you mean visually selecting the whole function quickly. One way is to use the plugin Indent text object (GitHub). You can use vai to select the whole function, provided your cursor is inside the function and only 1 indentation level lower.

If you really want select function regardless of indentation level, I suggest you read this and write your own text object. I imagine it would be quite easy since Python has def keyword for defining function.

Upvotes: 7

C. Braun
C. Braun

Reputation: 5201

If your function is long or has many blank lines, using v}}...}}d is pretty slow.

The quickest way I've found (without plug-ins) is zc2dd. The command zc closes the fold under the cursor, so if you are at the function declaration or any line at the outermost indent level, the whole function will fold. Then 2dd (or 3dd if you have two blank lines before/after your function) will remove the whole thing.

Upvotes: 0

Marc Moreaux
Marc Moreaux

Reputation: 300

I try to avoid visual, hence for actions like yank, I tend to go to the beginning of the paragraph [[, yank till the end y]] and come back ^o. All in one, it is [[y]]^o (With ^ standing for control).

For visual you might use [[v]] or some variations like [[v][, [[v]m, or [[v]M.

Upvotes: 0

Julius
Julius

Reputation: 925

The way I do it is not specific to functions. It will just select a continuous block of Python code:

v]] if what you want to select is below the cursor
v[[ if it is above the cursor.

Just remove one bracket if the cursor is on the first line of the code block you want to select.

Upvotes: 13

Peruz
Peruz

Reputation: 433

I normally use vip or any of the above depending on the need, do not forget that you can always select up (i.e., down =)) to the next occurrence using v/match (followed by Enter to confirm, and possibly n for next). For python, you could look for the next def or for the return (ret is normally enough).

This isn't the fastest at the beginning, but it is very general, use it in any language and also outside coding (md, latex, etc.).

Upvotes: 5

SergioAraujo
SergioAraujo

Reputation: 11820

try vis to visualy select and o to jump edges

Upvotes: 16

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11048

you have to use omnicompletion for vim7 but it's only working with vim-nox, vim-gtk, vim-gnome o vim-athen

also read throu this page for configuring vim with python (autoindend, syntax highlight and autocompletion)

Upvotes: 0

Related Questions