功能已经存在VIM(Function already exists VIM)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
功能已经存在VIM(Function already exists VIM)

我在/.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() ... endfunction

vim在第一次使用时会自动加载此函数。

在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() ... endfunction

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

更多推荐

本文发布于:2023-04-21 18:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/14b15823127a73706116a3b61676b0c5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   VIM   exists   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!