Vim Function to Generate a Comment in Go
OK, I’ll admit it. Sometimes I don’t add comments to describe my Go structs and functions. There, I said it. And after my linter properly chastises me, I dutifully work down the list of errors, adding the code comments… One at a time. I know… I know… I should just add the comments while I’m writing the code.
Well, today I found that task tedious. So I decided to write my very first Vim function. What does it do? When you put your cursor on a word in normal mode, then call the function, a comment block will be inserted above your method or struct that starts your new comment.
How did I do it? I’ll show you! Remember though, this is my first Vim function in Vimscript, and I’m pretty new to it. Be gentle.
Here’s a line by line breakdown:
- Just a comment describing our Vim function
- The function declaration. My function is name GenerateComment
- Get the current line number where the cursor is at. Assign that to lineNumber
- Get the word under the cursor. Assign that to…