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
44
votes
3answers
14k 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* :...
3
votes
1answer
861 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, ...
2
votes
2answers
306 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 ...
9
votes
3answers
3k 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 ...
4
votes
1answer
119 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 ...
19
votes
2answers
5k 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 -...
6
votes
2answers
858 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: ...
4
votes
1answer
1k 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)...
2
votes
1answer
879 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 ...
1
vote
1answer
241 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 ...
39
votes
5answers
9k 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 ...
14
votes
1answer
4k 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 ...
8
votes
4answers
2k 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 ...
17
votes
2answers
2k 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 ...
1
vote
1answer
123 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{...
6
votes
1answer
276 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 ...
6
votes
1answer
590 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 ...
6
votes
2answers
629 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 (...
5
votes
2answers
123 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.
4
votes
1answer
483 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 ...
0
votes
2answers
373 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 ...
21
votes
2answers
5k 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 ...
15
votes
2answers
8k 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 ...
12
votes
1answer
3k 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 ...
3
votes
2answers
674 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 ...
10
votes
1answer
552 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(...
7
votes
1answer
1k 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'...
7
votes
2answers
471 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,...
5
votes
1answer
272 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 ...
2
votes
0answers
53 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: ...
8
votes
1answer
193 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 ...
7
votes
1answer
1k 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 ...
6
votes
3answers
969 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 ...
5
votes
1answer
206 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 ...
4
votes
1answer
485 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 ...
4
votes
2answers
276 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 ...
3
votes
1answer
91 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 ...
3
votes
2answers
861 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 ...
3
votes
4answers
1k 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, ...
2
votes
2answers
43 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 ...
2
votes
2answers
132 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 ...
0
votes
1answer
83 views

Why is an autocmd using cryptmethod a better way to prevent Vim from recording events than a separate vimrc?

There is a comment by Carpetsmoker which suggests a way to prevent Vim from recording events for certain filetypes: I would not recommend using a separate vimrc file, for the simple reason that it'...