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
72 votes
3 answers
27k 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.2k
29 votes
3 answers
9k 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
  • 741
4 votes
1 answer
1k views

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

I found it strange that I can never have Vim show line number automatically for .py files, even if I have this as my ~/.vimrc set background=dark autocmd FileType py,c set number Strangely enough, ...
iBug's user avatar
  • 227
2 votes
2 answers
632 views

Using `TabEnter` and `FileType` in the same `autocmd`

I would like to set set spell whenever I open a tex file. My first attempt is autocmd FileType tex setlocal spell I am using setlocal here because I may open other files in new tabs after opening a ...
hklel's user avatar
  • 123
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
4 votes
1 answer
321 views

Autocmd treat user defined command differently?

This works fine: augroup au_test | au! autocmd BufNew * if 1 | echom 123 | endif augroup end But this doesn't work: com Test echom 123 augroup au_test | au! autocmd BufNew * if 1 | Test | endif ...
dedowsdi's user avatar
  • 6,058
7 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
  • 24.5k
4 votes
1 answer
2k views

How to set a local variable for a plugin in a BufNewFile autocmd

I currently have autocmd BufRead *.sage \ set filetype=python | \ let b:syntastic_skip_checks=1 which works as intended (sage files are treated as Python, but with Syntastic disabled)...
hugov's user avatar
  • 41
3 votes
1 answer
2k views

Fire autocmd FileType on current buffers in windows

Context I have shortcuts to edit my vimrc and to source my vimrc. When I'm writing code I can quickly add a new mapping and then source vimrc. And all that would be good if I did not use autocmd ...
grochmal's user avatar
  • 1,636
6 votes
3 answers
1k 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
  • 185
1 vote
1 answer
466 views

Unmapping filetype mappings

I'm trying to unmap some key sequences mapped by python filetype plugin. While I know a better way, I'm still curious if it can be done with autocommands. Here's what I've got so far: augroup ...
x-yuri's user avatar
  • 349
55 votes
6 answers
22k 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,095
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
  • 785
15 votes
1 answer
5k 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
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
8 votes
2 answers
4k 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
2 answers
842 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,451
8 votes
1 answer
719 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
  • 24.5k
6 votes
2 answers
413 views

Is there an autocmd-like event for when operator pending mode is entered?

I'd like to make a plugin to display currently pending keys while in operator-pending mode. As the first part of that, how would I register an autocmd to run when operator-pending mode is entered? I'm ...
lahwran's user avatar
  • 161
5 votes
3 answers
1k 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
4 votes
1 answer
899 views

Remove augroups around autocmds in .vimrc

For each autocmd (or set of related autocommands) in my .vimrc I currently define an augroup to prevent having multiple autocommands when I re-source my .vimrc. I do not need the augroup for anything ...
cbaumhardt's user avatar
  • 2,059
2 votes
2 answers
5k views

FileType autocommand not working in Neovim

My init.vim is just au FileType * echom "test" However, if I open a file, neovim-qt will not echo test. If I load a new file while the window is open, it will also not work. I have managed to find ...
Mason's user avatar
  • 461
1 vote
1 answer
202 views

formatoptions for hybrid (short markup + long paragraphs) content

