Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set syntax highlighting automatically in Vim

Tags:

syntax

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?

like image 972
Léo Léopold Hertz 준영 Avatar asked Jun 26 '09 22:06

Léo Léopold Hertz 준영


People also ask

How do I enable syntax highlighting in vim by default?

Syntax highlighting is on for vim editor by default. The content of login.sh will be displayed with the following format when the syntax highlighting is on. After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting.

How do I enable Python syntax highlighting in vim?

The command to enable syntax highlighting in vim is :syntax on , if you want it to be active everytime you launch vim, just add a line containing syntax on in your . vimrc file. maybe your vim doesn't have filetype detection enabled, try adding filetype on to your .

How do I enable colors in vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.


2 Answers

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
like image 69
Laurence Gonsalves Avatar answered Sep 21 '22 22:09

Laurence Gonsalves


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.

like image 22
ekerner Avatar answered Sep 22 '22 22:09

ekerner