Skip to content

Instantly share code, notes, and snippets.

@tjdevries
Created June 30, 2023 16:21
Show Gist options
  • Save tjdevries/e7ff5a7ca2e87a9628cdb00810e1ee74 to your computer and use it in GitHub Desktop.
Save tjdevries/e7ff5a7ca2e87a9628cdb00810e1ee74 to your computer and use it in GitHub Desktop.
tell me about autocommands
Read 11 files
Searched entire codebase for relevant files
test/functional/autocmd/cmdline_spec.lua
test/functional/autocmd/show_spec.lua
test/old/testdir/test_cmdline.vim
test/functional/autocmd/autocmd_spec.lua
test/functional/autocmd/textyankpost_spec.lua
test/functional/legacy/autocmd_option_spec.lua
test/functional/autocmd/filetype_spec.lua
test/old/testdir/test_quickfix.vim
runtime/doc/autocmd.txt
runtime/doc/usr_40.txt
README.md
Autocommands are commands that are automatically executed when some event occurs.Some examples of events are:
BufReadPost: After reading a buffer (opening a file)
BufWritePost: After writing a buffer (saving a file)
BufEnter: When entering a buffer
BufLeave: When leaving a buffer
BufNewFile: When creating a new file
Autocommands allow you to automatically execute commands when these events occur. For example:
Set options for certain file types:
autocmd FileType c set shiftwidth=4 softtabstop=4
Automatically source a file when entering a buffer:
autocmd BufEnter *.{py,c,cpp} source ~/.config/nvim/abbrevs.vim
Automatically save on focus lost:
autocmd FocusLost * :wa
Some other uses of autocommands are:
Syntax highlighting
Indentation
Filetype detection
Autocommands are defined using the :autocmd command. The basic syntax is:
:autocmd {event} {pattern} {command}
{event} is the event that triggers the autocommand (like BufReadPost)
{pattern} is a file pattern that filters which buffers the autocommand applies to
{command} is the command to execute when the event occurs
Autocommands allow you to customize and automate a lot of Vim's behavior. They are a very powerful feature of Vim.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment