Skip to main content
Share Your Experience: Take the 2024 Developer Survey
74 votes
Accepted

Why should I use augroup?

The main reason to use augroups is to allow greater control over ranges of autocommands, e.g. to delete a bunch of autocommands in one go. A common pattern is to use the au! as the first part of a ...
Karl Yngve Lervåg's user avatar
39 votes

Why should I use augroup?

Autocommand groups give you the opportunity to organize your autocommands, which is a quite useful feature in and of itself. Say you are writing a plugin. Grouping your plugin's autocommands under ...
romainl's user avatar
  • 41.1k
30 votes

For an autocmd in a ftplugin, should I use pattern matching or <buffer>?

Is this usage of <buffer> correct? I think it is correct, but you just need to wrap it inside an augroup, and clear the latter, to make sure that the autocmd won't be duplicated every time you ...
user852573's user avatar
  • 1,023
27 votes

Why should I use augroup?

Aside from the other answers, I find that the most immediate practical benefit is that it makes it easy to reload vimscript files. For example stick this in your vimrc: autocmd BufCreate * echom "...
Martin Tournoij's user avatar
25 votes
Accepted

BufWinLeave * mkview with unnamed file: Error 32

Switching to ?* as the regex will no longer match empty file names. I actually recommend you switch to the following, which is a corresponding block of .vimrc that has been tweaked to suppress many of ...
kevinlawler's user avatar
24 votes
Accepted

How to enable spell check for certain file types?

The best way to set an option for a particular filetype is to use autocommands. Here you could add something like that in your .vimrc: autocmd FileType markdown setlocal spell This line will ...
statox's user avatar
  • 50.1k
20 votes
Accepted

How to remove Neovim trailing white space?

