Linux 如何在 Vim 中突出显示 Bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2576687/
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-08-03 19:55:11  来源:igfitidea点击:

How to highlight Bash scripts in Vim?

linuxbashvimsyntax-highlightingvim-syntax-highlighting

提问by never_had_a_name

My Vim editor auto highlights PHP files (vim file.php), HTML files (vim file.html) and so on.

我的 Vim 编辑器会自动突出显示 PHP 文件 ( vim file.php)、HTML 文件 ( vim file.html) 等。

But when I type: vim fileand inside it write a Bash script, it doesn't highlight it.

但是当我输入:vim file并在其中编写一个 Bash 脚本时,它并没有突出显示它。

How can I tell Vim to highlight it as a Bash script?

我如何告诉 Vim 将它高亮显示为 Bash 脚本?

I start typing #!/bin/bashat the top of the file, but it doesn't make it work.

我开始#!/bin/bash在文件的顶部打字,但它不起作用。

采纳答案by Mark Rushakoff

Are you correctly giving the shell script a .shextension? Vim's automatic syntax selection is almost completely based on file name (extension) detection. If a file doesn't have a syntax set (or is the wrong syntax), Vim won't automatically change to the correct syntax just because you started typing a script in a given language.

您是否正确地为 shell 脚本提供了.sh扩展名?Vim 的自动语法选择几乎完全基于文件名(扩展名)检测。如果一个文件没有设置语法(或者是错误的语法),Vim 不会因为你开始用给定的语言键入脚本而自动更改为正确的语法。

As a temporary workaround, the command :set syn=shwill turn on shell-script syntax highlighting.

作为临时解决方法,该命令:set syn=sh将打开 shell 脚本语法突出显示。

回答by Matteo Riva

Vim can also detect file types by inspecting their contents (like for example if the first line contains a bash shebang), here is a quote from filetype.txthelp file:

Vim 还可以通过检查文件的内容来检测文件类型(例如,如果第一行包含 bash shebang),这是filetype.txt帮助文件中的引用:

If your filetype can only be detected by inspecting the contents of the file

如果您的文件类型只能通过检查文件内容来检测

Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Example for Unix:

创建您的用户运行时目录。您通常会使用 'runtimepath' 选项的第一项。Unix 示例:

:!mkdir ~/.vim

Create a vim script file for doing this. Example:

为此创建一个 vim 脚本文件。例子:

if did_filetype()   " filetype already set..
    finish      " ..don't do these checks
endif
if getline(1) =~ '^#!.*\<mine\>'
    setfiletype mine
elseif getline(1) =~? '\<drawing\>'
    setfiletype drawing
endif

See $VIMRUNTIME/scripts.vim for more examples. Write this file as "scripts.vim" in your user runtime directory. For example, for Unix:

有关更多示例,请参阅 $VIMRUNTIME/scripts.vim。在您的用户运行时目录中将此文件写为“scripts.vim”。例如,对于 Unix:

:w ~/.vim/scripts.vim

The detection will work right away, no need to restart Vim.

检测将立即生效,无需重新启动 Vim。

Your scripts.vim is loaded before the default checks for file types, which means that your rules override the default rules in $VIMRUNTIME/scripts.vim.

你的 scripts.vim 在文件类型的默认检查之前加载,这意味着你的规则覆盖 $VIMRUNTIME/scripts.vim 中的默认规则。

回答by raimue

vim already recognizes many file types by default. Most of them work by file extensions, but in a case like this, vim will also analyze the content of the file to guess the correct type.

默认情况下,vim 已经可以识别许多文件类型。它们中的大多数通过文件扩展名工作,但在这种情况下,vim 也会分析文件的内容以猜测正确的类型。

vim sets the filetype for specific file names like .bashrc, .tcshrc, etc. automatically. But a file with a .shextension will be recognized as either csh, ksh or bash script. To determine what kind of script this is exactly, vim reads the first line of the file to look at the #! line.

VIM将像特定文件名的文件类型.bashrc.tcshrc自动等。但是带有.sh扩展名的文件将被识别为 csh、ksh 或 bash 脚本。为了确定这到底是什么类型的脚本,vim 读取文件的第一行以查看 #! 线。

If the first line contains the word bash, the file is identified as a bash script. Usually you see #!/bin/bashif the script is meant to be executed directly, but for a shell configuration file using a simple # bashwould work as well.

如果第一行包含单词bash,则该文件被标识为 bash 脚本。通常您会看到#!/bin/bash脚本是否打算直接执行,但对于使用 simple 的 shell 配置文件# bash也可以。

