tags:

views:

344

answers:

2

my vim editor auto highlights php files (vim file.php), html files (vim file.html) and so on.

but when i type: "vim file" and inside it write a bash script, it doesnt highlight it.

how can i tell vim to highlight it as a bash script?

i start typing #!/bin/bash at the top of the file but it doesnt make it work.

+3  A: 

Are you correctly giving the shell script a .sh extension? Vim's automatic syntax selection is almost completely based on file name (extension) detection. If a file doesn't have a syntax set (or is the wrong syntax), Vim won't automatically change to the correct syntax just because you started typing a script in a given language.

As a temporary workaround, the command :set syn=sh will turn on shell-script syntax highlighting.

Mark Rushakoff
but the most scripts dont have a .sh extension? should i put it as a .sh extension just to make it work? is it best practice to name your scripts? and its bash...so its still .sh?
never_had_a_name
@ajsie: I'm not sure what scripts you're using, but most `bash` scripts I encounter do indeed have a `.sh` suffix. To really specify *bash* as opposed to just `sh`, you would use the `#!/bin/bash` shebang line on a `.sh` file.
Mark Rushakoff
+5  A: 

Vim can also detect file types by inspecting their contents (like for example if the first line contains a bash shebang), here is a quote from filetype.txt help file:

If your filetype can only be detected by inspecting the contents of the file

Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Example for Unix:

:!mkdir ~/.vim

Create a vim script file for doing this. Example:

if did_filetype()   " filetype already set..
    finish      " ..don't do these checks
endif
if getline(1) =~ '^#!.*\<mine\>'
    setfiletype mine
elseif getline(1) =~? '\<drawing\>'
    setfiletype drawing
endif

See $VIMRUNTIME/scripts.vim for more examples. Write this file as "scripts.vim" in your user runtime directory. For example, for Unix:

:w ~/.vim/scripts.vim

The detection will work right away, no need to restart Vim.

Your scripts.vim is loaded before the default checks for file types, which means that your rules override the default rules in $VIMRUNTIME/scripts.vim.

kemp