I am using vim 8.1 on macOS (vim-plug for plugin management). I need to work with COOL files, so I manually downloaded the syntax file and put it in ~/.vim/syntax
I added the following line to my .vimrc in order to associate the .cl extension with COOL:
au BufNewFile,BufRead *.cl setf cool
However, when I load .cl files into vim, it fails to recognise the language.
:verbose set ft ?
yields:
filetype=lisp
Last set from /usr/local/Cellar/vim/8.1.0650/share/vim/vim81/filetype.vim line 875
Changing the .virmc line to:
au BufNewFile,BufRead *.cool setf cool
makes vim recognise the files as COOL.
What should I do in order to have .cl default to COOL instead of LISP?
Create the file ~/.vim/filetype.vim with the following content:
augroup filetypedetect
au BufNewFile,BufRead *.cl setf cool
augroup END
See :help new-filetype.
Related
When trying to automatically open the corresponding .cpp or .h file using autocommand I encounter no colorscheme on the corresponding file that is opened.
I'm not too familiar with vimscript but I believe Vim is opening the file thinking it is of file type ".txt" and therefore using a default colorscheme.
Two autocommand lines in ~/.vimrc:
au BufRead,BufNewFile *.cpp exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h exe "vsplit" fnameescape(expand("%:r").".cpp")
Any help would be appreciated.
Your answer is a workaround (though you should use :setlocal instead of :set to avoid that the syntax leaks out to new buffers that are opened from that one), but it doesn't attack the root cause, which you'll find explained at :help autocmd-nested:
By default, autocommands do not nest. If you use ":e" or ":w" in an autocommand, Vim does not execute the BufRead and BufWrite autocommands for those commands. If you do want this, use the "nested" flag for those commands in which you want nesting.
Syntax highlighting (you say colorscheme in your title, but that's actually just the color and font attributes that are then used by syntax highlighting) is based on :autocmd events (same goes for filetype plugins, so any C++-related settings you also wouldn't find in the split file, assuming you have :filetype plugin on in your ~/.vimrc). Without the nested attribute, the split file will be opened, but none of the usual actions will be run on them. Though nesting in general can be problematic, this is one of those cases where it is needed.
au BufRead,BufNewFile *.cpp nested exe "bel vsplit" fnameescape(expand("%:r").".h")
au BufRead,BufNewFile *.h nested exe "vsplit" fnameescape(expand("%:r").".cpp")
Unfortunately, this introduces another problem: The one autocmd will trigger the other one, and vice versa (up to a limit). You need to guard the actions so that a split is only done if the file isn't open yet. (This also improves on the usability in a general way, when you open a file with the other already open.) :help bufwinnr() checks whether the target buffer is already visible in a window:
au BufRead,BufNewFile *.cpp nested if bufwinnr("^" . expand("%:r").".h$") == -1 | exe "bel vsplit" fnameescape(expand("%:r").".h") | endif
au BufRead,BufNewFile *.h nested if bufwinnr("^" . expand("%:r").".cpp$") == -1 | exe "vsplit" fnameescape(expand("%:r").".cpp") | endif
If anyone cares to look at this in the future the solution was that Vim was loading the second file as syntax=none. So adding | set syntax=cpp at the end of each auto command fixed it.
I'm trying to detect buck's TARGETS file as 'buck' files instead of 'python', which is the filetype vim's currently detecting for them.
I tried to add autocmd BufRead,BufNewFile **/TARGETS set filetype=buck to my .vimrc but it didn't seem to work.
What's the correct way to change a 'default' detected file type ?
Your autocmd is the way to go in principle; just omit the **/ in the pattern:
autocmd BufRead,BufNewFile TARGETS set filetype=buck
see :help autocmd-patterns
Ideally, you'd have this line in a .vim/ftdetect/buck.vim file
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?
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
My ~/.vimrc contains the following line:
au BufNewFile,BufRead *.fsh,*.vsh setf glsl
My ~/.vim/syntax directory contains glsl.vim that works if loaded manually (:setf glsl in Vim), but it doesn't load automatically when editing a file with those extensions. How do I get syntax highlighting for those files automatically? Syntax highlighting works for other files as usual. My Vim version is 7.4 running on Linux Mint.
I don't think that line can be in your ~/.vimrc. Due to loading order, it may have to be in ~/.vim/filetype.vim instead. Try putting this into that file:
augroup filetypedetect
au BufNewFile,BufRead *.fsh,*.vsh setf glsl
augroup END
The GLSL syntax highlighter http://www.vim.org/scripts/script.php?script_id=1002 works well. But it doesn't recognize .frag or .vert shader files that commonly begin with a line like #version 330. (This is on Ubuntu 12.04.2 LTS, everything up to date.)
With such a line, after loading the file one must manually type set syntax=glsl,
probably because /usr/share/vim/vim73/filetype.vim has
" Generic configuration file (check this last, it's just guessing!)
if ... getline(1)=~'^#' ... setf conf.
Why doesn't that get overruled by ~/.vimrc's au BufNewFile,BufRead *.frag,*.vert setf glsl? Is there a workaround short of blundering about in filetype.vim?
How can I stop vim from loading a syntax file automatically for certain file types? came close. Here's the workaround, a file ~/.vim/filetype.vim:
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
" Override filetypes for certain files
autocmd! BufNewFile,BufRead *.frag setfiletype glsl
autocmd! BufNewFile,BufRead *.vert setfiletype glsl
augroup END