> For the complete documentation index, see [llms.txt](https://fashaun.gitbook.io/dev-vim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://fashaun.gitbook.io/dev-vim/autocommands.md).

# Autocommands

Autocommands are a way to tell Vim to run certain commands whenever certain events happen.

examples

Let's change it so that Vim creates files as soon as you edit them. Run the following command:

:autocmd BufNewFile \* :write

Take a look piece by piece

:   : :autocmd BufNewFile \* :write ^ ^ ^ | | | | | The command to run. | | | A "pattern" to filter the event. (regular expression) | The "event" to watch for.

The event type of vim

```
Starting to edit a file that doesn't already exist.
Reading a file, whether it exists or not.
Switching a buffer's filetype setting.
Not pressing a key on your keyboard for a certain amount of time.
Entering insert mode.
Exiting insert mode.
BufWritePre: the event will be checked just before you write any file.
FileType: the event will check file type (like javascipt, python)
```

So try more specific

:autocmd BufNewFile \*.txt :write

:autocmd BufWritePre \*.html :normal gg=G #reindent html file before save

## Multiple Events

:autocmd BufWritePre,BufRead \*.html :normal gg=G #reindent html file before write and after read

## FileType Events

`:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>`

`:autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>`

Open a Javascript file (a file that ends in .js), pick a line and type c. This will comment out the line.

Now open a Python file (a file that ends in .py), pick a line and type c. This will comment out the line, but it will use Python's comment character!

## Exercises

Skim :help autocmd-events to see a list of all the events you can bind autocommands to. You don't need to memorize each one right now. Just try to get a feel for the kinds of things you can do.

Create a few FileType autocommands that use setlocal to set options for your favorite filetypes just the way you like them. Some options you might like to change on a per-filetype basis are wrap, list, spell, and number.

Create a few more "comment this line" autocommands for filetypes you work with often.

Add all of these autocommands to your \~/.vimrc file. Use your shortcut mappings for editing and sourcing it quickly, of course!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://fashaun.gitbook.io/dev-vim/autocommands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
