prevent vi from partially highlighting variable names that contain python keywords - vim

I'm having trouble with vi's lexical highlighting on variables that contain builtins or keywords. For instance, a python variable like missions_in_window will have partial highlighting on the word "in" while the rest of the variable stays a default color. See the example image below.
Is it possible to prevent this highlighting? This is just a pet peeve, but it's distracting to the point that I'll change a variable's name to avoid the issue. My .vimrc is here if that is helpful.

When you've :set iskeyword-=_, Vim will treat missions_in_window as three separate keywords: missions, in, window, and the in is highlighted by the Python syntax script.
The solution is simple: Don't do that. To navigate through and edit fragments of those variables, you can use my camelcasemotion plugin.

Related

How to autocomplete in vim based off partial matching via ctags

Example:
In a file in another directory I have a function defined by the following:
def _generator_function_1(self):
passs
In the file of my current directory, I have typed the following:
def test_generI
where I denotes my cursor position.
I would like to use vim's autocompletion functionality (i.e. via ^n or ^p) to autocomplete the function definition to test_generator_function_1. Is there a way of configuring vim autocompletion to match not based off full-prefixes? Or, is there a way in ctags to generate tags based off keywords instead of full function definitions?
EDIT:
To clarify, I am specifically wondering if keyword-based autocompletion exists. I have autocompletion by tags setting up, so if I typed "_gen", then ^n would complete to give me "_generator_function_1". In my example, however, it is because the string is prefixed by "test" that "test_gener" as the starting typed word does not lead to any autocomplete suggestions. So I am wondering if this can somehow be made possible.
Vim doesn't have "autocompletion functionality". It only has "completion", not "autocompletion". You need a plugin for "autocompletion".
No, there's no way to obtain your desired behavior without some serious vimscripting. See :help complete-functions.

Vim recipe for a minor syntax highlighting extension

I want to leave my system's Python syntax highlighting mostly intact, but I have a specific pattern I'd like to highlight for an idiom I use a lot. How can I add additional highlighting instructions on top of the existing highlighting done by vim?
(Apologies if this has already been asked. All the vim syntax highlighting questions I found seemed to involve writing a new syntax highlighting from scratch.)
Put your additional :syntax commands into ~/.vim/after/syntax/python.vim, and they will be automatically executed after the original syntax script.
It's easy to highlight stuff that so far isn't parsed at all.
For elements already parsed / hightlighted, you need to find out by which syntax group (e.g. pythonFunction), and add a containedin=pythonFunction clause to your :syntax commands. Without that, the original matching will obscure yours. To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
Introducing highlighting across (larger) elements that have multiple existing syntax groups is difficult, as your match will obscure the original ones, and that may break the entire parsing. You need to carefully examine the existing nested element structure, and try to fit in yours, again via contains= and containedin= clauses. Depending on the actual situation, that can be difficult.
For the actual syntax definitions, see the help starting at :h :syn-keyword. Basically, there are simple keyword definitions, regular expression matches, and regions defined by start and end patterns.

MacVim - edit color theme for javascript

I am using MacVim with the Cobalt theme. I found it very nice, however, coming from Sublime Text, I feel there aren't enough different colours which makes my javscript code hard to read.
For example, I'd like the function name to be coloured to make them stand out a bit more:
myClass.prototype.myFunction = function myFunction() {
// here, I'd like "myClass" to have a different color from the text
// same for "prototype" and "myFunction"
}
Another example is the use of methods:
myArray.pop();
// I'd like to change the color of ".pop()" for more visibility
How can I add these types of patterns?
A syntax script parses the programming language into different groups (which can be listed via :syntax list). A colorscheme then prescribes how to color and format each individual group.
So, if there are distinct groups, but your colorscheme just assigns the same color to it, that can be easily changed by putting
:hi link <syntaxGroup> <highlightGroup>
commands into your ~/.vimrc.
The detail of parsing depends on the language and syntax script. Extending an existing syntax (to parse out more details) is possible, but complex. For JavaScript, there exist some alternatives (like this) to the built-in syntax script; you might want to give those a try.
PS: :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
I use the plugin vim-javascript-syntax.

Make vim highlight python builtins when they are not followed by a dot

Is there a way to highlight built in Python functions in vim only when they are preceded by 1 more whitespaces? Furthermore, is there a modular way to do this? That is, I don't want to edit every single syn keyword pythonBuiltinFunc abs chr ... line, I just want to be able to say something like syn keyword pythonBuiltinFunc onlymatchafter="\s+"?
EDIT:
Here's an example, since the two people who answered my question didn't seem to understand what I was asking which is my fault for not being more clear.
When I write the following Python code
import numpy as np
x = np.abs(np.random.randn(10, 10))
The word abs gets highlighted simply because vim is essentially just matching anything that has the word abs in it that is not inside of a string. How can I get vim to highlight the Python builtins WITHOUT highlighting them when they are preceded by a dot?
The matched text of :syn keyword can only be comprised of keyword characters; though that set can be configured (:setlocal iskeyword=...), it would be foolish to include whitespace in there.
You have two options: Either re-write all keywords with :syn match (which can include whitespace), or make all keywords contained and define a :syn region that only starts after whitespace.
Both are rather huge interventions that basically mean you're (re-)writing your own Python syntax. You haven't told us why you'd want that... I'd say it's a bad idea.
Is the reason you want to do this because you want to remind yourself of using a consistent style in Python?
If so, I would like to recommend you adding the syntastic plugin. In combination with a tool such as flake8 it automatically detects and highlights any style errors you wish (by default PEP8 style violations), together with general syntax errors. I use this plugin a lot and highly recommend it.

visualize dependence on global variables using vim, ctags potentially

I'd like to highlight variables in my (Maple-code, but doesn't matter much) code which are global for routines.
e.g. I have
global_var1:=1;
global_var2:=2;
...
some_proc:=proc()
local local_var1, global_var2;
local_var1:=1;
local_var2:=local_var1*global_var1+global_var2;
end proc;
I want to highlight global_var1 inside of some_proc() in this example. Obviously the naming is not so trivial in general as in the example.
Can I use ctags to do this?
It depends on ctags. With some languages it is unable to extract local variables (viml), with other languages, it doesn't detect all local variables (C++). Hence, the first thing you'll have to do is to see what ctags can do for your language (Maple).
The other difficulty is to restrict the highlighting to one specific function, and to stay synchronized every time newlines are inserted to the edited file. I know no easy way to do this -- may be with a vim syntax region that starts at local.*{global-name} and ends at end proc to neutralize the highlighting of all global variables?
One task that'll be much more easier would be to highlight variable masking, i.e. highlight global_var2 at the point in the function where it is declared local. Alas, it's not what you're looking for.

Resources