I have *.q files ( that are very similar to other SQL ) and I want to make vim syntax highlight it in the same way as SQL.
create a ~/.vim/ftdetect directory if not existing.
create a ~/.vim/ftdetect/<new-file-type-extention>.vim with content:
au BufRead,BufNewFile *.<new-file-type-extention> set filetype=<existing-file-type-extention>
Include it in the ~/.vim/filetype.vim, as described at Vim FAQ 26.8:
" my filetype file
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
au! BufRead,BufNewFile *.x setfiletype c
augroup END
For this case it would be changed to something like this:
au! BufRead,BufNewFile *.q setfiletype sql
Check :help new-filetype for additional details.
Related
I would like to apply the following autocmd:
autocmd BufNewFile,BufRead *.txt set filetype=markdown
autocmd BufNewFile,BufRead *.txt colorscheme OceanicNext
However, I do not want this to apply to any vim help files that are loaded whenever I do :h <something>. Is there a way to exclude that from the autocmd?
Use FileType event whenever possible.
Use setfiletype to set filetype if it wasn't already set.
~/.vim/ftdetect/markdown.vim
autocmd BufNewFile,BufRead *.txt setf FALLBACK markdown
~/.vim/after/ftplugin/markdown.vim
setlocal foo bar
I'm going through the VimCasts.org archive of videos and number 5 covers using Vim's auto-indentation to format source code. I see it working properly in my Objective-C file but not my .vimrc.
My tab settings are as follows:
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
My .vimrc file has the following if block:
if has("autocmd")
filetype on
autocmd BufNewFile,BufRead *.rss,*.atom setfiletype xml
autocmd BufWritePre *.py,*.js :call <SID>StripTrailingWhitespaces()
endif
I would think that if I placed the cursor on the first line above and pressed Vjjjj= I would get the second, third and fourth line indented by two spaces, but what I get instead is:
if has("autocmd")
filetype on
autocmd BufNewFile,BufRead *.rss,*.atom setfiletype xml
autocmd BufWritePre *.py,*.js :call <SID>StripTrailingWhitespaces()
endif
Are my expectations incorrect or is this correct for some reason given the Vimscript language?
You need to add filetype plugin indent on to your vimrc to get vim to do indentation properly. (The plugin part isn't really necessary but is nice to have)
I would recommend replacing the filetype on line with filetype plugin indent on
I am trying to add :autocmd BufWrite * make to modeline in vim, but it is not triggered. I see that the help says only set <options> can be used in modelines. Is there a way to achieve this effect, where I write a file to run make.
Set your autocmd in your ~/.vimrc with a pattern that will match your file name. I would also suggest using augroup so it can be made to be re-sourced.
augroup my_project
autocmd!
autocmd BufWrite /project/*.c make
augroup END
For more help see:
:h :au
:h :aug
In my .vimrc I add this,
autocmd BufNewFile,BufRead *.markdown setfiletype octopress
But it seems not working because after I open a xxx.markdown file and input the command setfiletype octopress everything works fine.
Here is my intact .vimrc
set nocompatible
syntax on
filetype off
colorscheme desert
set nu
set mouse=a
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
Bundle 'gmarik/vundle'
filetype plugin indent on
Bundle 'vim-octopress'
autocmd FileType markdown setfiletype octopress
Hope someone can help me and tell me how to debug this thing...
I don't see this:
autocmd BufNewFile,BufRead *.markdown setfiletype octopress
I only see this at the end:
autocmd FileType markdown setfiletype octopress
I think fixing that will fix you issue.
Update:
A couple more things to consider. First, the Markdown-syntax plugin sets the filetype to be mkd, not markdown. This doesn't seem to work correctly either:
autocmd FileType mkd setfiletype octopress
But this does:
au FileType mkd set filetype=octopress
...And that makes sense now. setfiletype won't set the file type if it's already been set. Since it was already flagged as being of type mkd, it wasn't being updated to the new file type.
It's because setfiletype ... but only if not done yet in a sequence of (nested) autocommands.
Doc about setfiletype function
filetype plugin on
filetype indent on
au FileType htm,html,php,css setl ts=2 sw=2 sts=2 et
I can enable syntax highlighting for a file that has an extension that is unknown to vim by doing the following
set syntax=c
Every time I switch tabs however, I have to renter the command. Is there any way to let vim know that a file with an extension .xyz should be coloured with C syntax?
Put this at the end of your .vimrc (I'm assuming you have autocommands enabled).
autocmd BufRead,BufNewFile *.xmlx set filetype=xml
In your home directory, create the .vim/ftdetect/xyz.vim:
au BufRead,BufNewFile *.xyz set filetype=c " to overrule an existing filetype
au BufRead,BufNewFile *.xyz setfiletype c " to set it only if no filetype has been detected for this extension
With autocommand. E.g.
au BufNewFile,BufRead *.xyz setf c
You can set it in the vim config file:
http://beerpla.net/2008/04/02/how-to-add-a-vim-file-extension-to-syntax-highlighting/