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
41
votes
3answers
13k 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* :...
39
votes
5answers
8k 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 ...
21
votes
2answers
4k 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 ...
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 ...
16
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 -...
16
votes
1answer
2k 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 ...
15
votes
2answers
7k 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 ...
14
votes
1answer
5k 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?
14
votes
1answer
3k 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 ...
13
votes
3answers
2k 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 ...
13
votes
3answers
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 ...
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 ...
10
votes
1answer
539 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(...
9
votes
3answers
2k 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 ...
8
votes
2answers
499 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 ...
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 ...
8
votes
1answer
191 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 ...
8
votes
1answer
1k 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 ...
8
votes
1answer
293 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 ...
7
votes
2answers
495 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 ...
7
votes
1answer
1k 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 ....
7
votes
1answer
725 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 ...
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 ...
7
votes
2answers
288 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 ...
7
votes
1answer
1k 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 ...
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
1answer
356 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 ...
6
votes
2answers
2k 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 (...
6
votes
3answers
951 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 ...
6
votes
2answers
830 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: ...
6
votes
3answers
1k 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(.)) ...
6
votes
1answer
581 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
1answer
433 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,...
6
votes
1answer
373 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 ...
6
votes
1answer
271 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 ...
5
votes
4answers
2k views

Stop highlighting when entering Insert mode

I would like Vim to stop highlighting matches when entering Insert mode. And not to highlight them again until a new search is performed. A first thought would be to add the following line to my ....
5
votes
2answers
3k 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 ...
5
votes
2answers
579 views

Why doesn't Vim work with lowercase event names?

From the Vim event documentation: Vim recognizes the following events. Vim ignores the case of event names (e.g., you can use "BUFread" or "bufread" instead of "BufRead"). So in my ...
5
votes
2answers
1k 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: ...
5
votes
2answers
478 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
1answer
1k views

In an autocmd what's the use of the BufRead event?

I'm trying to learn a bit of vimscript and currently reading the 12th chapter of Learn Vimscript the Hard Way. In this page, the author says : A common idiom in Vim scripting is to pair the ...
5
votes
1answer
271 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 ...
5
votes
1answer
203 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
1k 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 ...
4
votes
2answers
258 views

How do I keep custom cursor reset function from interfering with vim-template?

I have an autocommand function in my .vimrc that resets the cursor to where it was before I last closed the file. This function looks like this: function! ResCur() if line("'\"") <= line("$") ...
4
votes
2answers
208 views

Jump to a series of line numbers one by one after a stay of x seconds at each line

I have a set of 10 line numbers, n1, n2 ,..., n10 in a code of 1000+ lines. I need to jump from one line to another. My normal practice is to do n1G, n2G... till the set is complete. It takes about 5 ...
4
votes
2answers
507 views

How to detect whether Vim has been invoked by another shell command?

I would like to modify the behavior of one of my mapping but only when Vim is reading data which were piped to it by $ vipe. The mapping closes/quits the current window/session depending on certain ...
4
votes
2answers
727 views

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

When writing a Vim help file, it's often useful to set the filetype to help to show syntax highlighting and such. But I find this annoying to work with when editing the document due to the "concealed" ...
4
votes
1answer
169 views

augroup end indentation

When I'm editing my vimrc, if I gg=G vim autoindents nicely except where augroup is involved I end up with augroup movingBackup au! au BufWritePost * call WriteBackupFile(expand("%:t"), ...
4
votes
2answers
332 views

How can I cancel reading a file into a buffer on BufReadPre?

In order to open PDFs using an external application, one can use the following script: augroup nonvim au! au BufRead *.pdf sil exe "!open " . shellescape(expand("<afile>")) | bd augroup ...

1
2 3 4 5 6