Highlight txt files but not help files - vim

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

Related

Vim autocmd for FileType not working indirectly

I have a custom ~/.vimrc I made with this augroup:
augroup filetype_vim
autocmd!
autocmd! BufWritePost .vimrc* source %
autocmd FileType vim |
setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
augroup END
When I open vim directly to edit the ~/.vimrc like this: vim ~/.vimrc, the folding works as expected, I can fold {{{ marker:
:set foldmethod?
> foldmethod=marker
When I open vim without specifying a file: vim, and then trying to edit: :e ~/.vimrc, the foldmethod is different!
:set foldmethod?
> foldmethod=syntax
Which obviously comes from a different part of my vimrc.
Why doesn't it recognizes the file type when I open the file indirectly?
You've failed with VimScript syntax. Must be
autocmd FileType vim
\ setlocal foldlevel=0 foldmethod=marker foldmarker={{{,}}}
What you did instead is
autocmd FileType vim <nothing> | <nothing>
setlocal foo bar
Therefore setlocal applies to the current buffer only (i.e. command-line argument), not to anything else.

set a colorscheme for a filetype in vim

Within vim, I am trying to use a special colorscheme for some filetypes using an autocmd. Strangely, that does not work for all the filetypes. Here is my vimrc:
autocmd filetype troff :colorscheme troff
autocmd filetype tintin :colorscheme troff
autocmd BufNewFile,BufRead *.tt set ft=tintin
autocmd BufNewFile,BufRead *.tr set ft=troff
While openning f.tr, the colorscheme "troff" is used, but while openning f.tt, while the filetype is correctly set to "tintin", the default colorscheme is used. If I manually set the filetype (sef ft=tintin), then the colorscheme troff is loaded. Could you please help me to figure what could cause that strange behaviour?
I cannot reproduce your problem. However I would suggest the following auto commands:
autocmd BufNewFile,BufRead *.tt let g:tmpcolor=g:colors_name
autocmd BufNewFile,BufRead *.tr let g:tmpcolor=g:colors_name
autocmd BufEnter *.tt colorscheme troff | set ft=tintin
autocmd BufEnter *.tr colorscheme troff | set ft=troff
autocmd BufLeave *.tt exe 'colorscheme '.g:tmpcolor
autocmd BufLeave *.tr exe 'colorscheme '.g:tmpcolor
This will create a variable g:tmpcolor that stores the original color scheme. When you edit a file of type .tt or .tr the color scheme will change to troff. When you leave these files, the color scheme will change to g:tmpcolor.

how to make file extensions to syntax highlighting in vim?

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.

In vim, why my autocmd setfiletype command not working?

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

Enable syntax highlighting for various filetypes in vim

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/

Resources