如何通过 python 源代码文件中的注释覆盖 vim 选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/376111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:59:19  来源:igfitidea点击:

How do you override vim options via comments in a python source code file?

pythonvim

提问by Paul D. Eden

I would like to set some vim options in one file in the comments section.

我想在评论部分的一个文件中设置一些 vim 选项。

For example, I would like to set this option in one file

例如,我想在一个文件中设置此选项

set syntax=python

The file does not have a .py extension and I am not interested in making my vim installation recognise all files with this extension as python files.

该文件没有 .py 扩展名,我不想让我的 vim 安装将所有具有此扩展名的文件识别为 python 文件。

I know this can be done because I have seen it, but my googling for this has not yet been fruitful.

我知道这是可以做到的,因为我已经看到了,但是我对此的谷歌搜索还没有结果。

回答by Harper Shelby

You're wanting a modelinesyntax, e.g.

你想要一个模式行语法,如

# vim: set syntax=python:

See: Modeline magicat Vim Wikia for more details.

有关更多详细信息,请参阅:Vim Wikia 上的Modeline magic

回答by Ben Blank

I haven't used vim much, but I think what you want is to add a line like the following to the end of your file:

我没有经常使用 vim,但我认为您想要的是在文件末尾添加如下一行:

# vim: set syntax=python:

回答by kenorb

You override the Vim options by adding the modeline near the top or the bottom of the file, such as:

您可以通过在文件顶部或底部附近添加模式行来覆盖 Vim 选项,例如:

// vim: set syntax=python:

or:

或者:

/* vim: set syntax=python: */

or like:

或喜欢:

# vim: set syntax=python ts=4 :

Other examples (from wikia):

其他示例(来自wikia):

// vim: noai:ts=4:sw=4
   -or-
/* vim: noai:ts=4:sw=4
*/
   -or-
/* vim: set noai ts=4 sw=4: */
   -or-
/* vim: set fdm=expr fde=getline(v\:lnum)=~'{'?'>1'\:'1': */

Here is the example which I'm using (on the last line of the file):

这是我正在使用的示例(在文件的最后一行):

# vim: set ts=2 sts=2 et sw=2 ft=python:

Few highlights:

几个亮点:

  • Vim executes a modeline only when modelineis set to modelineor a possitive integer and you're not root (some OS such as Debian, Ubuntu, Gentoo, OSX, etc. disable modelines by default for security reasons), so you need to add set modelineinto your ~/.vimrcfile (:e $MYVIMRC),
  • the line must be in the first or last few lines,
  • space between the opening comment and vim:is required,
  • location where vim checks for the modeline is controlled by the modelinesvariable (see: :help 'modelines'),
  • with set, the modeline ends at the first colon (:),
  • text other than "vim:" can be recognised as a modeline.
  • Vim 仅在modeline设置为modeline或 正整数并且您不是 root时才执行模式行(某些操作系统,例如 Debian、Ubuntu、Gentoo、OSX 等,出于安全原因,默认情况下禁用模式行),因此您需要添加set modeline到您的~/.vimrc文件 ( :e $MYVIMRC),
  • 该行必须在前几行或最后几行中,
  • 开头评论和vim:必填之间的空格,
  • vim 检查模式行的位置由modelines变量控制(参见 :):help 'modelines'
  • set,在第一结肠(该模式行端部:),
  • “vim:”以外的文本可以被识别为模式行。

Related:

有关的: