0

Suppose we have an xml file like this:

<tag1>
somthing here
</tag1>
<tag2>
somthing here
</tag2>
<region>
autocomplete here
</region>

Whenever the cursor in within the region tag, I want the autocompletion c-n and c-x c-l to be restricted to a some custome options, for example:

with c-n:

myword1, myword2, myword3, myword4

with c-x c-l:

myline1
myline2
myline3
myline4

Is it possible?

Solutions in python would be preferred.

qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

1

Yes, a custom completion function can do that.

Fixed values depending on location

Vim has dictionary completion for a fixed set of words, cp. :help i_CTRL-X_CTRL-K. To configure different sets based on the location, an :autocmd CursorMoved,CursorMovedI could change the underlying 'dictionary' option.

Alternatively, you can write a custom completion function, as the CompleteMonths() example found under :help complete-functions.

Dynamic values depending on Vim range

My CompleteHelper plugin makes it very easy to write custom completions. With regular expression atoms like \%l, you can restrict the range where matches are found. The plugin page has links to several custom completions written with it.

Dynamic values depending on XML structure

To only get matches from certain XML tags, we're moving from general text editing to IDE-like features. You'd probably need an external XML parser (as parsing XML via regular expression is not possible), but can then follow the recipe for custom completion functions given above.

Community
  • 1
  • 1
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324