David Levner
David Levner

Reputation: 341

Why doesn't vim recognize a new file type?

I am trying to get vim to recognize a filetype.vim file consisting of the following:

if exists("did_load_filetypes")
  finish
endif

augroup autodetect
  au! BufRead,BufNewFile *.ish  setfiletype perl
augroup END

This file is in the directory ~/programs/vim

My .vimrc file contains the following line:

set runtimepath=~/programs/vim,$VIMRUNTIME

I checked that this line is being executed by typing ":set runtimepath?" The result is "runtimepath=~/programs/vim,/usr/share/vim/vim74".

But when I open a file such as ish.ish, the vim filetype variable is set to 'on'. When I open x.pl, the vim filetype variable is set to 'perl'.

I can fix the problem by copying (or moving) filetype.vim to the ~/.vim directory (without changing runtimepath). Why doesn't vim recognize filetype.vim in ~/programs/vim?

Upvotes: 0

Views: 1166

Answers (1)

romainl
romainl

Reputation: 196456

The 'runtimepath' option is meant to tell Vim where to look for "standard" *.vim files such as colorschemes and plugins. Try :echo &rtp in a clean Vim session to see what it should look like and read :help 'runtimepath'. By setting this option to a meaningless value you effectively make Vim unable to find those files and thus work correctly.

To add a specific directory to 'runtimepath', use the following syntax:

set runtimepath+=/path/to/directory

But what's the reason you'd want to use a non-standard directory for standard scripts? What's wrong with ~/.vim?

The most obvious way to tell Vim about a new filetype is to add these lines to your ~/.vimrc:

augroup autodetect
  autocmd!
  autocmd BufRead,BufNewFile *.ish set filetype=perl
augroup END

The cleanest way is to put this line in ~/.vim/ftdetect/ish.vim:

autocmd BufRead,BufNewFile *.ish set filetype=perl

Upvotes: 4

Related Questions