Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to create your own completion for vim

Posted on Jun 25 Have you ever thought about defining your own Completion for particular things like emails, contact names or for something complex like tailwind classes or github usernames? Well, my goal on this post is to show you how to do it in a simple and concise way!As you can see on the video above it's quite a simple mechanism to define yourown completion, first you create a function that return an array of strings andthen you assigned to completefunc option that accept a function reference andafter that you can trigger the completion by pressing Ctrl+x Ctrl+u when oninsert mode. Now let's understand the structure of this function and write amore exciting one!Here is the function used on the video above for example:The first argument findstart it's a numeric argument and vim will call it with value 1 on the first execution of completion to find the column position of the current word (to position the completion popup for example), then on a second moment vim call this function again with a value 0 where it expects an actual lists of items.That's why on the video I got an error saying E745: Using a List as a Number, because vim was expecting the column number at that point of the completion cycle.The second argument base is a string with all the previous completion matches grouped together, of course it's empty on the first execution and gradually will be incremented if the user keep pressing the completion keybind.Now that we understand the basic structure for a completion function let'simprove it a little bit with a filter! Basically we'll detect the substringthat the user typed and return the correct option first so it's easy to selectit.Here is an example:The matchfuzzy on this context is a default function of vimscript and help us get an fuzzy matching algorithm that give our simple completion function a lot of power, as shown below:If you mess with this function on your own setup you'll observe that exists a bug, to clarify and keep us on the same page i'll show the bug below:As you can see on the video we can only complete the first word and then everything stop working, that's because we didn't return a proper integer value for findstart! that way vim can't find the start of the current word and provide correct base value, this is quite simple to solve and we can increment it like this:On this version we're effective walking through the line and updating the startcol variable accordingly, that way vim will always find our current incompleted word and provide correct arguments.Well at this point I think you understand the structure and power of a custom completion function in vim, so let's spice things up and give a more complex example.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse PythonBasics - Jul 1 '21 Meet Rajesh Gor - Jun 29 '21 Steadylearner - Jun 29 '21 Siddharth - Jun 15 '21 Once suspended, cherryramatis will not be able to comment or publish posts until their suspension is removed. Once unsuspended, cherryramatis will be able to comment and publish posts again. Once unpublished, all posts by cherryramatis will become hidden and only accessible to themselves. If cherryramatis is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Cherry Ramatis. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag cherryramatis: cherryramatis consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging cherryramatis will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

How to create your own completion for vim

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×