69
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 ...
37
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 "...
26
votes
Accepted
How to enable/disable an augroup on the fly?
Building on your answer: you don't need a variable to keep state of the augroup, you can use exists() for that, provided that you know at least one of the autocmds that are part of the group:
...
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 ...
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 ...
20
votes
Accepted
What is the order of WinEnter, BufEnter, BufRead, Syntax, FileType events?
I tried googling for this with different sets of keywords, and struck gold on one such attempt with this result:
https://web.archive.org/web/20220121051339/https://developer.ibm.com/tutorials/l-vim-...
17
votes
What is the order of WinEnter, BufEnter, BufRead, Syntax, FileType events?
To complete the @sundar answer:
You can log the order of the events simply with a logging function:
augroup EventLoggin
autocmd!
autocmd BufNewFile * call s:Log('BufNewFile')
autocmd BufReadPre *...
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 ...
14
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 ...
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
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.
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 ...
12
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 ...
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
How to enable/disable an augroup on the fly?
(I actually figured it out by myself but as I didn't find an answer on this site I thought that could be usefull to create a self-answered question)
To do so it is possible to create the following ...
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
How do I execute "ctags -R" on saving *.h and *.cpp files without a confirmation prompt?
When you execute an external command with :!, Vim doesn't know what the external command might have written to the terminal, so it waits for you to confirm that you are ready to continue.
To avoid ...
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
\ ...
9
votes
How to enable/disable an augroup on the fly?
I find the easy way is to use a global variable. Example:
augroup TestAutoGroup
autocmd!
autocmd BufEnter * |
\ if get(g:, 'toggle_autocmd', 1) |
\ echom "BufEnter " . bufnr("%")...
9
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 ...
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
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 ...
8
votes
How do I define an autocommand to work on all files excepted a certain filetype?
In the same spirit as @Sundar answer but with a little more flexible syntax you can do:
let ftToIgnore = ['latex', 'plaintex']
autocmd BufWritePre * if index(ftToIgnore, &ft) < 0 | set your ...
8
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 ...
8
votes
Accepted
When exactly does <afile> differ from <amatch>?
<amatch> is exactly what is matched against the autocmd pattern. For autocmds that match filenames, symlinks are resolved, the result is canonicalized, and the full path is returned.
<afile&...
8
votes
VimLeave event not triggered?
What I was doing wrong is adding <cr> at the end of the line. That's work pretty well:
autocmd VimLeave * :!touch /home/odiszapc/vimleave
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
autocmd × 424vimrc × 70
vimscript × 51
neovim × 48
filetype × 45
buffers × 32
key-bindings × 25
command-line × 14
save × 13
functions × 12
filetype-tex × 11
vim-windows × 10
insert-mode × 9
netrw × 9
events × 9
regular-expression × 8
terminal × 8
highlight × 8
folding × 8
lua × 8
search × 7
external-command × 7
formatting × 7
quickfix × 7
statusline × 7