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
0
votes
0answers
25 views

What is the event triggered when the command line changes?

I have a function that I'd like to run every time the command line is changed, where by command line I mean the line where text is written when I save a file, get a warning, an error, or echo messages....
1
vote
1answer
38 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 ...
1
vote
0answers
17 views

Adding an header when checking an HPP file on the fly

I often use HPP files when separating a template header from its implementation. In this scheme an #include"impl.hpp" is inserted in the .h (the header) file after all relevant definitions. As such, ...
1
vote
1answer
52 views

Usage of a autocmd to write into a buffer - E523: Not allowed here

I am trying to make a plugin, which will be displaying a content of registers in the side bar and updating this side bar at every register's changing. To accomplish this task I decided to set a hook ...
2
votes
1answer
21 views

Autocmd pattern to match empty file/no filename

Say I want to match a buffer with no filename. I can use the pattern *, then manually check the filename, like autocmd myVimrc BufEnter * if @% == "" | echom 'blank filename' | endif Is there a ...
1
vote
1answer
63 views

Which event is triggered by deleting a character with the x key?

I have written a vimscript function. I want to use autocmd to call this function when a character is deleted with the x key. But autocmd requires an event name. Which event is triggered when a ...
3
votes
2answers
46 views

Abbreviations for local filtype buffers only

After typing tds I would like to see it replaced with the current time stamp in Markdown files, but eg. NOT while editing C source code. The following mostly works (stripped for brevity) except that ...
1
vote
2answers
43 views

Preventing an autocmd from overriding last cmd - :<bang><bang> (:!!)

I have an autocmd set up like this: au BufWritePost *.sh,*.zsh,.zshrc silent exec "!ctags --language-force=sh %" When I run :!!<cr> after saving a file, the command !ctags --language-force=sh %...
1
vote
1answer
53 views

Split window size change event

Couldn't find anything in autocommand-events, but is there may be a way to be notified if window split size was changed (with mouse or with C-w +)? What I try to achieve is to be able to auto-save ...
3
votes
1answer
54 views

How to override ftplugin set commands in Neovim

One thing I hate about a lot of the default Vim plugins is that they add o to formatoptions. In Vim I would get around this by using au FileType * set formatoptions-=o in my vimrc, but for some reason ...
0
votes
1answer
88 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 ...
2
votes
1answer
36 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.
0
votes
1answer
65 views

How do I tell vim to detect when a new file was created and do some action? [closed]

What I want to achieve is simply set up some auto command that will detect anytime I create a new file and based on the name of the file I want to insert a couple of lines of text in that file. So if ...
2
votes
1answer
70 views

How to handle a drag & drop event in vim in order to paste the file path instead of opening it

I would like to handle a drag & drop event of a file into gvim in order to insert the file name into the buffer I am dropping it. This should happen only when such file is an image, e.g. .png, or ....
2
votes
1answer
41 views

How can I access buffer variables from BufUnload/BufDelete?

vimdoc says: BufUnload Before unloading a buffer. ... NOTE: When this autocommand is executed, the current buffer "%" may be different from the buffer being unloaded "<afile>". Don't change ...
2
votes
1answer
63 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 ...
4
votes
2answers
159 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 ...
0
votes
2answers
77 views

Vim auto save buffer on Tmux `select-pane`

How to make (N)Vim auto save buffer on Tmux select-pane? In my vimrc I tried: " Auto save when buffer lose focus autocmd FocusLost,BufLeave * silent! wa " Save on buffer change set autowrite But ...
3
votes
2answers
58 views

mutually exclusive Autocommands for similar file extensions

I have a couple of autocmds in my vim config along the lines of this (the actual commands are quite long): au BufWritePost *.latex.md silent! ... au BufWritePost *.md silent! ... where ...
2
votes
0answers
37 views

How to profile save time

Sometimes it takes a really long time to save a file out. Saving the same file after various changes takes various amounts of time. It does not seem to be related to file size. I've disabled any ...
1
vote
2answers
35 views

Auto Command fails with file plugin command

I have following line in my .nvimrc au! BufWritePost *.elm ElmMake Each time I save an elm file vim tells me that this command does not exist. However I am able to run this command in command more ...
2
votes
1answer
98 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 ...
1
vote
1answer
44 views

Call a function after inserting a newline

In insert mode, I want to call some function if <Enter> was typed. I tried InsertCharPre but it did not work.
1
vote
2answers
61 views

Save buffer before opening new file

When I'm in a modified buffer and try to open a file using :e otherFile I get E37: No write since last change (add ! to override) Since this happens fairly often, I'd like to automatically save ...
0
votes
1answer
148 views

How to echo a message in BufWritePost so that it writes over the “~/.foo xxxL, xxC written” status message?

Say I write an autocommand for a particular file to do something when I save it, such as autocmd BufWritePost .Xresources silent! execute 'xrdb ~/.Xresources' I'm doing it with silent! execute, ...
1
vote
1answer
57 views

Avoiding unwanted TextChanged events to avoid 'modified' (hex editing)

