tags:

views:

660

answers:

4

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?

A: 

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

Rob Wells
There is no line saying "syntax off".
Masi
+1  A: 

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.

Michael Madsen
The files have no extension.
Masi
A: 

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.

Jeremy Smyth
I have the following in my .vimrc: filetype plugin indent on
Masi
+5  A: 

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
Laurence Gonsalves
I put the PATTERN to be *Vim* unsuccessfully. It seems that the command considers only filenames. -- This suggests me that the easiest way is just to add the extension vim to each file.
Masi
I added a bit about autocmd patterns to the answer, though just naming the files with a .vim extension is probably the best long-term solution.
Laurence Gonsalves
@Laurence: Thank you for your evaluative arguments!
Masi