方括号映射 [[
, ]]
, [m
, ]m
等
$VIMRUNTIME/ftplugin/python.vim
现在(2018年)重映射了所有在:h ]]
和:h ]m
下记录的Python语言内置映射。这些映射为:
]] 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:
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
class MappingSubclass(Mapping):
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 相似但略有修改的动作:
原版 Vim 8.0 的“类”动作(例如“]]”,“[[”等)会查找从第一列开始的块,无论这些块是否是类或函数块,而它的“方法/函数”动作(例如“[m”,“]m”等)则会在任何缩进级别上查找所有块,无论这些块是否是类或函数块。相比之下,“Pythonsense”类动作仅查找所有类定义,无论其缩进级别如何,而其方法/函数动作仅查找所有方法/函数定义,无论其缩进级别如何。
有关所有细节和示例,请参见https://github.com/jeetsukumaran/vim-pythonsense#stock-vim-vs-pythonsense-motions。
此外,该插件定义了文本对象ic/ac
(类)、if/af
(函数)、id/ad
(文档字符串)。
对于 neovim,您可以使用 treesitter 和 neovim 插件nvim-treesitter-textobjects。
文本对象if/af
& ic
/ac
这些不包含在$VIMRUNTIME/ftplugin/python.vim
中,但是有一些插件提供了这些文本对象:
有关 Python 文本对象的讨论,请参见what's the fastest way to select a function of Python via VIM?。
$VIMRUNTIME/ftplugin/python.vim
中提供的映射,我建议你在 https://github.com/tpict/vim-ftplugin-python 的上游存储库中提出问题。请提供一个示例文件和对于不同方括号映射所期望行为的描述。 - Hotschke