Skip to main content

Questions tagged [autocmd]

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

Filter by
Sorted by
Tagged with
77 votes
3 answers
32k views

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* :...
DJMcMayhem's user avatar
  • 17.7k
60 votes
7 answers
28k views

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

Suppose I start Vim to edit a new file in a directory that is not yet created: vim nonExisitingDirectory/newFile.txt Vim will happily show me an empty buffer and I can start writing my new file. But ...
DeltaLima's user avatar
  • 1,155
32 votes
3 answers
11k views

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

I've gone through the autocmd.txt help, but the information about this is scattered and not always present (eg. with BufRead). Could someone who understands this well list the order of these events -...
Sundar R's user avatar
  • 781
27 votes
2 answers
7k views

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 ...
flashburn's user avatar
  • 689
24 votes
3 answers
4k views

How to enable/disable an augroup on the fly?

I created an augroup in my .vimrc containing several autocmd and I need to enable/disable these autocommand on the fly. The idea is to create a mapping (let's say F4 for example) which would enable ...
statox's user avatar
  • 50.1k
20 votes
1 answer
4k views

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

I have an autocmd for TeX and Markdown files to save the file automatically. Nothing unusual: autocmd CursorHold *.tex,*.md w However, as custom settings for these files increased, I split them off ...
muru's user avatar
  • 25k
19 votes
2 answers
3k views

Disable syntax highlighting depending on file size and type

I often have to edit various XML files with vim, which vary wildly in size - from configuration files containing a few hunded lines to production data files with sizes up to 2GB. Having syntax ...
l4mpi's user avatar
  • 855
18 votes
2 answers
11k views

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 ...
Andre Herman Bezerra's user avatar
18 votes
1 answer
9k views

How to enable spell check for certain file types?

I flip between Ruby and Markdown files a lot. Is there a way that I can have set spell automatically set when I open a *.md file, but not when I open a *.rb one?
Sauce McBoss's user avatar
15 votes
1 answer
6k views

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

Occasionally someone sends me a .odt, .doc, and .docx file; opening them in OpenOffice is always a pain because, well, OpenOffice. Right now I use a variety of commandline tools (such as odt2txt) to ...
Martin Tournoij's user avatar
14 votes
1 answer
5k views

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)?

I would like to run an autocmd in the following scenarios when I start Vim: With no filename specified. When the file is new. When the file exists but is completely blank. In all of the above three ...
John Sonderson's user avatar
13 votes
3 answers
1k views

How to automatically lower case the second letter of a word?

When I have to type a lot of text I often tend to keep my finger pressed on shift when I write the first letter of a sentence which often give: [...]end of sentence. NEw sentence[...] Here the E of ...
statox's user avatar
  • 50.1k
12 votes
3 answers
9k views

How to remove Neovim trailing white space?

In Vim I used autocmd BufWritePre * :%s/\s\+$//e to delete trailing white space on save. Now, Neovim 0.7 has the new vim.api.nvim_create_autocmd. What is the correct syntax to adapt this autocmd in ...
Joannes's user avatar
  • 237
12 votes
4 answers
4k views

How do I define an autocommand to work on all files excepted a certain filetype?

For example, I have the following autocommand definition: augroup cursorline autocmd! autocmd WinLeave,BufLeave * set nocursorline autocmd WinEnter,BufEnter * set cursorline augroup end ...
Kit's user avatar
  • 221
11 votes
3 answers
4k views

How do I add skeleton text to new vim files?

I use Vim for writing C++, and I have quite a few #include<> statements, comments, etc. that I must include in each new file I create. I've added autocmd BufNewFile *.cpp r C:\(full file path ...
electriccello's user avatar
11 votes
1 answer
2k views

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

Context I have been using Vim for more than 6 years and have compiled it from source a couple of times during that period. Over the years my vimrc did grow. Now, I am trying to clean my vimrc and I ...
grochmal's user avatar
  • 1,656
10 votes
3 answers
862 views

How can I automatically center first search result?

I want to have the very first search result centered on the screen. I know about :nnoremap n nzz mapping, but it only works for centering next matches, not the first. I thought that maybe I could use ...
Jan Warchoł's user avatar
10 votes
1 answer
4k views

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 * ...
aonemd's user avatar
  • 203
10 votes
1 answer
859 views

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

I'm trying to improve the code from this answer to ask for confirmation before creating the missing directory, this is what I write: function! CreateDirectoryAskConfirmation(path) if !isdirectory(...
toro2k's user avatar
  • 4,962
