方括号映射[[
, ]]
, [m
,]m
和类似的
$VIMRUNTIME/ftplugin/python.vim
now (2018) 重新映射记录在python 语言下:h ]]
的所有内置映射。:h ]m
映射是:
]] Jump forward to begin of next toplevel
[[ Jump backwards to begin of current toplevel (if already there, previous toplevel)
]m Jump forward to begin of next method/scope
[m Jump backwords to begin of previous method/scope
][ Jump forward to end of current toplevel
[] Jump backward to end of previous of toplevel
]M Jump forward to end of current method/scope
[M Jump backward to end of previous method/scope
以下带有注释的示例源代码说明了不同的映射
class Mapping: # [[[[
def __init__(self, iterable):
pass
def update(self, iterable):
pass
__update = update # []
class Reverse: # [[ or [m[m
def __init__(self, data): # [m
self.data = data
self.index = len(data) # [M
def __iter__(self): # <--- CURSOR
return self # ]M
def __next__(self): # ]m
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index] # ][
class MappingSubclass(Mapping): # ]] or ]m]m
def update(self, keys, values):
pass
在提交abd468ed0 (2016-09-08)、01164a6546b4 (2017-11-02) 和 7f2e9d7c9cd (2017-11-11)中添加和改进了映射。
如果您还没有此文件的新版本,您可以下载并将其放入~/.vim/ftplugin/python.vim
. 此文件夹优先于$VIMRUNTIME/ftplugin
.
在将这些映射添加到 之前,已经有提供、、和$VIMRUNTIME
的插件。此外还定义了文本对象、、和:python-mode
[[
]]
[M
]M
python-mode
aC
iC
aM
iM
这个 vim 插件提供类似于内置的动作:
2.4 Vim motion ~
*pymode-motion*
Support Vim motion (See |operator|) for python objects (such as functions,
class and methods).
`C` — means class
`M` — means method or function
*pymode-motion-keys*
========== ============================
Key Command (modes)
========== ============================
[[ Jump to previous class or function (normal, visual, operator)
]] Jump to next class or function (normal, visual, operator)
[M Jump to previous class or method (normal, visual, operator)
]M Jump to next class or method (normal, visual, operator)
aC Select a class. Ex: vaC, daC, yaC, caC (normal, operator)
iC Select inner class. Ex: viC, diC, yiC, ciC (normal, operator)
aM Select a function or method. Ex: vaM, daM, yaM, caM (normal, operator)
iM Select inner func. or method. Ex: viM, diM, yiM, ciM (normal, operator)
========== ============================
这个插件提供了类似的动作,但稍作修改:
常用的 Vim 8.0“类”运动(“]]”、“[[”等),查找从第一列开始的块,无论这些是类还是功能块,而它的方法/功能运动(“[m”、“]m”等)在任何缩进处查找所有块,无论这些块是类块还是功能块。相比之下,“Pythonsense”类运动致力于查找所有且仅类定义,而不管其缩进级别如何,而其方法/函数运动则致力于查找所有且仅方法/函数定义,而与它们的缩进级别无关。
所有详细信息和示例都在https://github.com/jeetsukumaran/vim-pythonsense#stock-vim-vs-pythonsense-motions中给出。此外,该插件还定义了文本对象ic/ac
(类)、if/af
(函数)、id/ad
(文档字符串)。
有关 python 文本对象的讨论,请参阅通过 VIM 选择 Python 函数的最快方法是什么?.