In Intellij Idea, there’s a feature. Let’s say I have used a variable myCamelCase somewhere in my code. Then if I type mCC and press Ctrl–Enter or some such key combination, it expands to myCamelCase. Is there something similar in Vim?
In Intellij Idea, there’s a feature. Let’s say I have used a variable myCamelCase
Share
Okay, forgive me for answering twice, but since my first attempt missed the point, I’ll have another go. This is more complicated than I thought, but possibly not as complicated as I have made it (!).
This is now modified to suggest all matching variable names.
First of all, here’s a function to generate the ‘mCC’ abbreviation from the ‘myCamelCase’ string:
Now, here’s a function that takes an abbreviation (‘mCC’) and scans the current buffer (backwards from the current line) for “words” that have this abbreviation. A list of all matches is returned:
Next, here’s a custom-completion function that reads the word under the cursor and suggests the matches returned by the above functions:
To make use of this, you must define the “completefunc”:
To use insert-mode completion, type CTRL-X CTRL-U, but I usually map this to CTRL-L:
With this code in your vimrc you should find that typing
mCCfollowed by CTRL-L will make the expected replacement. If no matching expansion is found, the abbreviation is unchanged.The code isn’t water-tight, but it works in all the simple cases I tested. Hope it helps. Let me know if anything needs elucidating.