bash 如何让 Vim 从 shebang 行检测文件类型?

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

How to make Vim detect filetype from shebang line?

bashvimfile-type

提问by Lai Yu-Hsuan

Sometimes I write scripts without any filename extension. For example:

有时我编写的脚本没有任何文件扩展名。例如:

#!/usr/bin/env node

console.log('hello world!');

I hope that Vim can detect the filetype from the shebang line (e.g. #!/usr/bin/env nodeis javascript). What should I put into filetype.vim?

我希望 Vim 可以从 shebang 行(例如#!/usr/bin/env nodeis javascript)检测文件类型。我应该放入filetype.vim什么?

采纳答案by ib.

Following the instructions listed in :help new-filetype-scripts, create the scripts.vimfile in the user runtime directory (~/.vimon Unix-like systems), and write the following script in it.

按照 中列出的说明:help new-filetype-scriptsscripts.vim在用户运行时目录中创建文件(~/.vim在类 Unix 系统上),并在其中编写以下脚本。

if did_filetype()
    finish
endif
if getline(1) =~# '^#!.*/bin/env\s\+node\>'
    setfiletype javascript
endif

回答by aliva

create this file ~/.vim/ftdetect/node.vimwith this contents

~/.vim/ftdetect/node.vim使用此内容创建此文件

fun! s:DetectNode()
    if getline(1) == '#!/usr/bin/env node'
        set ft=javascript
    endif
endfun

autocmd BufNewFile,BufRead * call s:DetectNode()

回答by Andri M?ll

A little late to the party, but Node.vimhandles detecting such JavaScript files for you. And then some. :-)

聚会有点晚了,但Node.vim 会为您检测此类 JavaScript 文件。然后还有一些。:-)

回答by dhulihan

If you're interested in a plugin, one does exist for this:

如果您对插件感兴趣,那么确实存在一个插件:

https://github.com/vitalk/vim-shebang

https://github.com/vitalk/vim-shebang

This contains a pattern for node -> javascripthighlighting.

这包含用于node -> javascript突出显示的模式。

AddShebangPattern! javascript ^#!.*\s\+node\>