Different syntax highlighting between inline and display-math in vim - vim

I work on Vim with the solarized colorscheme.
In my .tex files, the syntax highlighting is different from $...$ to \[...\](or align environment).
The inline maths are in yellow, while the display-maths are in red.
The mathzone seems recognized because i'm using a math context with Ultisnips that works.
Is this difference normal, and if it is, is there a way to highlight the both in the same way ?
I precise that i'm using Vimtex.

That's a colorscheme inconsistency. Solarized defines highlight only for texMathZoneX but it does nothing with texMath or texMathZoneY. Hence the result. You can use other colorscheme or do some workaround. E.g.
augroup FixColors | au!
autocmd ColorScheme solarized
\ if &bg ==# 'dark' |
\ hi texMath ctermfg=3 ctermbg=8 guifg=#b58900 guibg=#002b36 |
\ else |
\ hi texMath ctermfg=3 ctermbg=15 guifg=#b58900 guibg=#fdf6e3 |
\ endif |
\ hi! link texMathZoneX texMath
augroup end

Related

Delegating vim filetype after removing suffix

I know how to configure vim to select a particular filetype based upon an extension:
au BufNewFile,BufRead *.foo set filetype=foo
However, a common case for me is to wind up opening a file with an additional suffix. One specific scenario is resolving version control conflicts during a merge. I'll have up files like "foo.cpp.orig" or "foo.java.merge", etc. I'd like to configure vim so that if it opens a file ending in ".orig", etc. for it to strip that suffix and use remaining file extension to select the filetype.
And yes, I do know I could do something like
au BufNewFile,BufRead *.java.* set filetype=java
But that is less than ideal because I have to manually add an entry for all the possible filetypes I might be editing.
" Ignored extensions
if exists("*fnameescape")
au BufNewFile,BufRead ?\+.orig,?\+.bak,?\+.old,?\+.new,?\+.dpkg-dist,?\+.dpkg-old,?\+.dpkg-new,?\+.dpkg-bak,?\+.rpmsave,?\+.rpmnew
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
au BufNewFile,BufRead *~
\ let s:name = expand("<afile>") |
\ let s:short = substitute(s:name, '\~$', '', '') |
\ if s:name != s:short && s:short != "" |
\ exe "doau filetypedetect BufRead " . fnameescape(s:short) |
\ endif |
\ unlet! s:name s:short
au BufNewFile,BufRead ?\+.in
\ if expand("<afile>:t") != "configure.in" |
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r")) |
\ endif
elseif &verbose > 0
echomsg "Warning: some filetypes will not be recognized because this version of Vim does not have fnameescape()"
endif
actually, Vim already considered it. the above code is from filetype.vim
After looking at the code Dyno pointed me to, I think what I want is to add
au BufNewFile,BufRead ?\+.merge
\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
to my .vimrc. It isn't quite as nice as having some variable to set to extend the list of known patterns, but isn't as much of a hack as modifying the original filetype.vim.

Is there a way to use a short circuit operator silently in vimscript?

How should I load colorscheme so that I can use lucius colorscheme only on my local machine?
http://www.vim.org/scripts/script.php?script_id=2536
This colorscheme declares a few functions to change color styles. So I tried to write my setting as following not to harm remote environment that using same vimrc file:
silent! colorscheme lucius | LuciusBlackHighContrast
But it seems like the silent! always returns success, it turns out the line giving me an error: E492: Not an editor command: LuciusBlackHighContrast.
I just wanted to make it like: colorscheme lucius >/dev/null && LuciusBlackHighContrast.
All suggestions are appreciated.
The bar is just a separator, not a boolean operator; and both colorscheme and silent! are commands, not returning any value. This is not bash :) Try this:
let v:errmsg = ""
silent! colorscheme lucius
if v:errmsg == ""
LuciusBlackHighContrast
endif

set vim filetype for files with no extension, only

