Vim:针对名称中没有扩展名的特定文件类型的自动命令?

Vim: Autocommand for a specific file type that has no extension in the name?

autocommand BufEnter,TextChanged,InsertLeave *.sh Neomake

我正在尝试实现上述命令的无扩展shell脚本的效果。

:help autocommand

显示一堆选项,包括autocmd-patterns

:help autocmd-patterns

这样说:

The pattern is interpreted like mostly used in file names:
* matches any sequence of characters; Unusual: includes path separators
? matches any single character
? matches a '?'
. matches a '.'
~ matches a '~'
, separates patterns
, matches a ','
{ } like ( ) in a |pattern|
, inside { }: like | in a |pattern|
} literal }
{ literal {
\{n,m} like {n,m} in a |pattern|
\ special meaning like in a |pattern|
[ch] matches 'c' or 'h'
[^ch] match any character but 'c' and 'h'
...
It is possible to use |pattern| items, but they may not work as expected, because of the translation done for the above.

因此,如果我们想要匹配不包含 .<some-suffix> 的内容,我们可以使用类似

的内容进行修改
autocommand BufEnter,TextChanged,InsertLeave *[^.]\\{4\} Neomake

这将匹配任何至少 4 位(可能 5 位,取决于 * 是否匹配空字符串)数字长且没有 \..{0,3} 后缀(例如 .txt 等)。如果你有更长的后缀,你将不得不增加你的数字,这反过来会增加它将匹配的 non-suffixed 文件名的最小长度。这是一个丑陋的 hack,但我不相信有更好的方法。

当缓冲区的文件类型设置为 Shell 文件类型之一(例如 shbash)时,您可以为缓冲区创建 buffer-local autocommands[=] 19=]

例如:

autocmd FileType sh,bash autocmd BufEnter,TextChanged,InsertLeave <buffer> Neomake

您还可以使用 filetype plug-in,将以下内容添加到名为 ~/.vim/ftplugin/bash.vim 的文件中(假设您的 shell 脚本将 'filetype' 设置为 bash ):

autocmd BufEnter,TextChanged,InsertLeave <buffer> Neomake