我在/.vim/ftplugin/python.vim文件中定义了一个函数。 问题是,每次打开.py文件,我都会得到E122: Function MyFunction already exists, add ! to replace it E122: Function MyFunction already exists, add ! to replace it 。
我知道,如果我加! 然后它将覆盖该函数(这不是一个问题),但这意味着它将每次都替换它,并且它是一个无用的(而不是非常干净的)补充操作。
我猜这个问题来自每次打开一个新的.py文件时.py获取的Python配置文件。
我怎么能告诉VIM只采购一次?
I have defined a function in my /.vim/ftplugin/python.vim file. The problem is that every time I open a .py file, I get the E122: Function MyFunction already exists, add ! to replace it.
I know that if I add ! then it will override the function (which is not a problem), but that means that it will replace it everytime, and it is a useless (and not very clean) supplementary action.
I guess that the problem come from the Python configuration file being sourced again and again every time I open a new .py file.
How can I tell VIM to source only once?
最满意答案
我会建议将该函数放在自动加载目录中。 (阅读:help autoload它解释如何工作做得很好)。 快速版本如下。
编辑文件~/.vim/autoload/ftplugin/python.vim并在那里添加你的功能。 自动加载之后的所有内容都是函数签名的一部分。 (而不是/在目录之间使用#并且为.file目录留下.vim directory(s)#file#FunctionName )
function ftplugin#python#MyFunction() ... endfunctionvim在第一次使用时会自动加载此函数。
在filetype插件里面,你只需要创建必要的映射和命令。
command -buffer MyFunction call ftplugin#python#MyFunction() nnoremap <buffer> <leader>m :call ftplugin#python#MyFunction()<CR>并且该函数将在第一次调用时自动加载。 加载ftplugin的其他缓冲区不会遇到重新定义问题。
I would recommend putting the function in an autoload directory. (Read :help autoload it does a very good job explaining how this works). The quick version is below.
Edit the file ~/.vim/autoload/ftplugin/python.vim and add your function there. Everything after the autoload is part of the function signiture. (Instead of / use # between directories and leave off the .vim for the filename directory(s)#file#FunctionName)
function ftplugin#python#MyFunction() ... endfunctionThis function will automatically be loaded by vim the first time it is used.
Inside the filetype plugin you would just create the necessary mappings and commands.
command -buffer MyFunction call ftplugin#python#MyFunction() nnoremap <buffer> <leader>m :call ftplugin#python#MyFunction()<CR>and the function will automatically be loaded when it is called the first time. And other buffer that loads the ftplugin won't run into the redefinition problem.
更多推荐
发布评论