9 votes
1 answer
2k views

BufWinLeave * mkview with unnamed file: Error 32

Following this good piece of advice, I have added these lines to my .vimrc. augroup AutoSaveFolds autocmd! autocmd BufWinLeave * mkview autocmd BufWinEnter * silent loadview augroup END It ...
iago-lito's user avatar
  • 1,122
9 votes
2 answers
925 views

Prevent FocusGained autocmd running in command line editing mode

I have the following in my vimrc to automatically reload a buffer: " Triger `autoread` when files changes on disk " https://unix.stackexchange.com/a/383044/143394 autocmd FocusGained,BufEnter,...
Tom Hale's user avatar
  • 2,711
9 votes
1 answer
3k views

What does an `autocmd User` do?

I switched to vim-plug recently. In the README, there's this snippet for using YouCompleteMe with vim-plug: " Code to execute when the plugin is loaded on demand Plug 'Valloric/YouCompleteMe', { 'for'...
muru's user avatar
  • 25k
8 votes
2 answers
2k views

autocmd with quickfix window

How can set BufEnter or WinEnter autocmds for entering quickfix window? I tried autocmd WinEnter quickfix echo 'winenter' autocmd BufEnter quickfix echo 'bufenter' None of them works. I doubt that ...
doraemon's user avatar
  • 1,667
8 votes
3 answers
4k views

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

I'm using a really clever plugin that changes the line number style (relative/normal) contextually. Basically when you enter insert mode you have normal line numbers and when you leave insert mode it ...
Sukima's user avatar
  • 1,212
8 votes
2 answers
642 views

How do I execute "ctags -R" on saving *.h and *.cpp files without a confirmation prompt?

This is a noob question, so basically in vim I want to run ctags -R when I am writing to my source or header files (*.h or *.cpp) so I tried the following: autocmd BufWritePost *.cpp :!ctags -R ...
user1135541's user avatar
  • 1,113
8 votes
2 answers
1k views

How do I run a command on creation of a new file?

I'm aiming to make scripts I write in a certain directory (or of a certain extension) automatically executable on creation. I can do this whenever a file is written to, using the following autocmd: ...
muru's user avatar
  • 25k
8 votes
1 answer
280 views

The first and last 5 lines of a file? Use for file specific spell ignore list?

Rumor has reached me that it is possible to place vim commands in the first five, or in the last five lines of a a file. But, I could not find this in Google. Any leads would be appreciated. I ...
Yossi Gil's user avatar
  • 755
8 votes
2 answers
5k views

Why can't I paste commands into vi?

I've been using vi for 40 years. A week ago I upgraded; Disaster struck! The upgrade was to VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 4 2018 23:38:28) I run this on xterm under Cygwin (...
user19858's user avatar
  • 171
8 votes
3 answers
2k views

From a TextChanged autocommand event, how to get the range that has been changed?

Problem I am trying to implement "trimming trailing whitespace in changed lines only". For this purpose, I intercept TextChanged and TextChangedI autocmd events and record the current line (line(.)) ...
intelfx's user avatar
  • 317
8 votes
1 answer
757 views

Why don't I get syntax highlighting when I open a file in an autocmd?

While trying to devise a solution to How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?, I have got so far using Python: function LookupFiles ...
muru's user avatar
  • 25k
8 votes
1 answer
427 views

vim ZZ lost output with autocommand

I want to check file before quitinq vim with custom functions, everything seems fine with QuitPre autocommand except using ZZ to exit. In this case the output is missing. Consider following function ...
mrajner's user avatar
  • 213
7 votes
2 answers
2k views

Using an if-endif conditional in autocmd

I'm pretty new to writing stuff in my .vimrc, and I'm trying to understand how to use conditionals. I'm using neovim. I'm trying to get (an enhanced version of) nvim's terminal emulator to launch in ...
ShapeOfMatter's user avatar
7 votes
3 answers
2k views

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's user avatar
  • 193
7 votes
1 answer
3k views

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

I want to run a bash command on writing a .less file, saving it and generating the corresponding .css file with the same name and in the same directory. I do have the bash command which generates the ....
oddRaven's user avatar
  • 235
7 votes
1 answer
1k views

How do you manually trigger an event?

I have case where I would like to trigger the TextChanged event manually without actually changing the text. Would that be possible? How? I know it's hacky, but it would make my life easier right now. ...
cmosig's user avatar
  • 309
