Reputation: 637
in my vimrc I have a script that transform a text in bold when banded between * * double stars * * (like it does in this editor actually), but i don't want it to apply on my js or c or any programming files of course, so i tried to make it run only when it's a .txt file :
if (&filetype=='text')
set concealcursor=n
set conceallevel=3
hi AsteriskBold ctermfg=Green cterm=bold
autocmd BufEnter * syn match Asterisks contained "**" conceal
autocmd BufEnter * syn match AsteriskBold "\*\*.*\*\*" contains=Asterisks
endif
but obviously the condition of the "if" doesn't work since this rules doesn't apply anymore in none of my file, text or not
EDIT => SOLUTION
after reading the answers I choose this solution, in my vimrc (even though it's not the best way vim works as explaind by ingo)
au BufEnter *.txt setf text "(set a filetype unless it already exist)
au filetype text set cocu=n cole=3
au filetype text hi AsteriskBold ctermfg=Green cterm=bold
au filetype text syn match Asterisks contained "**" conceal
au filetype text syn match AsteriskBold "\*\*.*\*\*" contains=Asterisks
Upvotes: 1
Views: 2549
Reputation: 172530
Filetype-specific settings go into ~/.vim/after/ftplugin/text.vim
. (This requires that you have :filetype plugin on
; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/text.vim
.) Alternatively, you could define an :autocmd FileType text ...
directly in your ~/.vimrc
, but this tends to become unwieldy once you have many customizations.
For :syntax
commands, there's a corresponding directory ~/.vim/after/syntax/text.vim
. (Vim currently doesn't ship with a dedicated text
syntax; you could drop the after
part, and make your syntax the main one.)
By syntax script convention, your syntax groups should be prefixed with the filetype; e.g. textAsterisks
. The :hi
group has to be renamed as well; however, usually syntax scripts use :hi def link
to link the syntax group to a (more generic) highlight group: hi def link textAsteriskBold AsteriskBold
. More information at the end of usr_44.txt
.
These are global, you can put your :hi
command(s) directly into your ~/.vimrc
and define it just once.
The conceal settings are window-local, but filetypes and syntaxes apply to buffers. And by using :set
(instead of :setlocal
), those settings will be inherited by any new window opened from the one that shows a text file. Depending on your workflow (and whether other filetypes you edit use concealing at all), you may never notice this, and there's no good workaround (only a huge mess of :autocmd
could try to adapt those). Just be aware of this.
Upvotes: 8
Reputation: 31040
You're looking for an augroup
.
See :help augroup
and :help filetype
.
For instance:
augroup asteriskbold
au!
au BufNewFile,BufRead *.txt,*.md,*.mkd,*.markdown,*.mdwn set concealcursor=n conceallevel=3
au BufNewFile,BufRead *.txt,*.md,*.mkd,*.markdown,*.mdwn hi AsteriskBold ctermfg=Green cterm=bold
au BufNewFile,BufRead *.txt,*.md,*.mkd,*.markdown,*.mdwn syn match Asterisks contained "**" conceal
au BufNewFile,BufRead *.txt,*.md,*.mkd,*.markdown,*.mdwn syn match AsteriskBold "\*\*.*\*\*" contains=Asterisks
augroup end
Upvotes: 2