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 ...
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 ...
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 ...
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 "...
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 ...
23
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 ...
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 ...
16
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 ...
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.
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:
...
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:...
11
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 ...
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 ...
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('...
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 ...
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
\ ...
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 ...
10
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 ...
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 ...
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 ...
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
...
9
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....
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-...
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 ...
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 ...
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-...
7
votes
Accepted
Fire autocmd FileType on current buffers in windows
Is there a good way to fire the FileType event on all windows that Vim is currently showing?
Use :e instead of :set ft=python to reset the current buffer. It re-reads the file and triggers the ...
7
votes
Accepted
Make vim switch to other window after opening a diff
You could add this to your .vimrc:
command! -nargs=1 -complete=file Diffsplit diffsplit <args> | wincmd p
This create a Diffsplit command (note the capitalized D) which takes one argument and ...
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 ...
7
votes
Accepted
Autocmd event for `autoread`?
One way to create a highlighted message is this:
echohl WarningMsg | echo "Buffer changed!" | echohl None
The first command selects a particular highlight mode for any subsequent echo calls. When ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
autocmd × 452vimrc × 73
vimscript × 54
neovim × 54
filetype × 46
buffers × 33
key-bindings × 27
command-line × 18
save × 15
lua × 13
vim-windows × 12
functions × 12
filetype-tex × 11
regular-expression × 9
terminal × 9
insert-mode × 9
netrw × 9
events × 9
highlight × 8
folding × 8
formatting × 8
statusline × 8
search × 7
external-command × 7
quickfix × 7