Unable to set syntax highlighting automatically in Vim

I have the following in my .vimrc

syntax on
filetype plugin indent on       # Thanks to Jeremy

I run

vim ~/.vimrc

I get the right syntax highlighting.

I source many files in my .vimrc. My .vimrc is a like a roadmap for me where I navigate by

CTRL-W f

The problem occurs when I navigate to a file which I have sourced: no colors.

All my sourced files contain the word Vim in their PATHs. It may be possible to use this fact in solving the problem.

How can you provide a syntax highlighting automatically for the sourced files?

Upvotes: 6

Views: 12739

Answers (5)

ekerner
ekerner

Reputation: 5840

To make vi consider my jQuery (.jq) files are actually javascript (.js) I did: -

Create and/or or edit your vimrc file ...

e@dev3:~$ vi ~/.vimrc

Add the following text (press i to insert) ...

if has("syntax")
  syntax on
  filetype on
  au BufNewFile,BufRead *.jq set filetype=javascript
endif

Save the vimrc file ...

[esc]:wq[enter]

Further, to find supported filetypes look in filetype.vim ...

e@dev3:~$ sudo locate filetype.vim
/usr/share/vim/vim72/filetype.vim
e@dev3:~$ sudo grep "\.js[, ]" `locate filetype.vim`
au BufNewFile,BufRead *.js,*.javascript,*.es,*.jsx      setf javascript

... the filetype is the setf arg ...

e@dev3:~$ sudo grep "\.js[, ]" `locate filetype.vim` | cut -d " " -f 4
javascript

Have fun.

Upvotes: 3

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143154

Do the files in question end in ".vim"? If not, then vim's filetype detection may not be able to determine that these files contain vim-script. You can either rename the files so that they end in .vim, or add an autocommand to set the filetype appropriately.

To do the latter, you can add something like this to your .vimrc:

au! BufNewFile,BufRead PATTERN set filetype=vim

replacing "PATTERN" with a file pattern that will match the files in question.

EDIT:

See :help autocmd-patterns for how the patterns work:

The file pattern {pat} is tested for a match against the file name in one of
two ways:
1. When there is no '/' in the pattern, Vim checks for a match against only
   the tail part of the file name (without its leading directory path).
2. When there is a '/' in the pattern,  Vim checks for a match against the
   both short file name (as you typed it) and the full file name (after
   expanding it to a full path and resolving symbolic links).

In particular, note this example:

Note:  To match part of a path, but not from the root directory, use a '*' as
the first character.  Example: >
    :autocmd BufRead */doc/*.txt    set tw=78
This autocommand will for example be executed for "/tmp/doc/xx.txt" and
"/usr/home/piet/doc/yy.txt".  The number of directories does not matter here.

In your case you probably want something like:

au! BufNewFile,BufRead */Vim/* set filetype=vim

Upvotes: 11

Jeremy Smyth
Jeremy Smyth

Reputation: 23493

It could be:

  • the "filetype" option
  • the filetype might not be auto-detected by vim

filetype on sorts the first, and the second is fixable with autocmds based on the file extensions.

Upvotes: 0

Michael Madsen
Michael Madsen

Reputation: 55009

What is the extension of the files you source? The extension is the usual way for Vim to detect what syntax highlighting it neds to use, and for source-able files (vimscript) it should be .vim. It sounds like that's not the case, if you only see the problem with the sourced files, and not with any others.

Upvotes: 1

Rob Wells
Rob Wells

Reputation: 37103

One obvious question is there's no line saying "syntax off" in the files you're sourcing?

Upvotes: 0

Related Questions