Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 (!).</p> <p><strong>This is now modified to suggest all matching variable names.</strong></p> <p>First of all, here's a function to generate the 'mCC' abbreviation from the 'myCamelCase' string:</p> <pre><code>function! Camel_Initials(camel) let first_char = matchstr(a:camel,"^.") let other_char = substitute(a:camel,"\\U","","g") return first_char . other_char endfunction </code></pre> <p>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. <strong>A list of all matches is returned:</strong></p> <pre><code>function! Expand_Camel_Initials(abbrev) let winview=winsaveview() let candidate=a:abbrev let matches=[] try let resline = line(".") while resline &gt;= 1 let sstr = '\&lt;' . matchstr(a:abbrev,"^.") . '[a-zA-Z]*\&gt;' keepjumps let resline=search(sstr,"bW") let candidate=expand("&lt;cword&gt;") if candidate != a:abbrev &amp;&amp; Camel_Initials(candidate) == a:abbrev call add( matches, candidate ) endif endwhile finally call winrestview(winview) if len(matches) == 0 echo "No expansion found" endif return sort(candidate) endtry endfunction </code></pre> <p>Next, here's a custom-completion function that reads the word under the cursor and suggests the matches returned by the above functions:</p> <pre><code>function! Camel_Complete( findstart, base ) if a:findstart let line = getline('.') let start = col('.') - 1 while start &gt; 0 &amp;&amp; line[start - 1] =~ '[A-Za-z_]' let start -= 1 endwhile return start else return Expand_Camel_Initials( a:base ) endif endfunction </code></pre> <p><strong>To make use of this, you must define the "completefunc":</strong></p> <pre><code>setlocal completefunc=Camel_Complete </code></pre> <p>To use insert-mode completion, type <kbd>CTRL-X</kbd> <kbd>CTRL-U</kbd>, but I usually map this to <kbd>CTRL-L</kbd>:</p> <pre><code>inoremap &lt;c-l&gt; &lt;c-x&gt;&lt;c-u&gt; </code></pre> <p>With this code in your vimrc you should find that typing <code>mCC</code> followed by <kbd>CTRL-L</kbd> will make the expected replacement. If no matching expansion is found, the abbreviation is unchanged.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload