Search code examples
bashvimfile-type

How to make Vim detect filetype from the shebang line?


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 node is javascript). What should I put into filetype.vim?


Solution

  • 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()
        finish
    endif
    if getline(1) =~# '^#!.*/bin/env\s\+node\>'
        setfiletype javascript
    endif