11

I'd like to change my vim config file to allow highlighting of only my declared variables, not keywords. This article shows and explains what I mean: Alternate syntax highlighting

I'm a beginner to vim (I've never changed the default config file). Could anyone point me in the right direction?

CS Student
  • 1,613
  • 6
  • 24
  • 40
  • Have a look at this [wiki](http://vim.wikia.com/wiki/Vim_Tips_Wiki). – FlyingFoX Jan 27 '14 at 15:13
  • Probably not: https://groups.google.com/forum/#!topic/vim_dev/vULHSjFRmyc – mMontu Jan 27 '14 at 15:19
  • this type of highlightig is not available in vim...but might be a good idea for a plugin. Won't be very easy to implement though – mihai Jan 27 '14 at 15:29
  • You can temporarily color this up using for instance Mark--Karkat plugin or similar and this will be saved in view (this can be set to be automatic). The down side is that you have to do it at least once for each var, but on the other hand perhaps it could also get automated. The good side is that you can quickly turn this thing on/off. – majkinetor Jan 27 '14 at 15:38

4 Answers4

5

As proof of concept, I tried

let vars = ['init', 'editable', 'init_ui']
let colors = ['ff0000', '00ff00', '0000ff']
for var in vars
  execute 'syn keyword var_' . var var
  execute 'hi default var_' . var 'guifg=#' . remove(colors, 0)
endfor

and it worked as expected. This created syntax items for each variable in the list: var_init, var_editable, and var_init_ui. Then it assigns a highlight color to each syntax item.

In order to get beyond proof of concept, you have to get a list of variable names. You can do this by parsing a tag file (as produced by ctags, for example) or by writing a parser in vim (which would be very portable). You can sort the list and remove duplicates, but I think the use of :hi default will save you if you skip this step. Come up with a better way of generating colors than my example.

You can do all of that using an autocommand when a buffer is entered, or when the user explicitly calls a function. Then you can start thinking about automatic updating as new variables are defined.

benjifisher
  • 5,054
  • 16
  • 18
3

Benjifisher's answer outlines how such can be implemented, but that's still a major effort, and probably out of reach for a beginner. But, as majkinetor recommended in the comments, my Mark plugin would allow you to quickly set up different coloring for "interesting" variable names by manually (un-)marking them (the default mapping is <Leader>m, which usually translates to \ followed by M. I use this myself to understand complex parts of the code or when troubleshooting log files.

With the following command in your ~/.vimrc, you can have up to 77 different colors available:

let g:mwDefaultHighlightingPalette = 'maximum'
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

There is such a plugin: https://github.com/jaxbot/semantic-highlight.vim

Where every variable is a different color, an idea popularized by Evan Brooks' blog post.

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
  • It highlights keywords, but not variables if you have other syntax highlighting vimscript loaded. Tested in ES6 javascript. – Pencilcheck Aug 13 '17 at 19:26
0

I don't like the idea of turning text strings into commands when using a scripting language that ideally should be able to retain some semantics and structure for the kind of constructs, so I went about implementing a highlighting procedure able to use variables the following way instead:

function s:hl(group, attrs)
    let l:command = "highlight" . " " . a:group
    for name in keys(a:attrs)
        let l:command .= " " . name . "=" . a:attrs[name]
    endfor
    execute l:command
endfunction

With above, I can pass a dictionary of highlight attributes for a group to set, where values of the dictionary may obviously be variable references.

Then one can get Vim to effectively end up running the same highlight <group> <name>=<value> ... command, by calling the above function as follows, for example:

call s:hl("Keyword", { "guifg": "yellow" })

Since the function accepts a dictionary for its second parameter, other key-value pairs can be added accordingly, e.g. call s:hl("Normal", { "guifg": "white", "guibg": "black" }).

With variables defined it'd be more like:

let s:chefchaouen_blue = "#468fea"

call s:hl("Comment", { "guifg": s:chefchaouen_blue })

Anyway, such approach may look to be more verbose -- and in the end is still turned into a command string to be used with the execute command -- but I find Vim being able to highlight elements of its own script text -- e.g. the call s:hl(...) statements above in a color scheme file -- worth it. Also, more of it may be checked for syntax errors earlier (than if concatenated into a string), although the difference may be negligible for most.

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95