简体   繁体   English

在 emacs 中以 sh 模式打开 zsh 脚本

[英]Open zsh scripts in sh-mode in emacs

*.zsh files open in the default mode (text-mode for me). *.zsh文件以默认模式打开(对我来说是文本模式)。 However, sh-mode is actually multiple modes including behaviours for zsh, bash, etc. How can I tell emacs to open *.zsh files specifically in the zsh flavor of sh-mode?但是,sh-mode 实际上是多种模式,包括 zsh、bash 等的行为。我如何告诉 emacs 以 sh-mode 的 zsh 风格打开*.zsh文件?

The flavor of sh-mode is autodetected from the shebang line (first line of your script). sh-mode 的风格是从 shebang 行(脚本的第一行)自动检测到的。 If you have "#!/bin/zsh", zsh will be assumed and (for instance) autoload will be recognized as a keyword.如果您有“#!/bin/zsh”,则将假定为 zsh,并且(例如) autoload将被识别为关键字。 autoload will be not recognized as such if first line is "#!/bin/bash"如果第一行是“#!/bin/bash”, autoload将不会被识别

To make emacs recognize *.zsh files as shell scripts, just add this to your init file:要使 emacs 将 *.zsh 文件识别为 shell 脚本,只需将其添加到您的 init 文件中:

(add-to-list 'auto-mode-alist '("\\.zsh\\'" . sh-mode))

A programmatic way of selecting a flavor when you don't want to use the shebang is doing this in a sh-mode buffer:当您不想使用 shebang 时选择风味的一种编程方式是在 sh 模式缓冲区中执行此操作:

(sh-set-shell "zsh")

So in your case what you need (unless you use shebang) is to update the auto-mode-alist as above and因此,在您的情况下,您需要(除非您使用shebang)是更新自动模式列表如上和

(add-hook 'sh-mode-hook
          (lambda ()
            (if (string-match "\\.zsh$" buffer-file-name)
                (sh-set-shell "zsh"))))

Whether your file has a #!您的文件是否有#! shebang or not, you can always use a file mode line or a local variables section to set shell-script mode.不管是否是shebang,您始终可以使用文件模式行或局部变量部分来设置shell 脚本模式。 Having one of these in your script will allow Emacs to do the right thing even if you haven't updated the auto-mode-alist, so is recommended for any non-standard file extension.即使您没有更新 auto-mode-alist,在您的脚本中拥有其中之一将允许 Emacs 做正确的事情,因此建议用于任何非标准文件扩展名。

The Emacs file mode line for shell scripts is -*- mode: sh -*- . shell 脚本的 Emacs 文件模式行是-*- mode: sh -*- It should be in a comment, and must appear on the first line (or the second line if the first one is a shebang line).它应该在注释中,并且必须出现在第一行(如果第一行是 shebang 行,则出现在第二行)。

If you can't put it on the first (second) line for some reason, you can create a local variables section at the end of the file (in the last 3000 characters of the file, and on the last page, according to the manual ):如果由于某种原因不能把它放在第一(第二)行,你可以在文件的末尾(文件的最后 3000 个字符,最后一页, 根据手册):

# Local Variables:
# mode: sh
# End:

Note that just setting the Emacs mode will still rely on a shebang line for shell type autodetection, and if no shebang line is detected will default to the current SHELL environment variable or the value of sh-shell-file if set).请注意,仅设置 Emacs 模式仍将依赖 shebang 行进行 shell 类型自动检测,如果未检测到 shebang 行,将默认为当前的SHELL环境变量或sh-shell-file的值(如果已设置)。

If you can't have a shebang line, but want the correct shell type to be selected, the only way to do this is with an eval in the mode line or local variables section.如果您没有 shebang 行,但希望选择正确的 shell 类型,那么唯一的方法是在模式行或局部变量部分使用eval Adding this will generate a confirmation prompt every time the file is loaded into Emacs, so this is not generally recommended, but may be acceptable in some cases.添加此项会在每次将文件加载到 Emacs 时生成确认提示,因此通常不建议这样做,但在某些情况下可能是可以接受的。 The mode line would be -*- mode: sh; eval: (sh-set-shell "zsh") -*-模式行将是-*- mode: sh; eval: (sh-set-shell "zsh") -*- -*- mode: sh; eval: (sh-set-shell "zsh") -*- , and the local variables form would be: -*- mode: sh; eval: (sh-set-shell "zsh") -*- ,局部变量形式为:

# Local Variables:
# mode: sh
# eval: (sh-set-shell "zsh")
# End:

If you use the shebang method, a more robust form is如果你使用shebang方法,一个更健壮的形式是

#!/usr/bin/env zsh
# env will search the path for zsh.  Some distros may put it a different place.
# env is pretty much guaranteed to be in /usr/bin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM