extend/modify vim highlighting for all filetypes at once? - vim

How do I extend/modify vim highlighting for all filetypes at once?
I have certain relatively simple patterns which I'd like highlight differently, that can occur in any filetype. So rather than adding something like the below to every conceivable filetype I might use (~/.vim/syntax/python.vim, .../css.vim, .../html.vim, ...) is there some way I can define it once for all filetypes?
syn match SpecialComment "#[#\-+].*" containedin=Comment
syn match Comment "\* .*$"hs=s+1 containedin=SpecialComment
update:
As suggested I saved my changes to ~/.vim/after/filetype.vim, with the result that it works in Cream but not stock Gvim or Vim. The actual code I'm using here, a sample python file to test against here, and the desired result:

You could try putting those two lines in ~/.vim/after/filetype.vim. That should get sourced after any of the top level syntax files. It's possibly not the 'correct' place to put it, but it should work.
filetype.vim seems to be sourced BEFORE the syntax files, so it gets overwritten by the default syntax file. Therefore, I'd recommend you create a new file called something like:
~/.vim/after/common_syntax.vim
with the highlight lines that you're interested in. Then, add this to ~/.vim/after/filetype.vim:
if !exists("after_autocmds_loaded")
let after_autocmds_loaded = 1
au BufNewFile,BufRead * source ~/.vim/after/common_syntax.vim
endif
This will cause the file to be sourced once the file has been read.
P.S. Responding to the comment in your sample code: "why can't we use plain ol 'comment' group instead of 'pythoncomment' etc. ?", it's because the syntax highlight group is pythonComment, which is merely coloured in the same way as Comment. If your syntax is unique enough for it not to be a problem, you could just do containedin=ALL. If it is close, but not quite unique, you could do containedin=ALLBUT,conflictgroup where conflictgroup is the highlight group you want to steer clear of.

Related

Color highlight function calls in VIM

Does anyone know a way to color highlight function calls in Vim?
I know that some plugins could do something like that by keeping record of tags, but with what I've found online, I could not figure out how to make it work.
I've tried using easy tags (which, by the way, doesn't seem to be maintained anymore) and gutentags, but to be quite honest, I haven't come much close to make any of them to work.
On the other hand, I imagine that it would be quite simple to implement a script to highlight anything that lies between a dot and a left parentheses or a blank space and a left parentheses (as in .anyCodeAtAll(), anotherCode()), but I have no idea how to do it. It would be a incomplete solution of course, but it would be good enough for my purposes at the moment.
Does anyone know how to make that work?
I have something like that in my configuration, but it's quite language specific. For example for Golang, I have a ~/.vim/after/go.vim which contains:
syntax match goCustomParen "(" contains=cParen
syntax match goCustomFuncDef "func\s\+\w\+\s*(" contains=goDeclaration,goCustomParen
" Exclude import as function name, for multi-line imports
syntax match goCustomFunc "import\s\+(\|\(\w\+\s*\)(" contains=goCustomParen,goImport
syntax match goCustomScope "\."
syntax match goCustomAttribute "\.\w\+" contains=goCustomScope
syntax match goCustomMethod "\.\w\+\s*(" contains=goCustomScope,goCustomParen
highlight def link goCustomMethod Function
highlight def link goCustomAttribute Identifier
highlight goCustomFuncDef ctermfg=13
highlight goCustomFunc ctermfg=43
highlight goCustomAttribute ctermfg=247
highlight goCustomMethod ctermfg=33
And for Python, I have a ~/.vim/after/python.vim:
syntax match pyCustomParen "(" contains=cParen
syntax match pyCustomFunc "\w\+\s*(" contains=pyCustomParen
syntax match pyCustomScope "\."
syntax match pyCustomAttribute "\.\w\+" contains=pyCustomScope
syntax match pyCustomMethod "\.\w\+\s*(" contains=pyCustomScope,pyCustomParen
highlight def link pyCustomFunc Function
highlight def link pyCustomMethod Function
highlight def link pyCustomAttribute Identifier
highlight pyCustomFunc ctermfg=43
highlight pyCustomAttribute ctermfg=247
highlight pyCustomMethod ctermfg=33
In each case, the first block defines what is a function, a method, an attribute, and so on, the second block link these custom definition to the generic classes "Function, Identifier..." and the 3rd block defines the colors.
The files need to be in the after directory to be executed after the colorscheme and highlights definitions.
Here's a side by side comparison of with and without these settings (look on the last 3 lines):
Unless someone has a better solution, you could adapt the above for the language you need it.
It might be that you have not installed your plug-in correctly.
Try following these steps (or retrace your steps) and see if it works / missed any steps out:
cd ~ Go to home directory.
vim .vimrc open .vimrc
Insert:
call plug#begin()
Plug 'xolox/vim-easytags'
call plug#end()
easytags#Options states easytags should work out of the box, but you could add option here in the
.vimrc file now or later,
e.g. put:
let g:easytags_syntax_keyword = 'always'
afer the call plug block.
Anyway.
:wq write quit the .vimrc
source ~/.vimrc unsure if required as will do later
vim test.js open vim with test.whatever language you know.
:PlugInstall in vim
Here might take a bit of time.
Then :q out of that window.
:source ~/.vimrc this is needed
Then test out to see if you have syntax highlighting.
I'm pretty sure this is along the right lines. Might be misspelled plugin name.