Huh? From this interesting post, I took the idea to incorporate xxd into vim a little more nicely than the usual %!xxd. So I started structuring the code and testing. (It's a first draft.) Link to ...
2
votes
1answer
77 views

Behaviour of BufEnter

In neovim, open two different buffers as follows (using :vnew, for example): *--------------*--------------* | | | | 1 | 2 | | | ...
0
votes
1answer
56 views

I want to save my typescript file, and immediately run tsc myfile.ts from inside vim. What is the best way to accomplish this?

I am building a daemon, and nodemon currently monitors a file called server.js, which is compiled from the file I am working on, server.ts In order to compile server.ts, after saving with :w, I need ...
2
votes
0answers
46 views

Is it more efficient to use one function instead of multiple autocommands?

I have a lot of lines with with filetype-specific autocommands defining mappings and abbreviations, which looks like this: augroup vimrc au! ... au FileType blah nnoremap <buffer> aa bb au ...
1
vote
1answer
36 views

vim8 - why does my 0read not read? [closed]

I have the following function in an autoload file: function! closet#FindSkeletonForFile() if exists('b:closet_skeleton') return 0 endif let b:closet_skeleton = s:closet....
1
vote
2answers
32 views

Unable to conditionally test for presence of a plugin command & invoke it

I have the following code in my vimrc if exists(":Goyo") augroup LatexGoyo autocmd! autocmd BufReadPost,BufNewFile *.tex Goyo augroup END else augroup LatexColumnSet ...
1
vote
0answers
31 views

Autocmd commands for sharing terminal when open new tab

I was trying to create a trigger that does the following: When opening a new tab with a file, if there already exists a terminal buffer, it is opened below in this new tab. I have seen in logfiles ...
1
vote
1answer
54 views

Override indentation settings with autocmd

When editing Typescript neovim automatically sets indentexpr=GetTypescriptIndent(). I don't like the way it aligns to an open brace instead of inserting a (soft) tab or two, but it doesn't seem to be ...
1
vote
0answers
38 views

How to externally trigger [neo]vim to reload an edited file?

I have a file, /tmp/file.txt that is being edited by vim (or neovim), as well as some other external process. I wish the external app to notify vim, in such a way that vim takes that notification to ...
2
votes
1answer
94 views

Don't jump on search and replace

How do I get my cursor to not move to the last thing it replaced after a search and replace. No <C-o> or extra key presses. Is there some autocmd magic I can setup to listen on any substitute ...
1
vote
3answers
102 views

NERDTree maximize on enter

I'd like to maximize NERDTree when I enter the NERDTree buffer. The command I have so far is: autocmd BufEnter NERD_tree_* <A> ...but the <A> part is wrong. I get the error: E21: Cannot ...
0
votes
1answer
76 views

Reload vim autocmd Filetype [duplicate]

I tried searching for this on the internet but I get bombarded with answers saying to :so $MYVIMRCwhich does not work if I add lines of the type autocmd FileType foo bar. What's a more efficient way ...
2
votes
0answers
19 views

How can I trigger the autocmd FileType commands for all buffers? [duplicate]

I have a key binding to reload my ~/.vimrc. However, some of my options set via autocmd FileType are not active after reloading the config. Those are to set the text width, creating highlights, etc. I ...
1
vote
0answers
49 views

Automatically saving on FocusLost sometimes stops working for a gvim window

In my ~/.vimrc I have at the moment: autocmd BufLeave,FocusLost * nested silent! wall Which most of the time works as I would like, however sometimes it seems saving simply stops working (for a ...
0
votes
1answer
132 views

Apply templates when new file created

I have a file template which is rendered with replacing some stubs when new file created: augroup templates autocmd! autocmd BufNewFile *.java $r ~/.vim/templates/skeleton.java|call s:...
1
vote
1answer
50 views

Execute command only for specified filetype

I'm setting up an environment to automatically recompile .tex files on save. I've found solution for this: autocmd BufWritePost *.tex :! latexmk -pdf But is there any better way to do this? It seems ...
1
vote
0answers
36 views

Keep `scroll` equal to current window height

Is there a way to update scroll every time window height changes? I know, that I can achieve the same behavior calling winheight(0) on each ctrld press as suggested here, but I'm curious if there is ...
0
votes
1answer
93 views

Always move cursor to top of gitcommit buffer

I have the following autocmd in my vimrc. augroup line_return au! au BufReadPost * \ if line("'\"") > 0 && line("'\"") <= line("$") | \ execute 'normal! g`"zvzz' | \ ...
2
votes
1answer
198 views

why doesn't this autocmd take effect for neovim?

I am writing an autocmd which will warn me if the file opened is not in UTF-8 format. This is what I have got: augroup non_utf8_file autocmd! autocmd BufRead * if &fileencoding != 'utf-8' ...
3
votes
2answers
284 views

autocmd for the event: “the first time a buffer is entered”

I know that the autocommand autocmd BufEnter will be triggered every time a buffer is entered. Is there a way to define an autocmd that will be triggered only the first time a buffer is entered?
3
votes
1answer
412 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 * ...
0
votes
1answer
73 views

Enable line numbering for directories

When viewing a directory (using :Explore, for example), I would like to see line numbers. I can run :set number after opening the directory, and that works, but I can't get numbering to turn on by ...
0
votes
2answers
100 views

Trigger autocommand on error?

Is there an autocommand that is triggered when an error occurs (a Vim error of the type E###)? A try/catch block isn't enough because I need it at a more global level.
1
vote
1answer
74 views

VimEnter autocmd to change cursor shape sometimes outputs escape sequences

Inspired by this answer, I have the following in my vimrc: augroup cursor au! autocmd VimEnter * silent !echo -ne "\e[1 q" augroup end However, this sometimes prints the following on startup:...
1
vote
0answers
51 views

Run commands before and after a buffer is externally changed

Imagine I have a Vim buffer open and the file is changed externally (example: I switched git branches or the file was just edited normally). Any time that happens, I want to have 2 commands run. The ...

1
2 3 4 5