I am trying to write my PhD thesis in vim using LaTeX So the documents will have two distinct types of content - markups that are relatively short, and paragraph text that is rather long. \begin{...
Dr Krishnakumar Gopalakrishnan's user avatar
27 votes
2 answers
6k 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
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
14 votes
1 answer
4k 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
10 votes
1 answer
787 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,672
10 votes
3 answers
784 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
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
  • 24.5k
8 votes
1 answer
252 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
  • 745
7 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,597
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
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
  • 748
5 votes
1 answer
361 views

Run file though a command when opening from Explore

I have configured vim to automatically decompile ".class" files similar to the instructions in: http://vim.wikia.com/wiki/VimTip54. I have made a minor change to use cfr_0_115 as below: augr class ...
user98776's user avatar
  • 171
5 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
5 votes
1 answer
269 views

Different autocmd behavior when using --remote-tab-silent?

This question is a follow-up to Switching to the local directory after loading a file? I have the following in my .vimrc: autocmd BufReadPost *.tex lchdir %:p:h with the intent of setting the ...
Rmano's user avatar
  • 748
4 votes
1 answer
3k views

Delay before running vim command?

This is probably just a bandaid for a greater problem, but I had the following code for MacVim (GUI mode) in my .vimrc autocmd GUIEnter * winpos 100 50 The problem is it's not working properly on ...
Tallboy's user avatar
  • 165
4 votes
2 answers
898 views

Saving a scratch buffer

I currently use scratch buffers a lot for various uses such as when I read from STDIN to quickly analyze some data or take some very temporary notes. That way I don't have to worry about a swap file ...
Samuel Jackson's user avatar
3 votes
1 answer
918 views

autocommand within conditional autocommand

I have an autocommand that is supposed to register a different autocommand whenever I enter a buffer where a certain condition is met: let w:contextlist_open = 1 augroup LaunchShowContext ...
mnosefish's user avatar
  • 173
3 votes
1 answer
163 views

Beep upon using a mapping

Well, I try to use the following convention. ± (as unused key) is used for increase search where / is used for regular search. The implementation: set noincsearch nmap ± <esc>:set ...
eyal karni's user avatar
  • 1,176
3 votes
4 answers
3k views

Autocmd on directory to replace netrw

I use the ranger plugin wich I can trigger with a shortcut. But when I open a directory I would like to use the plugin instead of netrw. So I have found that the filetype of directories is netrw, ...
Nicolas Scotto Di Perto's user avatar
3 votes
2 answers
1k views

Selectively enable a subset of vim plugins for specific category of workflows (plugin virtualenv for vim projects)

I use vim for many different categories of projects, typically: Writing code (3 languages, python, Matlab and C) Writing documentation ( .rst, .html) Writing reports (.tex) Readme file (.md) As a ...
Dr Krishnakumar Gopalakrishnan's user avatar
2 votes
2 answers
2k views

Run autocmd when creating/deleting a buffer

I have two functions tex#PDFOpen() and tex#PDFClose() to automatically open and close the pdf file created by a TeX document. The way I assign them to autocommands is via the following autogroup in ~/....
noibe's user avatar
  • 179
2 votes
1 answer
250 views

Mapping specific for diff mode and vimdiff

My idea is that whenever &diff is true for a buffer, I would like some mappings to be active when that buffer is the current buffer. The following attempt works for when I open a session in diff ...
Enlico's user avatar
  • 2,054
2 votes
0 answers
75 views

Modify the path of a file be opened

I am trying to write a function that runs before a new file is created, or an existing file is opened. What I would like to do is edit the path to the file which is being opened depending on the path. ...
run_the_race's user avatar
2 votes
2 answers
271 views

Where do filetype-specific autocommands live?

I have a plugin for a specific filetype (*.tex), and I'm wondering where I can find all the commands which are executed when I type :e examplefile.tex? In general, is there a standardized practice for ...
Zim's user avatar
  • 187
2 votes
0 answers
73 views

the matching pattern for TabClosed autocmd

If I want to setup a TabClosed autocmd for a specific tab page only, what should I do? Can I use the autocmd {pat} argument? Is this {pat} still matched against the filename? Moreover, the t: ...
doraemon's user avatar
  • 1,597
2 votes
0 answers
703 views

Vim not recognizing markdown and tex files

Here is a sample of my .vimrc, that doesn't do a thing (the autocmd does not work) when I open a .md or .tex file, but works fine for .py files: augroup filetype_python autocmd FileType python ...
Ul Tome's user avatar
  • 31
1 vote
1 answer
31 views

Is it correct that between a CmdlineLeave and a CmdwinEnter there must be a CmdlineEnter?

Given augroup Dummy autocmd! autocmd CmdLineEnter / echom "CmdLineEnter /" autocmd CmdwinLeave / echom "CmdwinLeave /" autocmd CmdLineLeave / echom "CmdLineLeave /&...
Enlico's user avatar
  • 2,054
1 vote
1 answer
201 views

How do I set spelling and spelling highlight for markdown files?

When I do this: :set spell spelllang=en_us :hi SpellBad cterm=underline It underlines the spell on markdown. I just need this auto command on markdown files. If I put these commands in my .vimrc, and ...
eguneys's user avatar
  • 145