13

<C-p> and <C-n> allow the completion of words found within the current document.

I am taking some notes and writing say {0,1}^n and (E,D) many times. These are WORDs constituted by several words.

Is there a way to complete WORDs with <C-p> and <C-n> (or other related ins-completion command)?

2
  • 1
    You may be better off using :abbreviate instead. For example, issue the :ab 01n {0,1}^n command. Then, every time you type 01n as an independent word in Insert mode, it will expand to {0,1}^n. Feb 20 '15 at 4:58
  • @200_success, thank you. I had thought about abbreviations but still I wanted to know if it was possible with ins-completion. Feb 20 '15 at 9:37
5

Insert mode completion with Ctrl-n

I don't know if you could do WORD completion outside of writing your own function (like omnifunc). However, if you don't mind expanding what a word is to match your characters, you can add those characters to 'iskeyword'

For example, my 'iskeyword' defaults to

iskeyword=@,48-57,_,192-255

I then set it to that plus the additional characters above

:set iskeyword=@,48-57,_,192-255,{,},,,(,),^

This adds {, }, ,, (, ), and ^ to the characters that make up a word (in addition to what already did)

After that, typing {Ctrl-n completes to {0,1}^n (in a file containing only those two examples you gave)

Note: It would be better to do a :set iskeyword+= with just the additional stuff, as follows:

:set iskeyword+={,},,,(,),^

This way, you leave whatever other things make up a keyword already by appending the new characters to the existing setting.

Expansion with abbreviations

Although not part of the original question, another similar(ish) functionality is to use nvi or Vim's abbreviation functionality. Unlike keyword completion, abbreviations don't have to look like the final text. Also unlike keyword completion, abbreviations expand automatically, instead of only when you ask for them. Depending on your goal and usage, this can save a lot of typing, and let you type lots of text without the interruption of asking for a completion.

Abbreviations are good for a small set of keywords and when you can define them ahead of time (they aren't discovered from your text like word completion is).

The basic idea behind abbreviations is that you associate a WORD with some other text, and any time the editor detects that you typed that WORD (that is, the text you typed is surrounded by white space, where the completion action is adding the whitespace after the keyword or leaving insert mode), it is replaced with that other text.

Let's say your file contains {0,1}^n a lot, and there aren't other variations of it. Typing that is a bit of a finger stretch, so maybe you want to just type 01n or myvalue and have it expand to {0,1}^n, but only when 01n or myvalue is a word on its own (that is, if it happens to appear within another word, don't change it).

In nvi, you can do :abbrev 01n {0,1}^n. You can do the same in Vim, but I recommend using :iabbrev 01n {0,1}^n to restrict the expansion to insert mode only.

Then, if you have some text (where | represents the cursor)

We find that the value 01n|

And then you hit Esc or Space or Tab or otherwise move the cursors, the text gets replaced, and you have (example for using space)

We find that the value {0,1}^n |

and you can continue typing without interruption.

This is usually used to auto-correct common typos (e.g. changing 'teh' into 'the') but it can be equally useful for your use case, again assuming that you don't mind setting up the mappings ahead of time.

4
  • To me it seems that :set iskeyword+={,},,,(,),^ works as expected. Feb 18 '15 at 8:54
  • 1
    Indeed it does. When I tried it last time, Vim kept telling me it was a syntax error. It must have been somewhere else. Updating my answer.
    – John O'M.
    Feb 20 '15 at 1:24
  • @JohnO'M. could you add a reference on how abbreviate might be better suited for this job? Feb 20 '15 at 22:34
  • @GonçaloRibeiro done. I hope it helps.
    – John O'M.
    Feb 22 '15 at 4:41

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.