【问题标题】:Get emacs to recognize python3 shebang让emacs识别python3 shebang
【发布时间】:2023-03-29 19:47:01
【问题描述】:

我正在使用 emacs 编辑一些 Python 3 代码,但当 shebang 为 #! /usr/bin/env python3 时,它不提供语法高亮显示。仅使用#! /usr/bin/env python 就可以很好地突出显示。如何让 emacs 将 python3 shebang 识别为 Python 文件,并提供适当的语法高亮显示?

编辑:我使用的是 22.1.1 版本,无法更改。

【问题讨论】:

  • 你安装Emacs Python mode了吗?
  • @DYZ python-mode 是 emacs 的一部分
  • @jenesaisquoi 仅限 24.1 及更高版本。
  • @DYZ 啊谢谢,没想到是最近

标签: python-3.x emacs shebang


【解决方案1】:

检查auto-mode-interpreter-regexp 的值,默认情况下应该正确匹配shebang 条目。然后,确保您的interpreter-mode-alist 中有一个条目,就像

("python[0-9.]*" . python-mode)

如果不是出于某种原因,请将其添加到您的 init 文件中,例如。

(cl-pushnew '("python[0-9.]*" . python-mode) interpreter-mode-alist :test #'equal)

编辑

由于您的 emacs 相当古老,请尝试

(push '("python[0-9.]*" . python-mode) interpreter-mode-alist)

【讨论】:

  • 将建议行添加到我的初始化文件会给我以下错误:An error has occured while loading '/Users/[redacted]/.emacs': Symbol's function definition is void: cl-pushnew
  • 您使用的是非常旧的 emacs 吗?尝试在您的暂存缓冲区中评估(push '("python[0-9.]*" . python-mode) interpreter-mode-alist) 或使用M-: 并检查您的python 文件
  • 我坚持使用 22.1.1 版 - 编辑了我的问题以包含它。
【解决方案2】:

我在这里遇到了同样的问题,Rorschach 的另一个答案对我不起作用,也是因为我有一个无法升级的旧版本 (24.3) 的 emacs。经过反复试验,这对我有用:

在您的.emacs 文件中添加以下行:

(push '("python3" . python-mode) interpreter-mode-alist)

旧的 emacs(24.4 之前的版本)不支持正则表达式来编辑解释器模式列表,这就是为什么其他答案的建议修复不起作用的原因。

emacs 24.4 的变更日志提到了对正则表达式的新支持:“interpreter-mode-alist 中元素的汽车(原文如此)现在被视为正则表达式而不是文字字符串。”

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 2017-06-21
    • 2017-10-03
    • 2020-12-28
    • 1970-01-01
    • 2023-04-08
    • 2014-04-08
    • 1970-01-01
    相关资源
    最近更新 更多