VIM syntax coloring lua function name? - vim

I'm using VIM via terminal (SSH) and want to modify my syntax coloring settings in VIM to make the function_name Yellow for Lua programming. Unfortunately, I can't figure out how to do that.
For example, in the code below - I want my VIM to syntax color myFunc yellow
local function myFunc(arg1)
...
end
I've been able to figure out how to make function yellow, by using this code below:
hi luaFunction ctermfg=Yellow
But this code does not color the word myFunc yellow (and frankly, I'd rather not syntax color function at all)
Question: Any ideas how I can syntax color the function_name in Lua?

Another (more complex) regex to match the function name.
:hi luaCustomFunction ctermfg=yellow
:syn match luaCustomFunction "\(\<function\>\)\#<=\s\+\S\+\s*(\#="
This uses a look-behind to match the word function. Then after 1 or more spaces it will highlight any word that has a parenthesis as the first non whitespace character after it.
This only highlights the function name. It does not highlight the parenthesis.
I think if you put these commands in .vim/after/syntax/lua.vim it should work.
The problem with putting them in your vimrc is that sometime after the vimrc is sourced the syntax highlighting file is sourced and generally the first line in there is syn clear. (Which wipes the custom syntax highlighting you just set)

This very naive implementation works with your sample:
:hi luaCustomFunction ctermfg=yellow
:syn match luaCustomFunction "\s\+\zs\S\+\ze("
It is obviously very limited but at least you get a starting point. Read :h syntax for further help.

How about:
hi def link luaFunction Function
hi Function ctermfg=Yellow

Related

Highlighting set of specific keywords in gvim

I have tried highlighting specific words by adding them in .vimrc, colorscheme file and also in syntax.vim(I changed one at a time, not altogether).
syn match mygroupwords '\c\<\(-word1\|-word2\)'
hi def link mygroupwords colo_words12
hi colo_words12 guifg=red1 gui=bold guibg=white
But somehow it seems to be getting overwritten by default syntax highlighting
i need to highlight keywords irrespective of color-scheme or file-type which have following words-
Ex; -word1 , -word2
Any suggestions?
explanation of your failed attempts
A colorscheme just provides color definitions for predefined highlight groups; that's the wrong place for actual syntax matches! ~/.vimrc is the first configuration that is read; if a filetype is detected and a corresponding syntax script is loaded, that will override your syntax definition.
syntax extensions
If your desired highlightings are extensions to an existing syntax, you can place :syntax match commands in a syntax script in the after directory. For example, to extend Python syntax, put it in ~/.vim/after/syntax/python.vim. That may still fail if the original syntax obscures the match; sometimes, this can be solved via containedin=...
independent marks
If your highlightings are independent of syntax and filetype, there's a different built-in mechanism: :match (and :2match, and additional variants via :call matchadd(...)):
:match mygroupwords /\c\<\(-word1\|-word2\)/
This goes on top of (and is independent of) syntax highlighting. However, it is local to the current window. So, if you put this into your .vimrc, it will only affect the first window (but any files viewed in there). To apply this globally to window splits and tab pages, you have to use :autocmds. It's not trivial to get this totally right. If you need such a full solution, have a look at my Mark plugin; this supports multiple colors, allows presetting with :[N]Mark /{pattern}/ (similar to :match), and highlights in all windows.

gvim: Colour lines differently containing TODO/HACK/etc?

Often I'll put comments in code like TODO/HACK/BUG: I'd love if these showed up glaringly when I did a diff of the code in vim.
Is there an easy way to set this up?
You can define a syntax group like this:
:syntax match TODOs ".*TODO.*\|.*BUG.*\|.*HACK.*"
This will match entire lines which contain either TODO, BUG or HACK in them.
Then you can use the highlight command to highlight it.
:highlight TODOs ctermbg=red ctermfg=yellow term=bold,italic
You can add both the lines to your vimrc or the syntax file for appropriate file types.

Is it possible to give a user defined color to a 'specific symbol' in vim?

I am working on a Grammar.txt file in vim editor.Is there a way I could give a different color to the '/', so that it is visually more appealing to my eyes.
A lot of my work requires working on paper, and differentiating keywords on screen this way would be helpful.
Here is a small snippet from my file
<Prog> ------ <functions><building>
<functions> ------ <function><functions>/ #
<function> ------ <funsig> <funcbody>
<funsig>------ <type> id (<params>)
<type> ------ int/float/distance/<floortype>/point/wall/doors/window/ratio
<params>------ <type>id<LF1>/#
<LF1>---------comma<params>/#
<funcbody>----- {<stats>}
<stats>----- <stat> <stats>/ #
<stat>----- <assignmentstats>/<returnstats>/<declarativestats>
You could use the :match command:
:match {color-group} /\//
You can get a list of color groups by using the :highlight command without any arguments. Thus, something like this:
:match Function /\//
You can also clear any user-made matches by calling the clearmatches function.
:call clearmatches()
More help on these commands:
:help :match
:help :highlight
Assumed you use search highlighting (:set hlsearch), just search for /:
/\/
Example:
I'd like to complement #EvergreenTree's answer a bit.
Vim has this highlighting capability called matching, which indeed works like this:
:match Error /pat/
The first argument marks a highlight group, a list of which you can check out with :hi. The second argument is a regexp-powered search pattern that needs to be wrapped in / separators. In case of unclear patterns you usually test out the pattern by issuing traditional search. When the pattern looks fine, you can insert the last searched pattern in the :match line by typing <C-r> /. You can clear the highlights by calling :match alone, without arguments.
There are also commands :2match and :3match to highlight more different patterns simultaneously. These work similarly to :match.
If you need even more matches (happens easily with log file analysis), there's a set of VimL functions:
:call matchadd('Function', 'patt')
:call clearmatches()
This time the pattern doesn't need those / separators around it but otherwise the regexp syntax remains the same.

Vim syntax highlighting hide characters

I'd like to implement a syntax file for vim that hides certain characters in the file. Specifically, I want to write an improved highlighter for reading Markdown files that doesn't display some of the formatting characters, preferring instead to indicate them implicitly. For example, I'd like to have things like *bold* render as simply bold with bold text, or to have headings like
My Header
=========
not show their underline, but just appear a different color. I haven't managed to find any examples so far of vim syntax files that hide specific characters from display. Is this something that is possible in vim? If so, how?
To hide syntax items - or just certain characters - the conceal or Ignore arguments can be used. See
:help hl-Ignore
:help syn-conceal
For an example see the syntax file "help.vim" which is part of crefvim. CRefVim is a C-reference manual which is embedded in the Vim help system. The "help.vim" syntax file extends the standard syntax highlighting for help files.
An example. The '$' character is used here to show text in italic:
Maybe this example is a good starting point for you to dig further...
Habi
You could make your own syntaxfile with an according colortheme, using "bold", "italics" and the like. It wouldn't hide anything, so that your syntax must work with the original text.
For example this could be your syntax for headers
In your syntax you would need:
syn match Header '^\s*\u*\.\s.*$' contains=ALL
hi link Header ModeMsg
and in the colortheme
hi ModeMsg gui=bold guifg=NONE guibg=NONE cterm=bold ctermfg=NONE ctermbg=NONE term=bold
then a header like this
1. This is my new header, being bold
would be shown bold, without any markup at all. By the way, you can export it with the TOhtml feature while keeping the highlighting.

How do I make vim syntax highlight a whole line?

I'd like to have vim highlight entire lines that match certain patterns. I can get all the text in a line to highlight (by doing syn match MyMatch "^.*text-to-match.*$"), but it always stops at the end of the text. I'd like to to continue to the end of the term, like highlighting CursorLine.
I've tried replacing $ with a \n^, hoping that would wrap it around. No change. (I didn't actually expect this would work, but there's no harm in trying.) I also tried adjusting the syn-pattern-offset (which I read about here: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-pattern). Long story short, adding he=he-5 will highlight 5 fewer characters, but he=he+5 doesn't show any extra characters because there aren't characters to highlight.
This is my first attempt at making a vim syntax and I'm relatively new to vim. Please be gentle and include explanations.
Thanks!
(edit: Forgot to include, this is a multiline highlight. That probably increases the complexity a bit.)
It's not very adaptive as the filename (buffer) and line to full row highlight needs to be explicitly identified, but apparently the sign command can be used:
It is possible to highlight an entire
line using the :sign mechanism.
An example can be found at :help
sign-commands
In a nutshell:
:sign define wholeline linehl=ErrorMsg
:sign place 1 name=wholeline line=123 file=thisfile.txt
Obviously, you should pick a higlight
group that changes the color of the
background for the linehl argument.
source: Erik Falor, vim mailing list
From the documentation on syn-pattern:
The highlighted area will never be
outside of the matched text.
I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.
could also try
:set cursorline
:set cursorcolumn
change colors like this:
:hi cursorline
:hi cursorcolumn
using the usual term=, ctermfg=, ctermbg= etc
see this answer
VIM Highlight the whole current line

Resources