Vim:如何通过“#!”设置无扩展名文件的文件类型?
如何将 #!/usr/bin/env node
文件类型的文件设置为 javascript?
How could I set file with #!/usr/bin/env node
filetype to javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
利用模型行。
添加以下行:
有关 vim 模型行的更多信息。
Make use of modeline.
Put the following line:
more info about vim's modeline.
在 vim 中,
:e $VIMRUNTIME/scripts.vim
。当文件名中没有告诉 vim 类型的内容(如扩展名)时,该文件会对“脚本”进行文件类型检测。如果您搜索“#!”在此文件中,您将看到执行此操作的逻辑。问题是,这是一堆特殊情况,并且没有一个细粒度的钩子来实现您想要做的事情。一种可能是修改此 script.vim 文件,并将您的修改提交回以包含在 Vim 中。如果您不想修改 vim 附带的文件,那么另一种方法是在您的个人运行时路径中创建您自己的
scripts.vim
(使用:set rtp?
来查看它在您的系统上的设置)。除了 $VIMRUNTIME 中的运行之外,这还将运行。您可以包含用于查看第一行并检测“节点”的逻辑。您可能希望按照$VIMRUNTIME/scripts.vim
中的逻辑对代码进行建模。In vim,
:e $VIMRUNTIME/scripts.vim
. This is the file that does filetype detection of "scripts" when there isn't something in the filename (like an extension) that tells vim the type. If you search for "#!" in this file you'll see the logic that does this. The problem is, it's a bunch of special cases and there isn't a fine-grained hook for what you want to do. One possibility would be to modify this scripts.vim file and also submit your modification back to be included in Vim.If you don't want to modify files that came with vim then an alternative is to create your own
scripts.vim
in your personal runtimepath (use:set rtp?
to see what it's set to on your system). This will be run in addition to the one in $VIMRUNTIME. You can include your logic for looking at the first line and detecting "node". You'll probably want to model your code after the logic in$VIMRUNTIME/scripts.vim
.autocommand
可以用来检查文件类型(这是如何$VIMRUNTIME/scripts.vim
已加载)。...
将其添加到您的
.vimrc
- 打开新文件时将调用它:autocommand
can be used to check file types (this is how$VIMRUNTIME/scripts.vim
is loaded)....
Add this to your
.vimrc
- it will be invoked when a new file is opened: