5

There's a completion type that isn't listed in the vim help files (notably: insert.txt), but which I instinctively feel the need for rather often. Let's say I have the words "Awesome" and "SuperCrazyAwesome" in my file. I find an instance of Awesome that should really be SuperCrazyAwesome, so I hop to the beginning of the word, enter insert mode, and then must type "SuperCrazy".

I feel I should be able to type "S", creating "SCrazy", and then simply hit a completion hotkey or two to have it find what's to the left of the cursor ("S"), what's to the right ("Crazy"), regex this against all words in the file ("/S\w*Crazy/"), and provide me with a completion popup menu of choices, or just do the replace if there's only one match.

I'd like to use the actual completion system for this. There exists a "user defined" completion which uses a function, and has a good example in the helps for replacing from a given list. However, I can't seem to track down many particulars that I'd need to make this happen, including:

  1. How do I get a list of all words in the file from a vim function?
  2. Can I list words from all buffers (with filenames), as vim's complete does?
  3. How do I, in insert mode, get the text in the word before/after the cursor?
  4. Can completion replace the entire word, and not just up to the cursor?

I've been at this for a couple of hours now. I keep hitting dead ends, like this one, which introduced me to \%# for matching with the cursor position, which doesn't seem to work for me. For instance, a search for \w*\%# returns only the first character of the word I'm on, regardless of where I'm in it. The \%# doesn't seem to anchor.

Community
  • 1
  • 1
Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
  • Not strictly relevant, but you might get some hints from [this](http://stackoverflow.com/questions/6548541/camelcase-expansion-in-vim-like-intellij-idea/6550628#6550628) answer. There are example functions to generate a list of words from the current file, and a custom completion function. I will try and post an answer later on today. – Prince Goulash Oct 17 '11 at 08:32
  • Your link looks interesting and data-rich. I'll dig through it. Thanks for your help. – Gary Fixler Oct 17 '11 at 20:37

2 Answers2

0

Although its not exactly following your desired method in the past I've written https://github.com/mjbrownie/swapit which might perform your task if you are looking for related keywords. It would fall down in this scenario if you have hundreds of matches.

It's mainly useful for 2-10 possible sequenced matches.

You would define a list

:SwapList awesomes Awesome MoreAwesome SuperCrazyAwesome FullyCompletelyAwesome UnbelievablyAwesome

and move through the matches with the incrementor decrementor keys (c+a) (c+x)

There are also a few other cycling type plugins like swap words that I know of on vim.org and github.

The advantage here is you don't have to group words together with regex.

michael
  • 11,667
  • 2
  • 26
  • 25
  • Thanks for the link, Michael. I'll have to take a look through it. Have you seen the thesaurus completion stuff that comes with vim? I ran into it today in my search to solve the issues I listed. It lets you define related words, I think in a thesaurus file of sorts, and then match through them with the usual / hotkeys. – Gary Fixler Oct 17 '11 at 02:47
  • I had but didn't really think of using it for code libraries. Good idea. You might still find swapit useful for ad hoc refactoring where say you are going through a settings file and swapping SomeKeyword for AnotherKeyword for saw a some but not all keywords. You just set up the list go /Keyword and go (c+a)...n...n.n..n.(c+a) when it's needed without needing to add the list to file. – michael Oct 17 '11 at 03:21
  • Sounds like it could definitely help. Thanks! – Gary Fixler Oct 17 '11 at 17:11
0

I wrote something like that years ago when working with 3rd party libraries with rather long CamelCasePrefixes in every function different for each component. But it was in Before Git Hub era and I considered it a lost jewel, but search engine says I am not a complete ass and posted it to Vim wiki.

Here it is: http://vim.wikia.com/wiki/Custom_keyword_completion

Just do not ask me what 'MKw' means. No idea.

This will need some adaptation to your needs, as it is looking up only the word up to the cursor, but the idea is there. It works for current buffer only. Iterating through all buffers would be sluggish as it is not creating any index. For those purposes I would go with external grep.

Martian
  • 282
  • 1
  • 4