Learn to Code via Tutorials on Repl.it!

← Back to all posts
Vim autocmds: Inserting default code
h
CodeLongAndPros (1587)

How to setup mappings and default snippets for your source file

Today, I worked on setting up Vim for d programming (Whole other story),
when I got to my autocommands.

For those of you that don't know what an autocommand is, it is a Vim command that is ran on the target.

A autocommand has this form:

autocmd target filename_regex command

While writing this, I made this one:

autocmd BufWrite \*.md !grip % -b

You can make a groups of autocommands.

The syntax is:

augroup name
  autocmd! " Clear all command
  autocmd * * *
augroup END

To make Vim insert a template text for a file

Make a dir, ~/.vim/defaults

For the language you want, (I'll use C), make a file named ~/.vim/defaults/c

In your Vimrc:

augroup c
  autocmd!
  autocmd BufNewFile *.c r~/.vim/defaults
augroup END
Comments
hotnewtop
Highwayman (1443)

Hum, vim is cool, but I don’t know how to use it. Do you have other vim tutorials too?