Is this the best reference to learn how to create a simple plugin?

I'm looking to create a really simple Vim plugin. Basically, for a given file extension, when you save the file in Vim it should run an external command and then refresh the file in Vim.

Digging around, there doesn't seem like a great reference for creating a Vim plugin. This one seems to be about it, but there's a good chance by googling skills are lacking this morning.

Any recommended tutorials or references I should look at?

Posted Apr 11, 2017, 1:37 PM UTC

1118
stewa02System Engineer•

Have you read :h write-plugin?

4
thomas_stringerOP•

Sorry, I should have been explicit, yes I looked at both :h write-plugin and :h write-filetype-plugin. Again, it's material but wouldn't mind something a little more robust.

Thanks for the pointer!

3
ghost-in-a-shellHave you read from the good book of ':help'?•

Is an autocmd using BufWrite/ BufWritePost not sufficient?

4
thomas_stringerOP•

I don't really know enough about what autocmd is capable of..?

Basically, when the user saves a *.tf file, I want to run terraform fmt. And then I immediately want Vim to run :edit to refresh the window. Is autocmd capable of doing that?

1
ghost-in-a-shellHave you read from the good book of ':help'?•

I don't really know enough about what autocmd is capable of..?

:h autocmd.

Basically, when the user saves a *.tf file, I want to run terraform fmt. And then I immediately want Vim to run :edit to refresh the window. Is autocmd capable of doing that?

Something like autocmd BufWrite *.tf !terraform fmt %? Although preferably wrapped in an augroup.

2

Augroups are important. I had a set of weird keybindings that I couldn't find my settings for. They had got saved in a session file before I knew about augroups :/

1
jocap•

How do augroups help against that? If you could explain :)

1
[deleted]•

[removed]

1
HaulethNeoVim•

Use gq instead.

1
pulbicvoidgetname•

Have you seen this already? https://github.com/hashivim/vim-terraform It has :TerraformFmt command.

1
bassforte95igemnace on #vim•

Learn Vimscript the Hard Way is good to start with, it's on the sidebar. Getting the hang of :help will also help a lot.

4
justrajdeep•
3
thomas_stringerOP•

Thanks for those links!! I'll check out the video, looks like it'll be a good one worth watching.

1
thalesmello•
2
tresfaim•

I do think an autocommand is what you're after. Script your function, then have an autocommand invoke it on the particular vim event you want to trigger it.

I made a little simple plugin recently to manage my vim sessions, https://github.com/blkwtkns/vim-sesh There's no fluff to it, so if you decide you want to go on and make a plugin it might serve as an easy template to pull ideas from.

1
stewa02System Engineer•

You probably took u/-romainl- 's vim-qf plugin as a template and didn't change your loaded variable properly.

2
tresfaim•

Thanks for catching that mistake. It strangely didn't affect the plugin usage for me... I used u/-romainl- and ack.vim's mainly, but I looked at a dozen. I'm not using autoloading because of the autocommands I threw in, but those should actually be optional.

1
stewa02System Engineer•

This is what :h write-plugin says about it:

NOT LOADING

It's possible that a user doesn't always want to load this plugin. Or the system administrator has dropped it in the system-wide plugin directory, but a user has his own plugin he wants to use. Then the user must have a chance to disable loading this specific plugin. This will make it possible:

   if exists("g:loaded_typecorr")
       finish
   endif
   let g:loaded_typecorr = 1

This also avoids that when the script is loaded twice it would cause error messages for redefining functions and cause trouble for autocommands that are added twice.

The name is recommended to start with "loaded_" and then the file name of the plugin, literally. The "g:" is prepended just to avoid mistakes when using the variable in a function (without "g:" it would be a variable local to the function).

Using "finish" stops Vim from reading the rest of the file, it's much quicker than using if-endif around the whole file.

It's a convention to be able to disable the plugin and prevent it loading multiple times.

2