If you want to look at the details, this is implemented in $VIMRUNTIME/filetype.vim.

如果要查看详细信息,这是在$VIMRUNTIME/filetype.vim.

回答by bryant

The answers so far are correct that you can use the extension (like .sh) or a shebang line (like #!/bin/bash) to identify the file type. If you don't have one of those, you can still specify the file type manually by using a modeline comment at the top or bottom of your file.

到目前为止的答案是正确的,您可以使用扩展名(如.sh)或shebang 行(如#!/bin/bash)来识别文件类型。如果您没有其中之一,您仍然可以通过使用文件顶部或底部的模式行注释手动指定文件类型。

For instance, if you want to identify a script without an extension as a shell script, you could add this comment to the top of your file:

例如,如果要将没有扩展名的脚本标识为 shell 脚本,则可以将此注释添加到文件顶部:

# vim: set filetype=sh :

or

或者

# vim: filetype=sh

That will tell vim to treat the file as a shell script. (You can set other things in the modeline, too. In vim type :help modelinefor more info.)

这将告诉 vim 将该文件视为 shell 脚本。(您也可以在模式行中设置其他内容。在 vim 中键入:help modeline以获取更多信息。)

回答by hobbs

When you create a new file, only the filename detection comes into play; content detection (#!/bin/bash) doesn't apply if you type it after creating a new buffer.

创建新文件时,只有文件名检测起作用;#!/bin/bash如果在创建新缓冲区后键入内容检测 ( ) 则不适用。

The sensible thing is to just do :set ft=bashthe first time around, and the nexttime you edit it, the #!/bin/bashwill set the right filetype automatically.

明智的做法:set ft=bash是第一次做,下次编辑时,它#!/bin/bash会自动设置正确的文件类型。

回答by cmaster - reinstate monica

Probably the easiest way to get syntax highlighting on a new file, is to just reload it after writing the shebang line. A simple :w:ewill write out the file, reload it, and interprete the shebang line you have just written to provide you with the appropriate syntax highlighting.

可能在新文件上突出显示语法的最简单方法是在编写 shebang 行后重新加载它。一个简单的:w:e将写出文件,重新加载它,并解释您刚刚编写的 shebang 行,为您提供适当的语法突出显示。

回答by Arun Karikalan

vim can detect by the top comment

vim 可以通过顶部注释检测到

try this line at the top of the file

在文件顶部尝试这一行

#!/bin/sh

回答by Gaurav Khare

Actually syntax highlighting is a feature of vim not vi. Try using vim command and then do

实际上语法高亮是vim 的一个特性,而不是vi。尝试使用 vim 命令然后执行

:syntax on.

:syntax on.

回答by dash-tom-bang

Once you add the shebang at the top of the file, save it and reload it (e.g. :w|e) and syntax coloring can kick in.

在文件顶部添加 shebang 后,保存并重新加载它(例如:w|e),语法着色就可以开始了。

See also Vim inconsistently syntax highlighting bash files, the accepted answer may help as well.

另请参阅Vim 不一致的语法突出显示 bash 文件,接受的答案也可能有所帮助。

回答by Thanatos

I came to this answer looking for specifically how to highlight bashsyntax, not POSIX shell. Simply doing a set ft=sh(or equivalent) will result in the file being highlighted for POSIX shell, which leaves a lot of syntax that's valid in bashhighlighted in red. To get bash highlighting:

我来到这个答案是专门寻找如何突出显示bash语法,而不是 POSIX shell。简单地做一个set ft=sh(或等效的)将导致文件被 POSIX shell 突出显示,这留下了许多有效的语法,bash以红色突出显示。要获得 bash 突出显示:

" Set a variable on the buffer that tells the sh syntax highlighter
" that this is bash:
let b:is_bash = 1
" Set the filetype to sh
set ft=sh

Note that if your ftis already sh, you still need the setcommand; otherwise the letdoesn't take effect immediately.

请注意,如果您ft已经是sh,您仍然需要该set命令;否则let不会立即生效。

You can make this a global default by making the variable global, i.e., let g:is_bash = 1.

您可以通过将变量设为全局变量(即let g:is_bash = 1.

:help ft-sh-syntaxis the manual page I had to find; it explains this, and how to trigger highlighting of other flavors of shell.

:help ft-sh-syntax是我必须找到的手册页;它解释了这一点,以及如何触发突出显示其他口味的贝壳。