19

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

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
northtree
  • 8,569
  • 11
  • 61
  • 80
  • do you mean for code-completion, then have a look at omni-completion. Perhaps be a bit more verbose in your question... – Fredrik Pihl Jul 05 '11 at 08:32
  • 3
    Not sure if this is what they meant, but I read this at first as wanting to know if one can literally select a function easily (like one can use % in a language which uses braces). – Lucas Jones Jul 05 '11 at 08:40
  • You want code completion or jumping to a specified function? – Thanh DK Jul 06 '11 at 10:38
  • 2
    The OP means to use VIM's internal select feature `v` and motions to select an entire Python (indentation-delimited) function. – dotancohen Nov 20 '13 at 10:03
  • Note that there is a Stack Exchange community just for Vi/Vim/Neovim now: https://vi.stackexchange.com/ – Flimm Apr 29 '23 at 10:04

8 Answers8

15

try vis to visualy select and o to jump edges

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • 2
    This doesn't seem to work if the function contains a blank line. – LondonRob Oct 02 '20 at 10:47
  • 1
    Some better options are provided [here](https://stackoverflow.com/a/28284564/2071807). Note particularly `]m` which seems to work fairly well. – LondonRob Oct 03 '20 at 15:28
10

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.

Julius
  • 886
  • 8
  • 13
  • Does this work by looking at indentation? Where can I read more about this in Vim's help? – Flimm Apr 29 '23 at 10:06
5

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
ajkaanbal
  • 87
  • 1
  • 3
5

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.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Thanh DK
  • 4,257
  • 3
  • 23
  • 18
4

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

Peruz
  • 403
  • 3
  • 10
0

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.

Marc Moreaux
  • 190
  • 1
  • 9
0

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)

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
0

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.

C. Braun
  • 5,061
  • 19
  • 47