1

For example

import numpy as np
import matplotlib.pyplot as plt
x = np.array([-1, 0, 1])
y = np.abs(x)
plt.plot(x, y)
plt.show()

Is there a way to highlight all the occurrences of np and plt?

  • Not sure if it's doable automatically, but you could use interestingwords plugin to do it manually: github.com/lfv89/vim-interestingwords – grodzik Apr 5 '17 at 6:00
  • I can't answer definitively, but I suspect the answer might be that this is not possible using simple highlighting: Vim's syntax highlighting is based on regular expressions and does not actually parse the code, so it has no way of knowing that the plt in plt.show() was imported earlier on. You could write some VimScript that searched for imports and then set up highlighting with :match or matchadd(), but this isn't an elegant solution. – Rich Apr 5 '17 at 8:21
2

I wrote a plugin for this: impsort.vim

enter image description here

It originally was only for sorting imports. Since the imports were being parsed, I threw in highlighting as well.

Not only does it highlight imported modules, it highlights imported classes and functions (if it can figure out their types).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.