How do I set up filetype and/or syntax for files that have no extension in vim?
Note
This is a duplicate of vim default syntax for files with no extension. I'm asking again because no one has answered it properly in my view.
I don't want to know how to set the default syntax for unrecognized file types, I want to know how to set it only for extensionless files.
You can create an autocommand that checks if the filename contains a ., and if not, switches to a given syntax:
autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif
This one picks perl as a default syntax, but you can simply use whichever is appropriate.
:help ftdetect will show the part of the vim documentation about how to write filetype detection scripts.
Here's what I suggest for this case:
Create a file in ~/.vim/ftdetect named after your filetype, e.g. myfiletype.vim.
In this file put
au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=myfiletype | end
This will cause vim to set the filetype for files without any extension to myfiletype. If you want it to only be used if no other filetype was detected use setfiletype myfiletype instead of set ft=myfiletype.
Then create the syntax file ~/.vim/syntax/myfiletype.vim. This is just a normal vim syntax defintion file, nothing special. If you do not want to create your own filetype, just use the normal filetype in the autocommand instead of myfiletype. For example
au BufRead,BufNewFile * if expand('<afile>:e') == '' | set ft=html | end
would set the html file type which would load the html syntax file.
1) Hit escape to make sure you're in normal mode
2) Type ":set syntax=java" (or equivalent language)
3) :set filetype=FILETYPE, where FILETYPE is the filetype.
If you're looking to do this automatically, try using the solution from previous answer:
autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif
In the end, I've chosen this for my C++ configuration:
let s:reserved = '^NERD_tree\|^GoToFile$'
au BufNewFile,BufRead *
\ if expand('%:e') =~ '^\(h\|hh\|hxx\|hpp\|ii\|ixx\|ipp\|inl\|txx\|tpp\|tpl\|cc\|cxx\|cpp\)$' ||
\ expand('%:t') !~ '\.\|'.s:reserved && expand('%:t') =~ '[a-z]' |
\ if &ft != 'cpp' |
\ set ft=cpp |
\ endif |
\ set syntax=cpp11 |
\ call CSyntaxAfter() |
\ endif
Instead of only checking for the lack of . in the tail. This avoids several non cpp files from being set like COMMIT_EDITMSG, README, GoToFile from CommandT and NERD_tree* from NERD tree.
EDIT
I just gave up on this idea.

vim highlight remove overwrite others hi

I my ~/.vimrc I use this syn for long lines
augroup longLines
au!
au! filetype zsh,sh,python,vim,c,cpp
\ syn match ColorColumn /\%>80v.\+/ containedin=ALL
augroup END
but this overwrite other syn, with
without
Why the synoverwrite other highlight?
this is notorious in the last lines
sys.exit(1)
import settings
have different colors, with syn, the lines lost normal highlight
I use the following code:
highlight TooLongLine term=reverse ctermfg=Yellow ctermbg=Red
autocmd BufEnter,WinEnter * if &tw && !exists('b:DO_NOT_2MATCH') |
\ execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
\ endif
autocmd BufLeave,WinLeave * 2match
command -nargs=0 -bar Dm let b:DO_NOT_2MATCH=1 | 2match
command -nargs=0 -bar Sm execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
\ silent! unlet b:DO_NOT_2MATCH
If you don’t want to be able to remove this highlighting, depend on textwidth and insist on highlighting spaces that go beyond the limit, then you can truncate this to just
2match TooLongLine /.\%>80v/
This solution uses match-highlight that does not scrap syntax highlighting, but always overrides it.
I realize you asked this quite some time ago, but in case if other people ask too, perhaps you could try using the matchadd() function, instead, like this:
hi def longLine gui=reverse "or guibg=pink, or whatever you prefer
augroup longLines
au!
au! filetype zsh,sh,python,vim,c,cpp
\ call matchadd("longLine", "\\%>80v", 0, 9999)
augroup END
Most importantly, make sure that you do NOT set the guifg of whatever highlight group you decide to use. That would overwrite your syntax highlighting.
Another important part (for me, at least) is to use matchadd with 0 as the third parameter so that your Search highlighting remains effective and doesn't get overtaken by the longLine highlighting.
The fourth parameter can be omitted. It's just a constant so that you can :call matchdelete(9999) to easily remove the highlighting again later, if you want.
See :h matchadd and :h matchdelete

How to select color file based on filetype?

I want to source a color file based on the filetype python. How can I do that in .vimrc?
EDIT:
ZyX makes a valid point that the use of Buf* events will not ensure that the colorscheme is set correctly for python files where the filetype is set after the file is loaded (e.g., python files without the .py extension or if you begin writing a script in a new buffer and manually set filetype=python). As per ZyX's suggestion in the comments below, here's one that uses the FileType event.
autocmd FileType * if &filetype == 'python' | colorscheme jellybeans | else | colorscheme wombat256 | endif
My previous answer is here below:
autocmd BufNewFile,BufEnter,BufRead * if &filetype == 'python' | colorscheme jellybeans | else | colorscheme wombat256 | endif
The earlier command changed the colorscheme once you entered a python file and it remained so for the rest of the session. This one lets you have one type of colorscheme for python files and another type for the others.
This is how I got syntax highlighting for cuda (*.cu and *.cuh) on my machine. Might be helpful for you (I modified it to fit python):
Download a python.vim file from here and put it somewhere (I put it in ~/)
Add this to your .vimrc
au BufRead,BufNewFile *.py set filetype=py
au! Syntax py source ~/python.vim

Resources