Hot answers tagged

63 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 ...
user avatar
46 votes
Accepted

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

As far as I know there is no setting or some such to do this. But not all is lost, we can of course use the BufWritePre autocommand. This is executed before the buffer is written to the disk. So we ...
user avatar
35 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 ...
user avatar
  • 33.1k
31 votes
Accepted

Suppress output from a vim autocomand

Silent should go before !./make.sh, not before au. Putting silent before au just means it will register the autocommand silently. It should look like this instead: au BufWritePost * silent! !./make....
user avatar
29 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 ...
user avatar
25 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 "...
user avatar
24 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: ...
user avatar
  • 3,879
23 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 ...
user avatar
21 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 ...
user avatar
  • 46.5k
17 votes
Accepted

Is there a way to AND events in the autocmd?

An autocommand command is executed when one event occurs. You want a command to be executed after a sequence of events has occurred. One way to do that is like this: autocmd FileType c,cpp,python ...
user avatar
  • 5,934
16 votes
Accepted

Disable syntax highlighting depending on file size and type

This comment by derobert led me to the solution: autocmd Filetype xml if getfsize(@%) > 1000000 | setlocal syntax=OFF | endif This seems to reliably disable syntax highlighting in one buffer only....
user avatar
  • 755
16 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 *...
user avatar
  • 15.2k
16 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 ...
user avatar
  • 46.5k
15 votes
Accepted

Is it possible to easily work with .odt, .doc, .docx, .rtf, and other non-plain-text formats in Vim?

You should be able to add something like this to your vimrc: autocmd BufReadPost *.odt :%!odt2txt % That will send the entire buffer through the odt2txt program after it's read in by vim, but only ...
user avatar
  • 4,720
15 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: http://www.ibm.com/developerworks/library/l-vim-script-5/ Specifically, this part is ...
user avatar
  • 631
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: ...
user avatar
  • 46.5k
12 votes
Accepted

How can I run an autocmd when starting Vim with no file, a non-existing file, or a blank file (but not when starting with an existing file)?

This works for all 3 of your cases for me: function InsertIfEmpty() if @% == "" " No filename for current buffer startinsert elseif filereadable(@%) == 0 " File doesn'...
user avatar
  • 8,072
12 votes
Accepted

Run bash command on writing a file with a particular extension?

You can use autocommands to run things automatically when certain events occur. In your case, you could do this: autocmd BufWritePost *.less !less <afile> This autocommand runs every time a ...
user avatar
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 ...
user avatar
  • 46.5k
10 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.
user avatar
10 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 ...
user avatar
  • 1,999
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 ...
user avatar
  • 5,934
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 \ ...
user avatar
10 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 ...
user avatar
10 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('...
user avatar
  • 12.8k
9 votes
Accepted

How can I stop Vim from writing a file without throwing an error?

Instead of using the BufWritePre autocmd, you can use the BufWriteCmd autocmd, from :help BufWriteCmd: Should do the writing of the file and reset 'modified' if successful, unless '+' is in 'cpo' ...
user avatar
9 votes

Suppress output from a vim autocomand

The answer to your direct question is just as @EvergreenTree posted. However I feel like you are missing some of Vim builtin tool for building/compiling/linting projects. Most notably :make and the ...
user avatar
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("%")...
user avatar
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 ...
user avatar
8 votes

How can I switch to filetype=txt when entering insert mode for Vim help files?

Asked: Setting the filetype The autocommands in the question are a good start, and with 2 extra pieces of knowledge we can finish it: You can define autocommands everywhere. autocmd and augroup are "...
user avatar

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