I would like to have insert mode completion for sentences rather than lines in vim.

C-x C-l completes lines, but cannot complete sentences that do not start with a new line.

For example:

I have a dog. My dog's name is Spot.

I want to be able to type:

My dog

then hit a key to complete the sentence.

What is the best way to do this? A thread with the same title did not have a solution.


Try C-x C-u. You will need to define the completion as described in the complete-functions section of the vim help. Type:

:help complete-functions

To see this section. Similarly,

:help i_^X

(where ^X is the C-x character) will show you the section that describes this mode. Keep in mind that sentences typically end with the pattern /. /. Good luck.

  • Thanks. This is currently a bit beyond my level of knowledge, but I'll post if I find a specific solution. – stackoverflowed Jun 11 '12 at 2:06

I modified my thesaurus completer to do sentence completion. Some enhancements would be Complete using PWD/, Gutenberg/books/, Better regex using context around cursor.

" What: Complete sentence
" Usage: :so % <CR> Get sentence[press C-x C-o]
" How:  Get sentence completions using perl from current  file

set completeopt+=menuone

set omnifunc=MoshCompleteSentence
function!    MoshCompleteSentence(findstart, base)
    " == First call: find-sentence-backwards, see :help omnifunc
    if a:findstart
        let s:line = getline('.')
        let s:wordStart = col('.') - 1
        " Check backward for a sentence_prefix
        while s:wordStart > 0 && s:line[s:wordStart - 1] =~ '[A-Za-z0-9 _,]'
            let s:wordStart -= 1
        return s:wordStart

        " == Second call: grep for sentence_regex, output: list of sentences
        let a:sentence_regex = a:base
        " in regex trim spaces
        let a:sentence_regex = substitute( a:sentence_regex,'\v^\s*(.{-})\s*$','\1','')
        " in regex change punctuation as dot.
        let a:sentence_regex = substitute(a:sentence_regex,'\W','.','g')
        " grep using perl
        let s:cmd='perl -ne '' '
            \.'next if m/^[;#]/;'
            \.'if( /('.a:sentence_regex.'.{1,30})/io ){'
            \.'   print qq/$1;;/ '
            \. ' '' '
        " == To debug: redir echom to file
        " redir >> c:/tmp/vim.log
        echom s:cmd
        let   s:rawOutput = system(s:cmd)
        let   s:listing = split(s:rawOutput, ';;')
        " echom join(s:listing,',')
        " redir END
        return s:listing

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.