vim automatic hard wrap for fortran with line-continuation

I'm a Fortran programmer who uses both free-form and fixed form. Since I have to mix them, usually I write code in a common form between free and fixed format, so in this way I can tell to vim that all my files are in the free format.
Vim is great in doing things like autoindentation, but I would like to type and let vim automatically wrap my code, and placing the Fortran continuation character & at column 73 (or greater), and at column 6 in the new line. Is it possible, or does it exist a plugin for this?
Currently I'm using textwidth=72 in fortran files to hard wrap the lines.
Thanks in advance.
One way to make vim insert text when going to a new line is to use formatexpr. Set it so to capture the line and replace it with itself with & and new line appended, when at/beyond a given column. In this case you are handling line breaks and textwidth does not apply. I didn't yet get to test some simple code for it, but here is a related example.
Another way would be to write general code so that when in a given column it inserts & and <CR>.
However, making any such approach respect Fortran-specific exclusions (comments, for one thing) will make it more complicated. The best solution would be to find suitable existing option(s) for Fortran, but I haven't so far.
This is a comment on indentation in general. It should allow you to directly set up a desired rule for a new line. Here is one standard set of files that set up a lot of indentation rules and features.
The usual entry point is this vim script, which requires another standard set of files. The link given on that page for the other files is broken though, so here is where to find them: unpack this zip file (found on this page), right into your ~/.vim/ directory. It will create subdirectories indent/, syntax/, and ftpplugin/, or put files into them if they exist, so be careful if you have stuff there already.
Then you can put the first script linked above into .vim/after/indent/. In this file, there are specific calculations of where to put the cursor when a new line is entered. Find the right place(s) and change to your desired indent, or preferably set up a snippet from it in another file (so not to change this file). In this case you also need to set things up so that it overrides settings from the first file.
A useful resource is indentexpr (or :help indentexpr).
Here is also a tutorial on that.
These are comments on syntax in general, posted initially. They contain items of help related to what you want and should be generally useful, but probably have not much to say about adding &.
There are plugins for fortran. Here is the syntax file, with many things to tweak.
This may already be on your system. (It was on mine.) Thus I would go through and pick and choose things to add to your .vimrc. Here are a few options that are directly related
syn match fortranContinueMark display "&"
syn sync linecont "&" minlines=20
Here are paragraphs that seem to me relevant in their entirety
if (b:fortran_fixed_source == 1)
if !exists("fortran_have_tabs")
"Flag items beyond column 72
syn match fortranSerialNumber excludenl "^.\{73,}$"lc=72
"Flag left margin errors
syn match fortranLabelError "^.\{-,4}[^0-9 ]" contains=fortranTab
syn match fortranLabelError "^.\{4}\d\S"
endif
syn match fortranComment excludenl "^[!c*].*$" contains=#fortranCommentGroup
syn match fortranLeftMargin transparent "^ \{5}"
syn match fortranContinueMark display "^.\{5}\S"lc=5
else
syn match fortranContinueMark display "&"
endif
if b:fortran_dialect != "f77"
syn match fortranComment excludenl "!.*$" contains=#fortranCommentGroup,#spell
endif
Then a block of syn match statements follow for common cpp-like settings, and then
"Synchronising limits assume that comment and continuation lines are not mixed
if exists("fortran_fold") || exists("fortran_more_precise")
syn sync fromstart
elseif (b:fortran_fixed_source == 0)
syn sync linecont "&" minlines=20
else
syn sync minlines=20
endif
By your question it appears that you know how to set up .vimrc but here are a few comments.
Syntax support need be enabled with appropriate enable and autogroup statements, for example
syntax enable
" au BufRead,BufNewFile *.f90 FileType=fortran
au FileType fortran setlocal ...
Here are some common formatting options that I have for fortran
autocmd FileType fortran setlocal formatoptions=croql comments=:/!/
There can also be a t among options, for textwidth
Here are some specific settings I have, which I see in this syntax file with far more sophistication
let fortran_free_source=1
" Said to need fortran.vim and/or fortran support packages (they work)
let fortran_do_enddo=1
let fortran_more_precise=1
Standard vim help is of course extensive, but try :help fortran -- it has a number of useful settings right up front and is not overwhelming at all. Also see ft-fortran-syntax from help.
See this post with some troubleshooting if things aren't working right. Here is another useful post, even as it appears unrelated by its title.