This should do the trick: vim.api.nvim_create_autocmd({ "BufWritePre" }, { pattern = { "*" }, command = [[%s/\s\+$//e]], }) Be sure to read the h:nvim_create_autocmd() for ...
InsertSON's user avatar
  • 396
17 votes
Accepted

How can I automatically center first search result?

You can use the following mapping: cnoremap <expr> <CR> getcmdtype() == '/' ? '<CR>zz' : '<CR>' Which can be explained like this: cnoremap Create a mapping in the ...
statox's user avatar
  • 50.1k
15 votes

How do I save a file in a directory that does not yet exist?

Another way with a vanilla Vim (without extra conf or plugins). in Vim: :!mkdir -p /folder/you/want/ :w #save file or $ vim /folder/you/want/myfile.conf $ ctrl+z # switch to the terminal then ...
xaa's user avatar
  • 251
13 votes

How do I trigger an autocommand when user enters/leaves command mode

As of version 8.0.1206, Vim supports this with CmdlineEnter and CmdlineLeave.
Eric Pruitt's user avatar
13 votes
Accepted

Start Vim in insert mode for new files

You need to add this to your vimrc: autocmd BufNewFile * startinsert The autocommand creates a command which is executed on a specific event. Here the event is BufNewFile the doc descibes it has: ...
statox's user avatar
  • 50.1k
12 votes

How do I save a file in a directory that does not yet exist?

I can recommend a vim plugin from Tim Pope called vim-eunuch which defines many extremely useful commands when your work on UNIX/Linux with Vim (check out its features!). Let's say you open vim with ...
cbaumhardt's user avatar
  • 2,139
12 votes
Accepted

Using an if-endif conditional in autocmd

The :Term command you are using does not support the -bar option so it will take | endif as an argument. You can overcome this with :execute autocmd VimEnter * if argc() == 0 && !exists("s:...
Peter Rincker's user avatar
11 votes
Accepted

autocmd with quickfix window

The parameter for both the WinEnter and BufEnter events are matched against the buffer name, which is empty for the quickfix window: :echo bufname('') [no output] You can still use the buftype ...
Martin Tournoij's user avatar
11 votes
Accepted

Delay before running vim command?

In recent vim versions, you can use timer_start to delay an operation. For example, to execute winpos 200 milliseconds after vim starts, autocmd VimEnter * call timer_start(200, { tid -> execute('...
Mass's user avatar
  • 14.1k
10 votes

What is the order of WinEnter, BufEnter, BufRead, Syntax, FileType events?

This is the first result show up on google search about 'Vim event order'. So, i think it's worth providing the actual answer. Using the command from @nobe4 answer, this is the result: Opening a new ...
C. Martins's user avatar
10 votes
Accepted

What is the relevance of has("autocmd") today?

If you install from your package manager a Vim with the feature set tiny or small, you won't get autocommand support. Also if you compile your own Vim and disable autocommand feature in feature.h (or ...
Christian Brabandt's user avatar
10 votes
Accepted

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

You need to use | to run multiple commands: set tabstop=2 | softtabstop=2 It doesn't matter if you're using multiple lines, you still need to use the |: au BufNewFile,BufRead *.html, *.css \ ...
Martin Tournoij's user avatar
10 votes
Accepted

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

The command autocmd! BufWritePre * call StripTrailingWhitespace() Removes all autocmds for the event BufWritePre and the file pattern * from the default autocmd-group and sets a new autocmd for ...
Ralf's user avatar
  • 9,257
9 votes

autocmd with quickfix window

Just a remark to Martin Tournoij answer. When you just opened a quickfix buffer/window (and not switched to it yet), the &buftype option is not set yet. You may find another autocommand event ...
Kirill Bugaev's user avatar
9 votes
Accepted

Why can't I paste commands into vi?

set mouse= is the fix for the problem, with set t_BE= substituted in an xterm environment. The 'bug' these fixes solve was actually intended as a 'feature': creating an obstacle to pasting vi ...
user19858's user avatar
  • 171
9 votes
Accepted

Is there a way to tell what autocmds have run?

Vim's 'verbose' option will give you this information. Set 'verbose' to a large enough number to get the debug information you need. 'verbose' 'vbs' number (default 0) global ...
Peter Rincker's user avatar
9 votes
Accepted

How do you manually trigger an event?

What you are looking for is :doautocmd. With this you can trigger all the autocmds in an event, or just those that match a specific pattern. Or you can scope based on autocmd group. For example, to ...
Heptite's user avatar
  • 1,066
9 votes
Accepted

Detecting the Enter and Leave of Visual mode event

September of last year, a new event was added: commit f1e8876fa2359b572d262772747405d3616db670 (tag: v8.2.3430) Author: =?UTF-8?q?Magnus=20Gro=C3=9F?= <[email protected]> Date: 2021-...
3N4N's user avatar
  • 5,764
8 votes
Accepted

Vim never shows line number for Python files even though I have `.vimrc`

Why it didn't work From :help FileType, *FileType* FileType When the 'filetype' option has been set. The pattern is matched against the ...
D. Ben Knoble's user avatar
  • 26.4k
8 votes
Accepted

How can I access buffer variables from BufUnload/BufDelete?

Use :h <afile> to get unloading buffer name, use :h getbufvar() to get buffer local variable. let b:example_tempfile = tempname() augroup example au! au BufUnload <buffer> echom ...
dedowsdi's user avatar
  • 6,268
8 votes

How do I trim whitespace automatically?

Try this: autocmd BufWritePre * %s/\s\+$//e if you want to do this for BufReadPre as well you can https://stackoverflow.com/questions/356126/how-can-you-automatically-remove-trailing-whitespace-...
gdoubleod's user avatar
  • 201
8 votes

How to remove Neovim trailing white space?

I agree with the accepted answer. Something that begins to annoy quickly is that the cursor position is reset to the beginning of the line. To keep it, one can use getpos/setpos like this: vim.api....
BillGatesPriv's user avatar
7 votes
Accepted

augroup end indentation

You should use END instead of end for the closing augroup statement. Apparently it's a bug (cf Carpetsmoker's comment), it should behave the same but it does not. From the documentation (:h :aug): ...
nobe4's user avatar
  • 16.1k
7 votes
Accepted

How do I keep custom cursor reset function from interfering with vim-template?

Since this should only work for existing files, you'll want to use BufRead instead of BufWinEnter. vim-template won't trigger BufRead. If you really need BufWinEnter to be used to process modelines ...
Tommy A's user avatar
  • 6,790

Only top scored, non community-wiki answers of a minimum length are eligible