Questions tagged [autocmd]

autocmds are commands executed automatically on certain events. Questions on creating or modifying autocmd definitions are appropriate for this tag.

One of Vim's most powerful features is its ability to run arbitrary commands on certain events for matching files.

Key Terms:

  • event: The set of events on which vim triggers autocmd execution. These include staring to writ a file (BufWrite), before exiting vim (VimLeavePre), etc.
  • pattern: A pattern to match file names for which an autocmd will be executed. Patterns can include wildcards, environment variables and special terms for buffer-local action.
  • command: The command to be executed.

Standard information:

443 questions
76
votes
3 answers

Why should I use augroup?

I understand how auto commands work, and how to use them, but I'm somewhat unsure of what augroup is for. I read in :help augroup *:aug* *:augroup* :aug[roup] {name} Define the…
DJMcMayhem
  • 17,581
  • 5
  • 53
  • 85
27
votes
2 answers

Is there a way to AND events in the autocmd?

I'd like to trigger an autocmd on two events but not in a way it is usually done, i.e. if either of the events happened then trigger an autocmd. I want to trigger it if both events happened. For example: The usual way to do it autocmd…
flashburn
  • 689
  • 6
  • 17
18
votes
2 answers

Suppress output from a Vim autocomand

I have an autocommand defined in my vimrc: au BufWritePost * !./make.sh The script make.sh compiles a .tex file 3 times and I don't want the output to be displayed. Is there a way to suppress the output? I tried to add silent one line before the…
10
votes
1 answer

What's the difference between autocmd {cmd} and autocmd! {cmd}

For example, what's the difference between calling this command this way: autocmd BufWritePre * call StripTrailingWhitespace() vs. this way (with the bang after autocmd): autocmd! BufWritePre * call StripTrailingWhitespace()
aonemd
  • 203
  • 2
  • 6
7
votes
1 answer

When exactly does differ from ?

The documentation says: When executing autocommands, is replaced with the file name for a file read or write. When executing autocommands, is replaced with the currently effective buffer number (for ":r…
muru
  • 24,838
  • 8
  • 82
  • 143
7
votes
3 answers

How do I trim whitespace automatically?

I've been doing this: au BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif The problem is that it moves the cursor sometimes when I save the file.
Neil G
  • 193
  • 8
6
votes
1 answer

Strange behaviour of autocmd

After starting neovim with nvim -u NONE, why do the following commands :term :au BufWinEnter,WinEnter startinsert :tabnew leave me in insert mode in the new tab, and how can I avoid that? The same behaviour happens when replacing
Rastapopoulos
  • 533
  • 3
  • 14
6
votes
2 answers

Error detected while processing BufReadPost Auto commands for "*.html": E20: Mark not set

I have what I thought was a pretty simple setup in my neovim init.vim file. Basically all I wanted it to do was configure the tab settings when opening an HTML file. My configuration is: au BufNewFile,BufRead *.html, *.css \ set tabstop=2 …
Ron Obvious
  • 63
  • 1
  • 3
5
votes
2 answers

How can I cancel reading a file into a buffer on BufReadPre?

In order to open PDFs using an external application, one can use the following script: augroup nonvim au! au BufRead *.pdf sil exe "!open " . shellescape(expand("")) | bd augroup end However, this is not optimal, because the pdf…
Bart Louwers
  • 226
  • 1
  • 12
4
votes
0 answers

autocmd VimLeave event not firing

I had problem with YCMD server not shutting down properly, so I tried to kill it on VimLeave. After a couple tries I detected that VimLeave/VimLeavePre event not firing on vim exit. VimEnter is working though. What could be the cause of this?
fourslashw
  • 141
  • 2
4
votes
1 answer

How to test filetype in an autocommand without using the FileType event?

I want all trailing whitespace in the buffers I'm editing to be highlighted in red. Here is the autocommand I'm using: autocmd BufEnter,WinEnter * call matchadd('Error', '\v\s+$', -1) The matchadd() function looks for the pattern '\v\s+$' which…
saginaw
  • 6,756
  • 2
  • 26
  • 47
4
votes
2 answers

Why doesn't Vim work with lowercase event names?

From the Vim event documentation: Vim recognizes the following events. Vim ignores the case of event names (e.g., you can use "BUFread" or "bufread" instead of "BufRead"). So in my custom_filetype.vim, I use au filetype to run a command when…
cuonglm
  • 176
  • 8
4
votes
1 answer

Autocmd treat user defined command differently?

This works fine: augroup au_test | au! autocmd BufNew * if 1 | echom 123 | endif augroup end But this doesn't work: com Test echom 123 augroup au_test | au! autocmd BufNew * if 1 | Test | endif augroup end It results in following error when i…
dedowsdi
  • 6,248
  • 1
  • 18
  • 31
4
votes
2 answers

mutually exclusive Autocommands for similar file extensions

I have a couple of autocmds in my vim config along the lines of this (the actual commands are quite long): au BufWritePost *.latex.md silent! ... au BufWritePost *.md silent! ... where writing files with .md or .latex.md extensions runs…
myc3lium
  • 95
  • 3
4
votes
1 answer

Is there a way to tell what autocmds have run?

If the autocmd has an obvious side effect, this is not too hard to tell; but sometimes the result is more subtle and it would be nice to just see "Hey, here's all the autocmds that ran on startup." I checked :history but autocmds don't seem to show…
1
2 3 4 5