Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, how to I set an autocommand to be run after a plugin has loaded?

One of the Vim Plugins I use has a bug, causing it to set :syntax spell notoplevel. The bug is easily mitigated if I run the command :syntax spell toplevel after opening a file. However, I'm lazy and I'd like to put the fix in my init.vim / .vimrc file, so that it's run automatically.

How can I ensure that my fix is executed after the buggy plugin code, so that my setting is not overridden by the plugin?

like image 862
andypea Avatar asked Jun 05 '19 05:06

andypea


People also ask

How do I use Vimrc plugins?

Now you can place Vim plugins in ~/. vim/pack/vendor/start, and they'll automatically load when you launch Vim. A file tree will open along the left side of your Vim window. Any plugins installed into opt are available to Vim, but they're not loaded into memory until you add them to a session with the packadd command.

What is Autocommand Neovim?

1. Introduction *autocmd-intro* You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. For example, you can create an autocommand to set the 'cindent' option for files matching *.c.

Where are NVIM plugins?

Nvim's data directory is located in ~/. local/share/nvim/ and contains swap for open files, the ShaDa (Shared Data) file, and the site directory for plugins. Starting from Nvim's version 0.5, it is possible to setup Nvim via Lua, by default ~/. config/nvim/init.

What is Vim Ftplugin?

A file type plugin (ftplugin) is a script that is run automatically when Vim detects the type of file when the file is created or opened. The type can be detected from the file name (for example, file sample. c has file type c ), or from the file contents.


2 Answers

Create a file in ~/.vim/after/plugin/ such as ~/.vim/after/plugin/fix-spell.vim containing the command you'd run without the colon:

syntax spell toplevel

The files in ~/.vim/after/plugin are sourced after your plugins have loaded, thus providing a convenient hook to change settings that might have been set by a plugin.

like image 177
fphilipe Avatar answered Sep 30 '22 03:09

fphilipe


Alternatively, you can set it as an autocommand. There are a whole slew of events you can tie autocommands to (:help events for all of them). VimEnter is an event that fires after plugins are loaded, so you could set your command to run then with a line like this right in your vimrc:

autocmd VimEnter * syntax spell toplevel

That's what I am using to apply a plugin theme that is not available until after plugins load.

like image 38
shucks Avatar answered Sep 30 '22 02:09

shucks