How to make Vim detect filetype from the shebang line? How to make Vim detect filetype from the shebang line? bash bash

How to make Vim detect filetype from the shebang line?


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

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


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

fun! s:DetectNode()    if getline(1) == '#!/usr/bin/env node'        set ft=javascript    endifendfunautocmd BufNewFile,BufRead * call s:DetectNode()


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

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

This contains a pattern for node -> javascript highlighting.

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