How to add words to vim coloring in C/C++ mode?

I notice that in C/C++ mode, comments such as TODO XXX and FIXME get special color marking.
How can I add the word HACK to this list of words to be marked in the same way?
I tried adding the following to my ~/.vimrc, but it didn't work:
syn keyword cTodo contained TODO FIXME XXX HACK
I would advise against directly modifying the original syntax file; you then have to maintain your version whenever the original changes (e.g. after a Vim upgrade). For these small syntax enhancements, the place is in the ~/.vim/after/syntax/c.vim file, which is sourced after the original syntax. The line would be
syn keyword cTodo contained HACK
You need to modify the syntax file. Typically, it is in /usr/share/vim/vim72/syntax, and the file you want is c.vim and cpp.vim. You will see a line syn keyword cTodo contained followed by a list of words that are considered under the Todo label for coloring. You can add your word there, or make your own keyword, but adding your own keyword would mean adding your keyword to the coloring file as well.
For user only changes, make a directory ~/.vim/syntax. Copy the c.vim and cpp.vim files there, and edit as necessary.
Second edit: Decided to look further, and it appears you can just add to a current syntax file, but I haven't tried it. Add your one line you added to your .vimrc to a file in ~/.vim/after/syntax

highlight sub-match in vim

I'm trying to figure out how to highlight a specific portion of a match in vim.
Given the following example rule (taken from the coffeescript syntax file source):
syn match coffeeExtendedOp /\%(\S\s*\)\#<=[+\-*/%&|\^=!<>?.]\+\|[-=]>\|--\|++\|:/ display
This regular expression matches various coffeescript operators. The operators are highlighted (in my vimrc) like this:
hi Operator guifg=#ff0000
For example, since coffeeExtendedOp is linked to coffeeOperator which is linked to Operator, in the above source file. This all works, but I'm wondering how to specifically highlight the ++ operator matched in the above syn match with a different color, say blue, within my vimrc (that is, without altering the original source file above). I'm simply wondering if this is possible.
EDIT: I think the rules are placed under a cluster, so perhaps that's why it's not affecting anything. Is there a way to access the rule within the cluster?
EDIT: Question was clarified.
Solution:
syn match plusplus /++/ contained containedin=coffeeExtendedOp display
hi plusplus guifg=#0000ff
The problem now is that this only works when I run them as commands in vim, but not when I put it in my vimrc file. Any ideas? Could it be that the stuff is hidden behind the cluster? But then why is it visible in vim through a command? I tried including the syntax file but it didn't seem to have any effect.
Looking at the coffee.vim you linked to it seems like the dot belongs to the coffeeDotAccess syntax item. So you can highlight it just by doing this:
:hi coffeeDotAccess ctermfg=blue
I'm going to guess a bit at what you need. (I don't speak Coffeescript and your sample regex is way too complicated for me to start reading at the moment).
Transparent syntax items
You could have a look at transparent syntax rules: (http://vimdoc.sourceforge.net/htmldoc/usr_44.html)
In a C language file you would like to highlight the () text after a "while"
differently from the () text after a "for". In both of these there can be
nested () items, which should be highlighted in the same way. You must make
sure the () highlighting stops at the matching ). This is one way to do this:
:syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
\ contains=cCondNest
:syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/
\ contains=cCondNest
:syntax region cCondNest start=/(/ end=/)/ contained transparent
Partial matches in regex
If you really just meant highlighting submatches, have a look at the the
\zs start match
\ze end match
In short,
:match Error /foo\zsbar\zered/
would highlight only 'bar' in 'foobarred'

Context sensitive word wrap in vi/vim

How can I can specific word wrapping for specific tags. For example, in LaTex I want word wrapping for my paragraphs but not for my figure commands (they are always very long and run off the screen).
Or with Javascript, I want the right margin for code to be at, for example 50 columns, but for the comments to be at only 40 columns
This is not builtin
You could probably script something yourself using a devious combination of `formatexpr` and synID(). I suggest you look at the help of the latter first, because it contains inspirational samples:
for id in synstack(line("."), col("."))
echo synIDattr(id, "name")
endfor
taken from :he synstack
The formatexpr is usually set to something like
:set formatexpr=mylang#Format()
thus delegating to a filetype plugin. You could implement the function to use different margins for different syntax contexts.
Bear in mind
the default formatexpr (if absent, formatprg) are probably no good for a source file (in my experience it has the tendency to string together lines as if they were text paragraphs). But then again, you can implement it any which way you want
that syntax highlighting may become out of sync. I'm not sure what happens when the cursor is at, say, 70% of a large document and you issue ggVGgq. It might not update the syntax highlighting all the way (meaning that your formatexpr function would get the 'wrong' synID() values. You get around this by saying something like
:syntax sync fromstart
this again might impact the highlighting performance depending on the size/complexity of the source and highlighting scripts

Resources