7 votes
2 answers
1k views

How can I run a function / action every period of time (once an hour)?

I want to run https://stackoverflow.com/questions/7588531/vim-set-color-theme-based-off-time-of-day So something like: if strftime("%H") < 12 set background=light else set background=dark ...
Chris Stryczynski's user avatar
7 votes
2 answers
2k views

How to make autocmd FileType work when a file has multiple / compound dot separated types?

While working a Markdown plugin, we've wanted to change the filetype from mkd to markdown to increase compatibility with other plugins. To avoid breaking users .vimrc that had autocommands like: ...
Ciro Santilli OurBigBook.com's user avatar
7 votes
1 answer
2k views

When exactly does <afile> differ from <amatch>?

The documentation says: <afile> When executing autocommands, is replaced with the file name for a file read or write. <abuf> When executing autocommands, is replaced ...
muru's user avatar
  • 25k
7 votes
1 answer
2k views

Writing modified buffer to a temporary file

I'd like to write the modified buffer I'm currently editing to a temporary file periodically. In the background I could then, for example, use external tools to analyze the file. I don't want to ...
Edouard Poor's user avatar
7 votes
2 answers
427 views

au FileType netrw echom "ft is now netrw" does not show up in :messages

Entering in vim :au FileType netrw echom "ft is now netrw" Afterwards, I open a netrw window with :E . Now, I expect to have a message entry in :messages. However, no message is there. Why is there ...
Hotschke's user avatar
  • 4,790
7 votes
1 answer
3k views

Trouble to call a bash command from vimrc with autocommands | "Error: no previous command" (neovim)

basically i want to test within my vimrc, if /home/$USER/.dotfiles/nvim/bundle/vim-snippets/snippets was symlinked to /home/$USER/.dotfiles/nvim/snippets If not, it should create the symlink. the ...
uuu's user avatar
  • 547
7 votes
1 answer
3k views

how to include multiple commands nested inside a single `autocmd BufRead...`?

I want to make several keybinds and options set for certain files based on their location. Is there a more elegant way than... autocmd BufRead,BufNewFile /tmp/wiki-tag-reports/* nnoremap <Enter>...
alec's user avatar
  • 825
7 votes
1 answer
172 views

How often and when is foldexpr evaluated?

I want to toggle foldmethod between expr and manual because if we always set foldmethod=expr, then the cursor tends to move slowly in vim. I think that foldexpr is evaluated just after switching ...
kumegon's user avatar
  • 73
7 votes
1 answer
640 views

Enable cursorline and cursorcolumn after Quickfix window is opened

Due to performance, I disable the cursorline and cursorcolumn in Vim. However, in the case jumping through a Quickfix window, it is hard to locate the cursor in the file. So I would like to enable ...
tamlok's user avatar
  • 851
6 votes
2 answers
3k views

Start Vim in insert mode for new files

I mostly use Vim to edit configuration files so I understand why it starts in normal mode. But I also use vi mode for the REPL (bash, zsh, ipython, etc.) and the shell always starts in insert mode (...
isar's user avatar
  • 163
6 votes
2 answers
7k views

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 ...
Ron Obvious's user avatar
6 votes
3 answers
1k views

Switching to the local directory after loading a file?

When running gvim by double clicking with a file in a graphical file manager, like nautilus, the vim instance is called with a strange current directory (seems root sometime, but I am not sure). This ...
Rmano's user avatar
  • 758
6 votes
3 answers
2k views

How do I elegantly override file type detection patterns with more specific patterns

I have a file in my ~/.vim/ftdetect directory, which includes configuration such as: autocmd BufRead,BufNewFile,BufWritePost .envrc.template set filetype=sh However, I also have the vim-polyglot ...
Andrew Ferrier's user avatar
6 votes
1 answer
1k views

Strange behaviour of autocmd

After starting neovim with nvim -u NONE, why do the following commands :term :au BufWinEnter,WinEnter <buffer> startinsert :tabnew leave me in insert mode in the new tab, and how can I avoid ...
Rastapopoulos's user avatar
6 votes
1 answer
455 views

Fixing lag due to vim-troll-stopper plugin

I've recently installed a plugin called vim-troll-stopper. Some unicode characters are drawn exactly the same but have different meanings. I think they are called homoglyphs. Some programmers may use ...
saginaw's user avatar
  • 6,826

1
2 3 4 5
9